Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Word : Adding and Removing References

    I am trying to manipulate references to a dll file. I can easily add the reference:

    Application.VBE.ActiveVBProject.References.AddFrom File PATH & LAWBASE_DLL

    but am having great difficulty removing the reference:

    Application.VBE.ActiveVBProject.References.Remove (PATH & LAWBASE_DLL)

    The remove statement is giving me a type mismatch error and I am not sure where to begin. The Help file for Remove states:
    syntax: object.Remove(component), where component =
    "For the References collection, a reference to a type library or a project."

    Any ideas?

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    Karen,

    Check out <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Cat=&Board=vb&Number=19184&page=&v iew=&sb=&vc=1#Post19184>this thread</A>
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Star Lounger
    Join Date
    Jan 2001
    Location
    L.A., California
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    Karen,

    You have to remove the reference by referring to it as an object. You also have to know the name of the reference. Not the file name, as when adding the reference, but the name you would see in the Tools|References list.

    You have to loop through the references collection and remove the reference when you recognize the correct one. As with all collections, you should loop through in reverse order, because if you remove a member, all the higher indexes get re-set. Here's a sample procedure to do this:

    Sub RemoveReference(RefToRemove As String)

    Dim iRefID As Integer

    With ActiveDocument.VBProject

    iRefID = .References.Count
    Do While iRefID > 0

    If .References(iRefID).Name = RefToRemove Then
    .References.Remove .References(iRefID)
    End If

    iRefID = iRefID - 1
    Loop

    End Sub

    You can put this subroutine in your Normal template (although that's not recommended!) or any other "global" template you desire, or even in the document you are trying to remove the reference from. Wherever you put this subroutine, you should give that document or template a reference to the VBA Extensibility Library. As you can see, there are many ways to refer to things in VBA. You used the Application.VBE.ActiveVBProject object, and I used the ActiveDocument.VBProject object. Either works fine.

    Feel free to e-mail questions.

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    Hi Ron,
    Thank you very much for your reply. I've been off work so am only now just trying it out.

    My first question - I have heard of the VBA Extensibility Library but cannot see anything named like that in the list of available references. Where do I source it from?

    Second - and this may be because I haven't got the Extensibility Library referenced but I get an error message in the line:
    .References.Remove.References(iRefID)
    VB highlights the .Remove and gives me the error "Argument Not Optional"

    I am trying to do this with Word 97, but will probably also need it for Word 2000.

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    Found the extensibility library - still getting that error message though.

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    I've got this far. The first procedure simply sets the name of the reference - this is how it appears in the list of references under Tools | References.

    Sub UnloadReference()
    Call RemoveReference("LAWbase Document Management 3.53")
    End Sub

    This procedure is your one with a slight modification - I've modified the line in the If statement that removes the reference and now I get a type mismatch error - with the iRefID highlighted.

    Sub RemoveReference(RefToRemove As String)
    Dim iRefID As Integer

    With ActiveDocument.VBProject
    iRefID = .References.Count
    MsgBox .References(iRefID).Name
    Do While iRefID > 0
    If .References(iRefID).Name = RefToRemove Then
    .References.Remove iRefID

    End If
    iRefID = iRefID - 1
    Loop

    End With

    End Sub

    Interestingly, if I put in a message box as below the name is displayed as "lawbase" not the actual name that appears in the Tools | References list.

    With ActiveDocument.VBProject
    iRefID = .References.Count
    MsgBox .References(iRefID).Name
    End With


    The following procedure works, but the reference may not always be the eighth one, hence I'm trying to get your code above to work.

    Sub Remove_Reference()

    Dim x As Object

    Set x = Application.VBE.ActiveVBProject .References.Item(8)

    Application.VBE.ActiveVBProject.References.Remove x

    End Sub

    I seem to be going around in circles, and I can't grasp what's happening.

  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Word : Adding and Removing References

    Karen,

    Here's the code jscher posted in the thread which Geoff referred to earlier - this definitely did work:

    <pre>Sub NukeReference()
    Dim aRef As Reference, colRefs As References
    Set colRefs = VBE.ActiveVBProject.References
    For Each aRef In colRefs
    'Debug.Print aRef.Name & "; " & aRef.FullPath & "; " & aRef.Type
    If InStr(1, aRef.FullPath, "palmapp", vbTextCompare) > 0 Then
    colRefs.Remove aRef
    End If
    Next
    End Sub</pre>

    It seems that this method gives you a bit more flexibility in two related ways:

    - By looping through the references collection by means of For Each/Next, you don't have to worry about matching the index or name of the item.
    - Using the InStr function gives you more flexibility in finding a match - in your case

    If InStr(1, aRef.FullPath, "lawbase", vbTextCompare) > 0

    might do what you need.

    Hope this helps,
    Gary

  8. #8
    Star Lounger
    Join Date
    Jan 2001
    Location
    L.A., California
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    You need to say:

    .References.Remove .References(iRefID)

    The Remove method expects a Reference object, not an integer which is simply the object's index in the collection. That is why you got a type mismatch error. I don't know why your later example works, as it only supplies an integer.

    The code sample in the prior reply will also work. I use the For Each Object In Collection technique also. In fact, I started out my project using that technique. I don't remember exactly when I changed to using an index-based technique. For some reason I was getting an error of some type and was able to solve it by switching to an index instead of the For Each method.

    As far as the name problem, I'm not sure exactly what's going on there. In my experience, if you supply a name argument equivalent to the name you see in the Tools|References list, it should work. Could there be a slight difference between the two? Please advise.

  9. #9
    Star Lounger
    Join Date
    Jan 2001
    Location
    L.A., California
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    Oops, I guess I mis-read your example. Of course it works, because you're sending it an object, which just happens to be the eighth and final item in the collection.

    The whole purpose of looping through the collection is because you DON'T know the index of the item you want to remove.

    If I understand your message correctly, you say that the Name property of the particular reference does NOT match the name that displays in the Tools|References list??? That's bizarre to me, as it has always agreed in my experience. That just means that you have to do some sleuthing to find out the true name (which it appears you already did when you put up that message box).

  10. #10
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    <img src=/S/yep.gif border=0 alt=yep width=15 height=15>Finally it makes sense. The name of the reference is lawbase, but I needed the instr function to look for part of the actual filename - lbdm353!

    IT WORKS, IT WORKS, IT WORKS

    Now I can try to fit it all together and hopefully get a solution to my problem!

    Thank you Gary and Ron.

  11. #11
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Christchurch, New Zealand
    Posts
    250
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    Hi Ron,

    Thanks for your help - still couldn't get your code to work but I did manage to get a workable solution out of the code that Gary referred me to.

    There was definitely a difference in the reference name = "lawbase" being the actual name, and "LAWBase Document Management 3.53" being the visible name in the Tools References list, and LBDM353.dll being the actual filename. I don't really understand dlls so I'm not even going to wonder why...

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    Before I add a generic NukeReference(web.dot) to all procedures, I wondered if you could tell me WHY you needed to drop references.

    My problem is possibly insoluble: I could Nuke the refernce once the document is open, but VBA clags out on the Documents.Open command! My only thought right now is to trap the rror with an On Error, which i hate, and then Nuke references and try again to open the document. That doesn't make sense at all!


    I have some documents that have picked up references, opening the document by VBA generates a 5180 error. I've no idea why there is a reference, why it is needed, or, for that matter, what to do with references.




    "Run-time error '5180': Word cannot open this document template. (D:GREAVESADMINWEB2001CGISWEB.DOT)"

    on this line
    <pre> Documents.Open FileName:=strFile
    </pre>


    The document is open by the time I choose Debug (End/Debug/Continue choices), and the file web.dot is right there in the references along with a reference to Normal.dot.

    The template exists and is, in fact, the attached template.

    I have the code that Gary posted (Post: 30593 re: 30572 from KarenWright).

  13. #13
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Word : Adding and Removing References

    Chris,

    If the reference you need to remove is the reference to the attached template, then there's a more direct way to do it, which is to change the attached template to Normal, as in:

    <pre> With ActiveDocument
    .UpdateStylesOnOpen = False
    .AttachedTemplate = "D:WordData97TemplatesNormal.dot"
    End With
    </pre>


    But even this is not going to help if you're getting an error message as soon as the document opens (and tries to locate its attached template) - not sure how to get past that one, except that the root of the problem seems like some kind of network/file access issue.

    Gary

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word : Adding and Removing References

    Gary, I agree that this is more elegant if that is the problem, but it's not, so it isn't (grin!).

    It ought not to be a network issue, unlessIi installed a network in my sleep last weekend. I suspect it has something to do with the following scenario:

    Monday: Attached greavesweb.dot to document, saved document
    Tuesday: Moved greavesweb.dot from greaves to pers
    Wednesday: Opened document; Word discovers greavesweb.dot no longer exists, slides web.dot into references list, attaches Normal.dot (aka "sweeping it under the carpet)
    Thursday: I realise the error of my ways and moce persweb.dot back from pers to greaves
    Friday: All hell breaks loose.



    I am, of course, stupid enough to log off now and see if I can re-generate the problem this way. If I am successful, all that will do is confirm that that's what caused this problem.

    It won't, of course, go very far in letting me program around the problem for other users of my software.

    All the more annoying since quite obviously Word is able to open the document. It just gets confused by what it didn't ought to have done on Wednesday.


    Sigh.

  15. #15
    JustCallMeAl
    Guest

    Re: Word : Adding and Removing References

    Does this help at all?

    <pre> With Me.VBProject.References
    If .Item("MyProcedure").IsBroken Then
    .Remove .Item("MyProcedure")
    End If
    End With
    </pre>


Page 1 of 2 12 LastLast

Posting Permissions

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