Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts

    add reference under vba (Word 2003)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: add reference under vba (Word 2003)

    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.

  3. #3
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts

    Re: add reference under vba (Word 2003)

    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

  4. #4
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts

    Re: add reference under vba (Word 2003)

    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

    '----

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: add reference under vba (Word 2003)

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

Posting Permissions

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