• add reference under vba (Word 2003)

    Author
    Topic
    #438549

    Good morning loungers

    I want to add a reference to a project with vba. The equivalent to selecting Tools | References in the vba editor.

    I have the following in Template A.

    Sub document_new()
    Dim strLibrary as string
    strLibrary = ActiveDocument.AttachedTemplate.Path & “library.dot”
    AddIns.Add FileName:=strLibrary
    library.Module1.show_form
    End Sub

    This works well as long as Template A has been saved with the reference to Library.dot checked under tools | references in the vba editor.

    I was hoping to add the reference with code using the .addfromfile method. Something like this

    vbProj.references.addFromFile(strLibrary)

    Anyway I can not make it happen

    Any assistance would be appreciated.

    Geof

    Viewing 0 reply threads
    Author
    Replies
    • #1045990

      Good evening,

      To be able to set a reference in VBA in Word, you must already have set … a reference. Namely, to the Microsoft Visual Basic for Applications Extensibility 5.3 library. This exposes the References collection. You can then use

      ActiveDocument.VBProject.References.AddFromFile strLibrary

      However, I’ve never had much success with this type of code.

      • #1045993

        Hello Hans
        Thank you for the reply and assistance.

        It would seem then that it is necessary to manually set a reference prior to introducing code that avoids manually setting another reference.
        Hmmm !!.
        I can see why you commented that you had limited success with this type of code.

        Thank you for your efforts on the board. Happy New Year.

        Geof

      • #1045997

        Hello Hans
        I have had less than little success with this type of code.
        I set the reference to the extensibility 5.3 library with no success.
        I will abandon this so please do not trouble yourself further with it on my behalf.

        As a matter of interest however, the following seem to work without the ref to the extensibility 5.3 library.
        Once again thanks. I will consign this idea to the rubbish bin.

        Cheers
        Geof

        ‘——–
        Sub removeReference()
        Dim vbProj
        Dim vbRef

        Set vbProj = ActiveDocument.VBProject

        For Each vbRef In vbProj.References

        If vbRef.Name = “library” Then
        vbProj.References.Remove vbRef
        Exit Sub
        End If

        Next
        End Sub

        ‘—-

        • #1046013

          The reference to the Extensibility library is not really necessary, but it adds more IntelliSense.

    Viewing 0 reply threads
    Reply To: add reference under vba (Word 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: