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

    references - AddFromFile (Word97/SR2)

    This post is the source of my little essay on testing, setting and clearing references to projects automatically. I have attached a short BAS mdule that functions in Word97SR2.

    The relevant code is
    <pre> blnResult = Refs.blnReference(strRef)
    If blnResult Then
    Else
    Dim colRefs As References
    Set colRefs = VBE.ActiveVBProject.References
    colRefs.AddFromFile (strRef)
    blnResult = Refs.blnReference(strRef)
    End If
    </pre>


    I've run into this problem before, but can't find/remember the answer.

    If I run the code without interruption, it returns FALSE, as if the .AddFromFile hadn't had time to complete its housework before the test (blnReference) took place.

    If I single-step through the code, I get the omnipresent "can't enter break mode at this time"; choosing Continue (hence a time delay) returns TRUE!

    Inserting DoEvents doesn't solve the problem:
    <pre> colRefs.AddFromFile (strRef)
    DoEvents
    blnResult = Refs.blnReference(strRef)
    </pre>


    (later) This SUB demonstrates the problem more clearly, i hope:
    <pre>Sub InToto()
    MsgBox blnFileExists(strcLibrary) ' TRUE Generally you will find this.
    MsgBox blnClearReference(strcLibrary) ' FALSE, (after the first run through here) it should be gone
    MsgBox blnReference(strcLibrary) ' FALSE, it is gone.
    MsgBox blnAddReference(strcLibrary) ' TRUE, it can be added ** FAILS**
    MsgBox blnClearReference(strcLibrary) ' TRUE, it has been added
    MsgBox blnReference(strcLibrary) ' FALSE, it is gone
    End Sub
    </pre>


    > after an hour I want to shoot myself in the head over this one

    Jefferson, when you're done with it, can I borrow the gun? (grin!)

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: references - AddFromFile (Word97/SR2)

    Please don't lead me back into this hell... well, after another 15 minutes here, I have concluded that neither DoEvents or the API Sleep function can help because the thing we need to happen is in the VBE's thread(s) and not outside it. I can't think of a solution to the timing problem.

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

    Re: references - AddFromFile (Word97/SR2)

    >I have concluded that neither DoEvents or the API Sleep function can help

    Jefferson:

    Thanks for this. Not knowing that there isn't a solution is crippling; coming to believe that there isn't a solution is some kind of freedom.

    It helps me tremendously to know that YOU haven't been able to come up with a quick fix, or any form of solution.

    The whole thing is annoying, because when I'm doing outside-stuff (as in setting references), it seems reasonable to check that the references have, indeed been set.

    Obviously if I hadn't thought to check them after setting, I wouldn't know that I had a problem.
    Maybe I don't have a problem.
    Maybe by the time I get to use the references, scant microseconds later, the dust will have settled.

    The Gun?


    Everyone else:

    Here's your chance to beat Jefferson out of first place in my estimation (grin)

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

    Re: references - AddFromFile (Word97/SR2)

    Chris,

    I tried your code in Word 97 SR2 and in Word 2002 SP-2, but with a different file name in strcLibrary. It succeeded in both cases <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

    Does it help in your case to use Application.OnTime to check whether the reference has been added? Look up OnTime in the Word VBA help to learn about it. The code could be something like:

    Application.OnTime When:=Now + TimeValue("00:00:05"), Name:="TESTblnReference"

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

    Re: references - AddFromFile (Word97/SR2)

    > I tried your code in Word 97 SR2 and in Word 2002 SP-2 ... It succeeded in both cases

    Darn! That suggests that it is something weird on my system.

    > Does it help in your case to use Application.OnTime to check whether the reference has been added?

    Not really. OnTime can run a macro, but my function procedure "blnAddReference" needs to let me know that it has succeeded.
    That is, blnAddReference needs to return a result, and calling a macro doesn't help. Adding a delay of one second or more, while it might solve the problem, seems weird. I hate introducing un-necessary delays. (I know, this delay is "necessary").

    However you got me to wondering - if I replaced the call to the cute little function with the inline code - mebbe that would introduce JUST ENOUGH delay - but no! The code below fails for the same reason as the original:
    <pre> Dim colRefs As References
    Set colRefs = VBE.ActiveVBProject.References
    colRefs.AddFromFile (strRef)
    ' Application.OnTime When:=Now + TimeValue("00:00:05"), Name:="TESTblnReference"
    ' blnResult = Refs.blnReference(strRef)
    Dim aRef As Reference
    Set colRefs = VBE.ActiveVBProject.References
    For Each aRef In colRefs
    If InStr(1, aRef.FullPath, strRef, vbTextCompare) > 0 Then
    blnAddReference = True ' success
    Exit Function
    End If
    Next
    </pre>


Posting Permissions

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