PDA

View Full Version : [SOLVED:] Add a VBA Reference Library via code



Kim75
05-21-2010, 05:43 PM
Hi the forum,

For Excel workbook, I found this code : http://www.vbaexpress.com/kb/getarticle.php?kb_id=267 Thanks :friends:

But for Word, I found nothing :dunno Is it possible to add library reference programmatically with Word document ?

Using a ListView with MS Word UserForm requires "Microsoft Windows Common Controls 6.0 (SP6)" library reference.

And I have to avoid that users will be obliged to add this reference manually for using the document I send them.

Thanks for help, Kim.

Tinbendr
05-21-2010, 07:16 PM
How about


Application.VBE.ActiveVBProject.References.AddFromFile [Filename]

where filename is path\file to the reference?

Kim75
05-22-2010, 03:09 AM
Hi Tinbendr, the forum,

Thank you for your response, I stayed with AddFromGUID method and replaced "ThisWorkbook.VBProject" by "Application.VBE.ActiveVBProject"


Sub AddReference()
Dim strGUID As String, theRef As Variant, i As Long
strGUID = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}"
On Error Resume Next
For i = Application.VBE.ActiveVBProject.References.Count To 1 Step -1
Set theRef = Application.VBE.ActiveVBProject.References.Item(i)
If theRef.isbroken = True Then
Application.VBE.ActiveVBProject.References.Remove theRef
End If
Next i
Err.Clear
Application.VBE.ActiveVBProject.References.AddFromGuid _
GUID:=strGUID, Major:=2, Minor:=0
Select Case Err.Number
Case Is = 32813
Case Is = vbNullString
Case Else
MsgBox "A problem was encountered trying to" & vbNewLine _
& "add or remove a reference in this file" & vbNewLine & "Please check the " _
& "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
End Select
On Error GoTo 0
End Sub


That works, but not straight off :

If I place this macro in a module, and I call it from : Private Sub UserForm_Activate()
Call AddReference
End SubThat works only when I click for the second time the button calling UserForm.

I look for a way to run this macro when document opening, I attributed to the macro the name of Sub_AutoOpen(), in place of Sub AddReference(), and placed it in ThisDocument, and after in a Module, but that doesn't work.

Kim.

Tinbendr
05-22-2010, 04:51 AM
You'll have to add it to the Document_Open in ThisDocument.
(If you use a template, then use Document_New.)


Private Sub Document_Open()
AddReference
End Sub
Also, you'll have to add a trust option. (The Resume Next throws it into a endless loop.)

From the document, Tools, Macro, Security, Check the 'Trust access to Visual Basic Project'.

I'm pretty sure the recipient will have to do the same thing. So you're facing the same problem.

Don't addins carry the references for you? Maybe someone else can comment on this.

Kim75
05-22-2010, 05:02 AM
Hi Tinbendr,

I specify that I don't use a template, it's a simple document (NameDocument.doc).

I added in ThisDocument :


Private Sub Document_Open()
AddReference

End Sub

"Trust access to Visual Basic Project" is checked, but that doesn't resolve the problem.

Kim.

Tinbendr
05-22-2010, 05:19 AM
Weird....... The first several times I loaded #2, it didn't work, but after closing Word all the way down, and reloading the file, it works.

Try a restart, both Word and system and see if it clears up.

I'm out of suggestions.

Kim75
05-22-2010, 05:25 AM
Tinbendr,

If you open the document the first time and activate the reference by click two times the button calling UserForm, and after you close the document with save change, it's normal that if you open the same document a second time, you will have the reference activated.

Kim.

Tinbendr
05-22-2010, 10:03 AM
Yes, but I had intentionally unchecked the reference before closing the file. And I did not save changes.

You are right, though. If I get out of Word, then reload it, it does not set the reference. I even ran the Sub twice, but it didn't set it. :think:

TonyJollans
05-24-2010, 02:57 AM
Just skimming (as always), a quick point:

Application.VBE.ActiveVBProject gives you the active project in the VBE - at runtime this is not necessarily your project. Use ThisDocument.VBProject instead.

Kim75
05-27-2010, 05:24 AM
Hi Tony, Tinbendr, the forum,

Just for information, regrettably this macro does not work, neither with "ThisDocument.VBProject" nor with various tried manners, the application word bug.

Bonne journée, Kim.

TonyJollans
05-27-2010, 06:02 AM
No bug I can see. I just took Ken's sample code, changed ThisWorkbook to ThisDocument, and added in the GUID, and it added the reference for me.


Sub AddReference()
'Macro purpose: To add a reference to the project using the GUID for the
'reference library
Dim strGUID As String, theRef As Variant, i As Long
'Update the GUID you need below.
strGUID = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}"
'Set to continue in case of error
On Error Resume Next
'Remove any missing references
For i = ThisDocument.VBProject.References.Count To 1 Step -1
Set theRef = ThisDocument.VBProject.References.Item(i)
If theRef.isbroken = True Then
ThisDocument.VBProject.References.Remove theRef
End If
Next i
'Clear any errors so that error trapping for GUID additions can be evaluated
Err.Clear
'Add the reference
ThisDocument.VBProject.References.AddFromGuid _
GUID:=strGUID, Major:=1, Minor:=0
'If an error was encountered, inform the user
Select Case Err.Number
Case Is = 32813
'Reference already in use. No action necessary
Case Is = vbNullString
'Reference added without issue
Case Else
'An unknown error was encountered, so alert the user
MsgBox "A problem was encountered trying to" & vbNewLine _
& "add or remove a reference in this file" & vbNewLine & "Please check the " _
& "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
End Select
On Error GoTo 0
End Sub

Kim75
05-27-2010, 07:15 AM
Hi Tony,

For me too this macro worked the first times when I tested it, but when I used it the next day, it did not work any more, I don't know why ...

Kim.

TonyJollans
05-27-2010, 07:29 AM
What did it do when it 'did not work'?

Kim75
05-28-2010, 04:45 AM
Hi Tony,

I open the document with no problem at all, butwhen I click the command button to call the UserForm, the UserForm opens everything in white and the application bug.

You will notice that I changed the major and the minor of the macro according to the reference in question which is "Microsoft Windows Common Controls 6.0 (SP6)".

Here is the document in question : http://sd-1.archive-host.com/membres/up/779983057/TestRef.doc

And below the image of the bug :

http://sd-1.archive-host.com/membres/up/779983057/BugListe.jpg

Kim.

TonyJollans
05-30-2010, 01:34 AM
I'm guessing that you do not have "Trust access to VB Project" set (Tools > Macro > Security > Trusted Publishers tab). Because you ignore errors, your code can loop forever trying to check for broken references when it isn't allowed to do the check. Whatever the reason, some basic debugging should highlight it much more easily than I can guess.

Kim75
05-30-2010, 01:13 PM
Hi Tony,

Indeed, I had forgotten that, "Trust access to VB Project" was not checked ...
In fact, it's little annoying to have to ask users to check it, that frightens them ...

Thanks, Kim.