Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Elkins Park, Pennsylvania, USA
    Thanked 0 Times in 0 Posts

    Word code jumping the gun? (Word XP)

    I have written a very lengthy piece of code that users run to "clean" (read: edit) certain phrases in their documents. As it does each occurrance, it's logging the edits edit in a basic text file that is opened with the Open... For... command. So far so good...

    At the end of the edit process, my code opens the TXT file, selects the delimited text in it, removes all soft returns (among other things) and converts it to a table. This is done with these lines of code:

    Close 'close any open log files
    Documents.Open DesktopPath & "" & LogFile

    <font color=blue>LogFile</font color=blue> is the string variable assigned to the name of the log that was created with the Open... for... command, and <font color=blue>DesktopPath</font color=blue> is a string-function that returns the pathname to the individual user's desktop using the following code:
    uName = Environ("USERNAME") ' Get environment variable.
    DesktopPath = "Cocuments and Settings" & Trim(uName) & "desktop"

    (EndCleanCitationRoutine is the subroutine that selects and converts the delimited text in the file and formats it.)

    Here's where it gets hinky: sometimes the code doesn't wait for the file to completely open!! Instead, it moves on to the EndCleanCitationRoutine and starts selecting and formatting the text in the user's original document! I've tried to make sure that the active document is the correct one by putting this code at the beginning of the EndCleanCitationRoutine:

    If ActiveDocument.FullName <> DesktopPath & "" & LogFile Then
    Do Until ActiveDocument.FullName = DesktopPath & "" & LogFile
    StatusBar = "/"
    StatusBar = "-"
    StatusBar = ""
    StatusBar = "|"
    End If
    Set LogDoc = ActiveDocument

    Of course, if I STEP through the code, everything behaves properly and the variables are all correct (i.e. it "knows" what the log file is and what the user's document is), but of course the problem only happens when users run it in real time. And to compound the confusion, it doesn't always happen even with the same document.

    Can anybody shed any light on this, or give me some ideas? It's REALLY bumming me out!!
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Thanked 31 Times in 31 Posts

    Re: Word code jumping the gun? (Word XP)

    Although I'm not 100 % certain, I suspect that the mixed single document interface/multi document interface introduced in Word 2000 causes VBA to become confused sometimes about what the ActiveDocument is. It is better to define Document objects and work with those, instead of using ActiveDocument.

    Dim oDoc As Document
    Set oDoc = Documents.Open(DesktopPath & "" & LogFile)
    EndCleanCitationRoutine oDoc

    The EndCleanCitationRoutine would need an argument:

    Sub EndCleanCitationRoutine(oDoc As Dcoument)

    In the code for EndCleanCitationRoutine, use oDoc instead of ActiveDocument, and if possible avoid assuming that oDoc is the active document.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Silicon Valley, USA
    Thanked 94 Times in 90 Posts

    Re: Word code jumping the gun? (Word XP)

    The method suggested by Hans is best, but if you still have a timing problem, let us know.

    I love the StatusBar code. Must try that some time.

  4. #4
    Star Lounger
    Join Date
    Jan 2001
    U.S. - East Coast
    Thanked 0 Times in 0 Posts

    Re: Word code jumping the gun? (Word XP)

    Yes, it is a cute trick. But, as written, even moderately fast cpu's would seemingly display only a vertical bar due to the lightning-fast cycling of the shapes. There would have to be substantial loops of combinations of DoEvents and Application.ScreenUpdating to achieve the desired result. An alternative method is to display a series of characters, i.e periods, that builds horizontally so that the user can see the "progress".

    Still, <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> to Mesaka for thinking "outside the bar"!

Posting Permissions

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