How to programatically add a COM REfrence

  • Thread starter Jim Vierra [661815]
  • Start date
J

Jim Vierra [661815]

Does anyone know how to add a reference to a COM assembly in VBA use code.

Example: To support ADODB objects we need to add a reference to the ADODB object library from the [Tools][Refences} menu in the VBA editor. This can be done in VB with code and I believe it can be done in VBA. Does anyone know how to do this or where the documentation is for this (if it exeists)
 
C

Cindy M -WordMVP-

Hi Jim,
Does anyone know how to add a reference to a COM assembly in VBA use code.

Example: To support ADODB objects we need to add a reference to the ADODB object library from the [Tools][Refences} menu in the VBA editor. This can be done in VB with code and I believe it can be done in VBA. Does anyone know how to do this or where the documentation is for this (if it exeists)
Should be possible using the VB Extensibility Library, with the AddFromGUID method of the References collection.

In order to get into the Help files for this, make sure you have an active reference to the library, then type something like

ActiveDocument.VBProject.VBComponents(1)

then press F1.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :)
 
J

Jim Vierra

Thanks for the help Cindy. I tried that from Outlook but it appears that Outlook will only accept VBE access from a com add-in. I need to verify this but VBE is not exposed even if you add a reference. If I force access even to read the version I get "Programmatic access to Visual Basic Project is not trusted". It may be accessible when in Exchange mode. Probably also requires a signed code module.

Here is the code for Access and probably Word and Excel although I have only tested with Accesss 2003. (Posted in case someone else is looking.)

Sub TestAddRef()
On Error Resume Next

Dim e As Object
' you can use AddFromGuid here too.
Set e = Application.VBE.ActiveVBProject.References.AddFromFile("c:\windows\system32\wbem\wbemdisp.tlb")
If Err.Number <> 0 Then 'maybe it's already loaded or file is missing.
MsgBox Err.Description
Else
MsgBox "module loaded"
MsgBox e.Name ' print new module name.
End If

' VBE also is a good way to enumerate references.
Dim o As Object
For Each o In Application.VBE.ActiveVBProject.References
Debug.Print o.Name
Next
End Sub

' VBE gets windows too
Dim w As Window
For Each w In Application.VBE.ActiveVBProject.References.item(1).VBE.Windows
Debug.Print w.Caption
Next


--
Jim Vierra 661815
Cindy M -WordMVP- said:
Hi Jim,
Does anyone know how to add a reference to a COM assembly in VBA use code.

Example: To support ADODB objects we need to add a reference to the ADODB object library from the [Tools][Refences} menu in the VBA editor. This can be done in VB with code and I believe it can be done in VBA. Does anyone know how to do this or where the documentation is for this (if it exeists)
Should be possible using the VB Extensibility Library, with the AddFromGUID method of the References collection.

In order to get into the Help files for this, make sure you have an active reference to the library, then type something like

ActiveDocument.VBProject.VBComponents(1)

then press F1.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)


This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :)
 

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

Top