none
Using ActiveDocument stops macro 1st time it's run RRS feed

  • Question

  • Hi,

    I am trying to run a macro in Word 2003, and I've isolated the problem down to the use of ActiveDocument as in the example below.

     

     Sub test()
     MsgBox "Got here"
     MsgBox ActiveDocument.Name
    End Sub
    

    The very first time the macro is run after opening the document, it runs but stops once the "Got here" box has been closed. If I run it again, it works correctly. Also stepping into when first opening the doc works. I've added error handling but it doesn't catch anything.

    Obviously, something so simple should work! So I tried this with a new, clean document and it works fine all the time. One difference between the original doc and the new doc is that the original is based on a template. Could a template affect ActiveDocument?

    Any other reasons why this doesn't work?

    Thanks

    Andria


    Thursday, April 7, 2011 1:54 PM

Answers

  • Hi Cindy,

    I found that if the macro is in the document rather than the template it all runs fine. In doing so, I found that the security settings were such that the macros was blocked. After I changed the security settings, it doesn't matter where the macro is, it all runs fine. So it was just a matter of security settings but no messages to say this.

    Thanks for all your help,

    Andria

     

     

    Monday, April 11, 2011 7:26 AM

All replies

  • Hi Andria

    By "stops" do you mean it throws up an error message? If yes, what does the message say?

    In which project is the macro stored? In Normal? In the template? In the document itself?

    You say the problem only occurs when the document is linked to a particular template. does this template have macro code? Perhaps macros that execute when a document linked to it is opened?


    Cindy Meister, VSTO/Word MVP
    Thursday, April 7, 2011 1:59 PM
    Moderator
  • Hi Cindy,

    When I say stops, it just returns to the document, no messages at all.

    The macro is in the document itself.

    I don't think the template has any macro code, but I'm not sure how to see this - sorry, not very good with word templates!

    Thanks

    Andria

    Thursday, April 7, 2011 2:54 PM
  • Just noticed that the template does have a macro:

    Sub UpdateAll()
    Dim oStory As Range
    Dim oField As Field
    On Error Resume Next
    For Each oStory In ActiveDocument.StoryRanges
    For Each oField In oStory.Fields
    oField.Update
    Next oField
    Next oStory
    On Error GoTo 0
    End Sub


    Thursday, April 7, 2011 3:28 PM
  • Hi Andria

    Glad you've found the way into the template. Can you find a macro named AutoOpen or is there one named Document_Open in the ThisDocument module? If yes, could you show us the content?

    For it to be interfering with your macro it almost has to be something executing when the document is opened...


    Cindy Meister, VSTO/Word MVP
    Friday, April 8, 2011 6:56 AM
    Moderator
  • Hi Cindy,

    Well, I can't  see how to get to the template, but I can see that the UpdateAll macro is shown from the Tools->Macro->Macros... dialog box.

    However, there are just two macros, mine & the UpdateAll one I posted above. Once thing I noticed is that I added my macro to the template rather than the document. This wasn't intentional, just what happens when you create a new macro I think.

    Sorry, but I can't see anything about modules in anything to do with macros. Is this a feature of later versions of Word?

    Thanks

    Andria

    Friday, April 8, 2011 9:09 AM
  • Hi Andria

    <<Well, I can't  see how to get to the template...>>

    You copied the code of the UpdateAll method into a previous reply, so you are able to access the template.

    Modules are the contents of the folders listed in the "Project" window in the VBA editor. That's where you see "Normal", for instance.

    "New Macros" is a module, for example, and is where any recorded macros will be saved. 

    Every Project (top-level) has a folder "Microsoft Word Objects". click the +-sign next to that to see the "ThisDocument" module.


    Cindy Meister, VSTO/Word MVP
    Friday, April 8, 2011 10:12 AM
    Moderator
  • Hi Cindy,

    Ok I see where the modules are now - thanks for the help!

    The ThisDocument modules for both the document and the template are empty.

    Thanks

    Andria

    Friday, April 8, 2011 10:16 AM
  • I got my husband to put an example file at http://www.box.net/shared/u6oj3inbya

    I hope that helps!

    Andria

    Friday, April 8, 2011 10:51 AM
  • Hi Andria

    Well, I downloaded the document, opened it, and found only the one macro in it - the one to update. The test macro with which you're having difficulties was not in the document.

    I copied it from this message, pasted it under the other macro, saved, closed, opened the document and immediately ran the test macro. It worked just fine. I did test in Word 2007 as I no longer have 2003 installed.

    You can try doing what I did and see if that makes a difference, although I suspect not.

    After that, try starting Word in "Safe Mode" by holding down the CTRL key when you start it. Then open this test document with the two macros and see if it works any differently. If yes, then the problem is something in your Word setup...


    Cindy Meister, VSTO/Word MVP
    Sunday, April 10, 2011 5:58 AM
    Moderator
  • Hi Cindy,

    I found that if the macro is in the document rather than the template it all runs fine. In doing so, I found that the security settings were such that the macros was blocked. After I changed the security settings, it doesn't matter where the macro is, it all runs fine. So it was just a matter of security settings but no messages to say this.

    Thanks for all your help,

    Andria

     

     

    Monday, April 11, 2011 7:26 AM