Results 1 to 7 of 7
  1. #1
    New Lounger
    Join Date
    Jan 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro switches windows (Word 2003)

    I have two macros in 2 separate documents. If I use either separately they work fine. However, if I use one, and then, leaving the first document open, use the other on a second document later, the second macro starts in the 2nd open document but then bizarrely switches to the first document where it finishes! I never noticed this happened with Word 2000, has anyone any idea what I am doing wrong?

  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: Macro switches windows (Word 2003)

    Are the macros stored in Normal dot or another global container?

    By examining what the macro does to the first document, can you pin down the line of code at which it switches documents?

  3. #3
    New Lounger
    Join Date
    Jan 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro switches windows (Word 2003)

    Both Macros are stored in other (completely seperate) containers. I have been trying to pin down at which exact point it switches documents without any success so far - it is beginning to drive me crazy! I thought it may have been that it became confused over Public Subs with the same name in both templates, but after renaming them in one template the same thing happened anyway!

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

    Re: Macro switches windows (Word 2003)

    If you use ActiveDocument a lot in your macro, assign it to a variable at the beginning of the code, and refer to the variable instead of to ActiveDocument in the rest of the code:

    <img src=/w3timages/blueline.gif width=33% height=2>

    Dim oDoc As Document
    ' assign value at beginning of code
    Set oDoc = ActiveDocument
    ...
    ' lots of intervening code
    ...
    ' refer to oDoc instead of ActiveDocument, for example
    oDoc.PrintOut '
    ...
    ' release object memory at the end
    Set oDoc = Nothing

    <img src=/w3timages/blueline.gif width=33% height=2>

    If you use the Selection object a lot, you might try using a Range object instead, but that is not always possible. Specify the window to which it belongs:

    <img src=/w3timages/blueline.gif width=33% height=2>

    Dim oWnd As Window
    ' assign value at beginning of code
    Set oWnd = ActiveWindow
    ...
    ' lots of intervening code
    ...
    ' specify the window, for example
    oWnd.Selection.InsertAfter "Text goes here."
    ...
    ' release object memory at the end
    Set oWnd = Nothing

    <img src=/w3timages/blueline.gif width=33% height=2>

  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Macro switches windows (Word 2003)

    I have a macro which is supposed to display the File Properties Dialog. In Word XP and Word 2000 dismissing this dialog toggles to the last used application (the same as an Alt-Tab). To avoid this problem I had to add other code to bring it back as shown below - could this be adopted to solve your problem.
    Sub AutoNew()
    On Error Resume Next
    Dialogs(750).Show
    ActiveDocument.Fields.Update
    AppActivate "Microsoft Word"
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    New Lounger
    Join Date
    Jan 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro switches windows (Word 2003)

    Good idea, actually I decided to choose a rather banal solution: instead I put both macros in the same document! I figured better to 'give up' than waste a lot of time over this niggly problem! Thanks for your suggestion.

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

    Re: Macro switches windows (Word 2003)

    I'm quite interested in this problem, because I did some work on collections of like-named macros in like-named modules in liked-named templates a couple of years ago.

    If you still have the original docs and can send them to me as an email attachment, I'd love to roll up my sleeves and identify the culprit. cgreaves@vif.com

Posting Permissions

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