Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Star Lounger
    Join Date
    Sep 2002
    Location
    Cleveland, Ohio
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find a Registry Key (VBA/Word 2003/SP2)

    <font face="Comic Sans MS">I need to try and simply detect if a key exists in the registry. I don't need to read it. The key is HKEY_CURRENT_USERSoftwareMicrosoftOffice11.0WordRe siliency. Is this possible? I've tried using some Shell code to get this to work, but I haven't been able to so far. I'm not sure why. I'd like to place the code in an AutoExec routine of a startup template so that it fires when Word opens and tells me if this key exists or not. Can anyone help me? </font face=comic>
    finalword

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    Assuming that you really mean a registry key (in the left hand pane of RegEdit) and not a registry value (in the right hand pane), you can use this:

    Dim oShell As Object
    Dim strRegKey As String
    Set oShell = CreateObject("WScript.Shell")
    On Error Resume Next
    strRegKey = oShell.RegRead("HKEY_CURRENT_USERSoftwareMicrosoft Office11.0WordResiliency")
    If Err Then
    ' Code for if the key doesn't exist
    MsgBox "The key does not exist"
    Else
    ' Code for if the key does exist
    MsgBox "The key exists"
    End If

    Note: the backslash after the registry path is essential if you're looking for a key. You'd omit it if you were looking for a value.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    Hi There

    This may be useful to you

    http://vba-corner.livejournal.com/3054.html
    Jerry

  4. #4
    Star Lounger
    Join Date
    Sep 2002
    Location
    Cleveland, Ohio
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    Thanks for the code Hans. It still isn't finding the key though. And yes, it is a key (located on the left) . I didn't know about the backslash, but I was sure to include it when I quickly ran your code.
    finalword

  5. #5
    Star Lounger
    Join Date
    Sep 2002
    Location
    Cleveland, Ohio
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    Thanks for the Website Jerry! That was the site where I got the code I was working with earlier today. I just can't seem to get it to find this darn key! Hmmmm. I'm thinking of doing an API call. Anyone have experience with this?
    finalword

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    Check carefully that you haven't made a mistake. I've tested the code and it works correctly for me.

  7. #7
    Star Lounger
    Join Date
    Sep 2002
    Location
    Cleveland, Ohio
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    I'm still not able to get this to work. Do I need to declare something or make sure some sort of setting is turned on to use this type of code? I currently have the key in my registry, but each time I run this macro, it tells me that the key doesn't exist.

    Here is my code. It is the only code in this startup template and the startup template is the first to load.

    Sub AutoExec()

    Dim oShell As Object
    Dim strRegKey As String
    Set oShell = CreateObject("WScript.Shell")
    On Error Resume Next
    strRegKey = oShell.RegRead("HKEY_CURRENT_USERSoftwareMicrosoft Office11.0WordResiliency")
    If Err Then
    ' Code for if the key doesn't exist
    MsgBox "The key does not exist"
    Else
    ' Code for if the key does exist
    MsgBox "The key exists"
    End If

    End Sub
    finalword

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    I'm sorry, I can't explain that. The code is self-contained, it doesn't need special references or settings. It works correctly for me.

  9. #9
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    I just copied the code from your post, pasted it into a module in Word, and ran it - it ran fine and reported that the key exists.

    StuartR

  10. #10
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    Like finalword I am unable to get the code to find an existing key.

    Does the screenshot of the Locals window offer any clues?

    Is it possible that the Anti-Virus application is interfering?
    Attached Images Attached Images
    Regards
    Don

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    Your screenshot doesn't provide additional information, as far as I can see.

    I don't think AntiVirus programs have anything to do with this.

  12. #12
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    I wonder if you are getting an error that is not "Key does not exist" but something different. Can you put err.number and err.description into a message box in case they offer a clue.

    For a non-existant key (I added an x after the word resiliency) I get
    err.number = -2147024894
    err.description = Invalid root in registry key "HKEY_CURRENT_USERSoftwareMicrosoftOffice11.0WordR esiliencyx".

    StuartR

  13. #13
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    I added:

    If Err Then
    Debug.Print Err.Number; ; Err.Description

    and the return was "Unable to open registry key "HKEY_CURRENT_USERSoftwareMicrosoftOffice11.0WordR esiliencyDisabledItems" for reading"; (I can manually open the Registry, and the key exists) - could this be an Admin rights issue?
    -John ... I float in liquid gardens
    UTC -7ąDS

  14. #14
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    Message box as requested.
    Attached Images Attached Images
    Regards
    Don

  15. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find a Registry Key (VBA/Word 2003/SP2)

    No idea, I use a local admin-equivalent account both at home and at work.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •