none
Word 2010 AutoExec Macro wtih ActiveDocument.Name

    Question

  • I have the following AutoExec macro in a Word 2010 Startup Template.  When word starts, it looks to see if the active document is Document1. If so, it attaches a custom styles template, so our firm styles are available in every new document. Works perfect in Word 2007. any ideas why it does not work or how to fix this in 2010?

    Sub AutoExec()
        On Error Resume Next
        If ActiveDocument.NAME = "Document1" Then
        With ActiveDocument
            .UpdateStylesOnOpen = True
            .AttachedTemplate = "C:\Word2010UI\Workgroup\MUStyles.dotx"
            .XMLSchemaReferences.AutomaticValidation = True
            .XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False
        End With
        With ActiveDocument
            .UpdateStylesOnOpen = False
            .AttachedTemplate = "C:\Word2010UI\Workgroup\MUStyles.dotx"
            .XMLSchemaReferences.AutomaticValidation = True
            .XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False
        End With
        End If
    End Sub

    • Moved by Shanks Zen Friday, March 23, 2012 2:49 AM VBA issue (From:Visual Basic General)
    Wednesday, March 21, 2012 10:10 PM

Answers

  • As usual, as soon as I post I figure it out..

    Sub AutoExec()
        Application.OnTime When:=Now + TimeValue("00:00:01"), NAME:="AddDoc1Styles"
    End Sub

    Works perfect.

    Thursday, March 22, 2012 12:02 AM

All replies

  • You may have better luck getting an answer in the Visual Basic Applications forum.

    http://social.msdn.microsoft.com/Forums/en/isvvba/threads

    This is for VB.Net.

    Wednesday, March 21, 2012 11:36 PM
  • Thanks... thought that's where I was, but apparently got routed to the wrong forum somehow. 
    Wednesday, March 21, 2012 11:48 PM
  • I have the following AutoExec macro in a Word 2010 Startup Template.  When word starts, it looks to see if the active document is Document1. If so, it attaches a custom styles template, so our firm styles are available in every new document. Works perfect in Word 2007. any ideas why it does not work or how to fix this in 2010?

    Sub AutoExec()
        On Error Resume Next
        If ActiveDocument.NAME = "Document1" Then
        With ActiveDocument
            .UpdateStylesOnOpen = True
            .AttachedTemplate = "C:\Word2010UI\Workgroup\MUStyles.dotx"
            .XMLSchemaReferences.AutomaticValidation = True
            .XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False
        End With
        With ActiveDocument
            .UpdateStylesOnOpen = False
            .AttachedTemplate = "C:\Word2010UI\Workgroup\MUStyles.dotx"
            .XMLSchemaReferences.AutomaticValidation = True
            .XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False
        End With
        End If
    End Sub

    Wednesday, March 21, 2012 11:50 PM
  • As usual, as soon as I post I figure it out..

    Sub AutoExec()
        Application.OnTime When:=Now + TimeValue("00:00:01"), NAME:="AddDoc1Styles"
    End Sub

    Works perfect.

    Thursday, March 22, 2012 12:02 AM
  • Thanks for your sharing and have a nice day! :)

    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 23, 2012 8:36 AM
    Moderator
  • I had a different but similar problem: how to stop Word 2010 creating an empty "Document1" upon start ?

    After many trials, I found following simple solution:

    1) Create a new link to C:\…\Winword.exe, adding the starting option  /n  and store it on desktop and/or task bar.

    2) Replace all existing Word links ("Microsoft Office Single Image 2010" unfortunately not editable) by the previously created one.

    3) Add following procedure to normal.dotm :
          Sub Autoexec()
              Dialogs(wdDialogFileNew).Display
          End Sub
    Now the users have the choice upon starting Word to create whatever they want – any of the available templates with or without company logo, etc. Of course one could also display another, user-defined NewFile dialog.

    Sunday, January 11, 2015 2:22 PM
  • The problem is that AutoExec executes before any documents exist.  Adding a time delay will allow it time to finish starting up.  In one case I have AutoExec create a doc based on a template I created.  This works better than a time delay.  As a test call this in AuotExec

    Function IsAnyDocOpen() As Boolean
      Dim tDoc As Document
    
      IsAnyDocOpen = False
      For Each tDoc In Documents
        IsAnyDocOpen = True
        MsgBox "Doc Open: " & tDoc
      Next tDoc
    End Function
    

    Friday, January 16, 2015 6:50 PM