Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Jun 2004
    Location
    Gtr London, England
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Delay code (Office 2003)

    Can anyone help? I'm sure it is a simple fix !!!

    The code I have is, on occasion, running too quickly and functions are not completing correctly. If I put a breakpoint in at the correct place, then press F5 as soon as it stops it all runs well. So I'm thinking I need to just make sure this piece of code is completed before moving on. Can I put a type of "wait until this is done" in the code at this point?

    Many thanks
    Jack

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

    Re: Delay code (Office 2003)

    Perhaps inserting a simple DoEvents in the right place will solve the problem. This will give other processes a chance.

    Alternatively, you can use Application.Wait (available in Excel VBA):

    Application.Wait Now + TimeValue("0:00:02")

    Or you can split your code into two macros and use Application.OnTime (available in Word and Excel VBA) to call the second one from the first:

    Sub Macro1()
    ...
    Application.OnTime Now + TimeValue("0:00:03"), "Macro2"
    End Sub

  3. #3
    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: Delay code (Office 2003)

    Usually VBA doesn't outrun itself... Are you launching an external process using Shell or a Windows API function? There are ways to watch and see whether it has finished.

  4. #4
    Star Lounger
    Join Date
    Jun 2004
    Location
    Gtr London, England
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delay code (Office 2003)

    I'm not using an external process - if I put a stop in the code, then F5 at the stop, it runs fine !!! For your information I am manipulating a commandbar. I am using "Document Change", due to the fact that this is the only event that works for opening documents in Word and through Windows Explorer. "Document Change" is obviously running every time I move around the application. On "Document Change" I am searching the commandbars to see if my commandbar is in the commandbar collection. I'm returning true or false from the function, but for some reason, in three of the ten templates, it is returning false eventhough the commandbar exists, so it is then building another commandbar. So my thinking was to stop the code until this had run (DoEvents) - which in this case is not solving the problem. (This is a nightmare !!!)

    To throw yet another spanner in the works - when a document runs a set of styles specific to that document is populated in the commandbar. When I run the first document, then run the second document - from the second document I am not able to access the styles in the first. If I then close both documents and create them again, I am able to access all the styles in the second document. It's all very confusing - mainly because I can't trap it, as soon as I put a stop in I don't get the problem.

    Any ideas would be greatly appreciated.
    Jack

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

    Re: Delay code (Office 2003)

    When manipulating styles and command bars, you must keep track of the customization context. In general, a style or command bar stored in a document will only be available to that document, and a style or command bar stored in a (non-global) template will only be available to documents based on that template. If you want a commandbar to be available in all documents, it must be stored in Normal.dot or in a global template.

    You can retrieve or set the customization context in VBA through CustomizationContext, e.g.

    CustomizationContext = ActiveDocument

    or

    CustomizationContext = ActiveDocument.AttachedTemplate

  6. #6
    Star Lounger
    Join Date
    Jun 2004
    Location
    Gtr London, England
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delay code (Office 2003)

    Hi Hans

    I have set the CustomizationContext in the global template, but am passing in the template:

    From the Memo Template (calling the styles routine):
    IdentifyStyles docThisDocument, docDocument, c_strMemo, ctlMemo

    In the Global Template:
    CustomizationContext = docThisDocument

    When the code is running and I check "docThisDocument" it is passing in the Memo.dot template. Could this be causing the problem?

    Regards
    Jack

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

    Re: Delay code (Office 2003)

    Depends on what the active document is when running this code. If the active document does not have Memo.dot as attached template, the command bars from Memo.dot will not be available.

  8. #8
    Star Lounger
    Join Date
    Jun 2004
    Location
    Gtr London, England
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delay code (Office 2003)

    The activedocument also gets passed in, together with the template.

    Regards
    Jack

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

    Re: Delay code (Office 2003)

    The reason I mentioned this, is that starting with Word 2002 (I believe), Word has a nasty habit of changing the active document to something you didn't expect.

    I think it will be very hard to solve this problem without seeing the actual templates and code, but it will probably not be feasible to post them...

Posting Permissions

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