none
Change property on every document via global startup script - global event procedures RRS feed

  • Question

  • Hello - This is what I want to do: create global event procedures similar to AutoOpen and AutoNew without using Normal.dot. I plan on implementing that change via a template stored in the Startup folder. I already have a logon script ready to place the startup template on computers at work, now I need the VB code for the template.

    I've done a lot of looking, but so far I don't understand how to make this work for Office 2010. I do have these bits for reference: http://word.mvps.org/FAQs/MacrosVBA/PseudoAutoMacros.htm and http://msdn.microsoft.com/en-us/library/office/ff821398.aspx and http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

    Modification to make on every document created or opened: UpdateStylesOnOpen = False

    My assumption is the way to make this happen is to use an event procedure that runs when opening or creating a new document. Unfortunately I have failed in my attempts to do this. I just don't know enough about writing scripts for Word. Every time I try to follow the instructions on the mvps.org site my test script will not compile (Compile error: only valid in object module, WithEvents is hilighted). Maybe the info is out-of-date for Office 2010?

    Module ThisApplication:

    Option Explicit
    Public WithEvents oApp As Word.Application

    Module Module1:

    Option Explicit
    Dim oAppClass As New ThisApplication
    Public Sub AutoExec()
        Set oAppClass.oApp = Word.Application
    End Sub

    Once I get a little further I assume I'll need to integrate someting like this:

    Private Sub oApp_NewDocument(ByVal Doc As Document)
        ActiveDocument.UpdateStylesOnOpen = False
        MsgBox "Trying to change new document."
    End Sub
    Private Sub oApp_DocumentOpen(ByVal Doc As Document)
        ActiveDocument.UpdateStylesOnOpen = False
        MsgBox "Trying to change existing document."
    End Sub

    TIA
    Monday, September 24, 2012 3:59 PM

Answers

  • Hi TIA,

    Thanks for posting in the MSDN Forum.

    Based on your issue, I have reproduced the procedure in http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm with my Word 2010 and there is no problem.Did you insert the code Public WithEvents oApp As Word.Application into a Class Module? A standard code module is not an object module. I think you need to use a Class module, otherwise the Compile error: only valid in object module will come up. I would suggest you have a try again with the Word 2010 VB Editor.

    Hope it helps.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by stumppc Tuesday, September 25, 2012 6:48 PM
    Tuesday, September 25, 2012 3:16 AM
    Moderator

All replies

  • Hi TIA,

    Thanks for posting in the MSDN Forum.

    Based on your issue, I have reproduced the procedure in http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm with my Word 2010 and there is no problem.Did you insert the code Public WithEvents oApp As Word.Application into a Class Module? A standard code module is not an object module. I think you need to use a Class module, otherwise the Compile error: only valid in object module will come up. I would suggest you have a try again with the Word 2010 VB Editor.

    Hope it helps.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by stumppc Tuesday, September 25, 2012 6:48 PM
    Tuesday, September 25, 2012 3:16 AM
    Moderator
  • Thanks...My script works very well now. Apparently no matter how many times I read the instructions I missed that I was to create a class module instead of a module.

    James

    Tuesday, September 25, 2012 6:51 PM