Consulting

Results 1 to 16 of 16

Thread: Add a VBA Reference Library via code

  1. #1

    Add a VBA Reference Library via code

    Hi the forum,

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

    But for Word, I found nothing 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.
    Last edited by Kim75; 05-21-2010 at 05:57 PM.

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    How about

    Application.VBE.ActiveVBProject.References.AddFromFile [Filename]
    where filename is path\file to the reference?
    Last edited by Aussiebear; 04-06-2023 at 09:26 PM. Reason: Adjusted the code tags

    David


  3. #3
    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 : [vba]Private Sub UserForm_Activate()
    Call AddReference
    End Sub[/vba]That 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.
    Last edited by Aussiebear; 04-06-2023 at 09:27 PM. Reason: Adjusted the code tags

  4. #4
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    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.
    Last edited by Aussiebear; 04-06-2023 at 09:28 PM. Reason: Adjusted the code tags

    David


  5. #5
    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.
    Last edited by Aussiebear; 04-06-2023 at 09:29 PM. Reason: Adjusted the code tags

  6. #6
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    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.

    David


  7. #7
    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.

  8. #8
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    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.

    David


  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    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.

  11. #11
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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
    Last edited by Aussiebear; 04-06-2023 at 09:30 PM. Reason: Adjusted the code tags
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  12. #12
    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.

  13. #13
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    What did it do when it 'did not work'?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  14. #14
    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...57/TestRef.doc

    And below the image of the bug :



    Kim.
    Last edited by Kim75; 05-28-2010 at 08:03 AM.

  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  16. #16
    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.

Posting Permissions

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