Problems programming to the registry using RegEnumKey

  • Thread starter Thread starter Simon Livings via OfficeKB.com
  • Start date Start date
S

Simon Livings via OfficeKB.com

I am experiencing problems trying to do something with the registry and
would really appreciate any help that you guys can provide.

Essentially I am writing an add-in for exclusive use within my department
at work, and as the add-in gets re-released an extra item appears in the
Add-Ins dialog box in Excel. I am trying to delete the old unused items
automatically (I know that they can be deleted by hand by looking in
Software\Microsoft\office\11.0\Excel\Add-In Manager).

What I would like to do is enumerate all add-ins listed in the Add-In
Manager section in the registry, and where the add-in matches a previous
version of my Add-In and would like to delete the item from the registry so
that it doesn't appear in the Add-In dialog box anymore. I have all of the
API information that I know I will need by looking at other posts in the
newsgroups, but I can't get the RegEnumKey to work so that it provides a
list of all add-ins in the section.

The code I am trying to use to achieve this is as follows (I haven't got to
the deleting part).

Public Sub GetKeys()
Dim x, strSection As String

strSection = "Software\Microsoft\Office\" & Application.Version & "\Excel\
Add-In manager\"

x = EnumerateValues(HKEY_CURRENT_USER, strSection)
MsgBox UBound(x)
End Sub

Public Function EnumerateKeys(ByVal HKey As Long, ByVal hSubKey As String)
Dim Result As Long
Dim OpenKey As Long
Dim KeyName As String
Dim KeyNames() As String
Dim KeyCount As Long
ReDim KeyNames(0) As String

KeyCount = 0

Result = RegOpenKey(HKey, hSubKey, OpenKey)

Do Until Result <> 0
KeyName = Space(255)
Result = RegEnumKey(OpenKey, KeyCount, KeyName, 255)

If Result = 0 Then
KeyNames(UBound(KeyNames)) = Left(KeyName, Len(Trim(KeyName)) - 1)
ReDim Preserve KeyNames(UBound(KeyNames) + 1) As String
KeyCount = KeyCount + 1
End If
Loop


If UBound(KeyNames) > 0 Then ReDim Preserve KeyNames(UBound(KeyNames) - 1)
As String
KeyCount = KeyCount - 1

RegCloseKey OpenKey

End Function


If anyone can help with this I will be extremely grateful.
Many thanks,
Simon Livings.
 
Apologies - I have just noticed that I didn't assign the Keynames array
back to the function to be passed back to the calling sub. However, the
operation of the function still doesn't work.

Thanks.
 
please note that calling this routine from inside an excel instance is
useless as excel will rewrite the registry when it closes.


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Simon Livings via OfficeKB.com wrote :
 
Agreed - but my intention is to check the registry the next time Excel is
launched to help keep the dialog box tidy.
 
i think you missed my point:

you cant keep that box tidy with code running from inside excel
you must use an external (vb6) app or vbs script.

everytime you close excel it will rewrite those lines.
Try:
open excel..
open regedit.. kill the AddinManager entries.
close excel
refresh regedit window.




--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Simon Livings via OfficeKB.com wrote :
 
Surely, it would be build to build an installer (using VB, not VBA) that
does all of the work, installs the addin, tidies the registry, etc.

I have an in-progress VB app that does something similar that I can send you
if you want to play with it. I would be quite happy to help you develop it.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Thanks - I just tried to do it by hand and I see exactly what you mean. (I
must have had Excel closed when I deleted things by hand the first time so
assumed you could do it programmatically).

Thanks for pointing this out. I can stop scratching my head now and move
on!!
 
I appreciate the offer Bob, but my circle of expertise is really with VBA
and I am keeping it simple in that respect. Whilst I would love to know
how to write an installer and would love to see yours, I think that this
would over-complicate the toolkit that has been developed and would
probably be frowned upon by my department and probably IT as well!

Thank you nonetheless.
Simon.
 
Simon,

Why would any organisation that allows you to install add-ins frown upon an
automated installer? You could even pass it to IT for, no doubt, rigorous
operational testing (tongue firmly in cheek!). to ensure it conforms to best
and secure practice.

Don't forget VB is very very similar to VBA, you'll see nothing unusual in
my code. If you want to see the code, drop me your email address, and I will
post it.

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top