Share via

Getting Word Template Initialization Code to run on Document_New and Document_Open

Anonymous
2015-09-29T20:23:32+00:00

Microsoft Office Word 2011 version 14.5.5

I reviewed and tried both of these threads which refer to version 14.4.9 and an earlier version

http://answers.microsoft.com/en-us/mac/forum/macoffice2011-macword/my-macro-to-run-when-a-new-file-is-created-only/3940ad72-eec5-41d2-b7b9-94b68c36aec2

  • adding a one second delay in Auto_New

Sub AutoNew()

    Dim PauseTime, Start

'    Add a pause to allow time for Word to open before continuing

    PauseTime = 1  ' Set duration in seconds

    Start = Timer   ' Set start time.

    Do While Timer < Start + PauseTime

        DoEvents   ' Yield to other processes.

    Loop

End Sub

http://answers.microsoft.com/en-us/mac/forum/macoffice2011-macword/mac-word-documentnew/cf113d91-7709-44ce-96f7-2871eefbd119

  • adding HavePatience in Auto_New and Auto_Open which checks Application.Documents.Count < 1

Sub AutoNew()

HavePatience

End Sub

Sub AutoOpen()

HavePatience

End Sub

Sub HavePatience()

If StopCount > 9 Then

MsgBox "Sorry, your document failed to open."

StopCount = 0

End sub

End If

StopCount = StopCount + 1

weThereYet = Application.Documents.Count

If weThereYet < 1 Then

Application.OnTime Now + TimeValue("00:00:01"), "HavePatience"

end if

End Sub

Neither solution causes Document_New or Document_Open to be called consistently. In about 30 tries, I saw my MsgBox debug code once. I have resorted to checking one of my arrays to determine if the initialization code was run before running commands on my toolbar. I have also tried calling Sleep 1000 with no luck. Is one second too short or has someone found another workaround? Being able to distinguish between new and open would be useful.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2015-10-08T18:59:23+00:00

Change "Auto_New" to "AutoNew" and try it again. "Auto_New" is the subroutine's name in PowerPoint. This is the way I wrote and published the routine originally in 2011 for working in the ThisDocument class of Word 2011 VBA.

Dim weThereYet As Integer, StopCount As Integer

Sub AutoNew()

    HavePatience

End Sub

Sub AutoOpen()

    HavePatience

End Sub

Sub HavePatience()

    If StopCount > 9 Then

        MsgBox "Sorry, your document failed to open."

            StopCount = 0

            End

    End If

    StopCount = StopCount + 1

    weThereYet = Application.Documents.Count

    If weThereYet < 1 Then Application.OnTime _

        Now + TimeValue("00:00:01"), "HavePatience"

End Sub

Was this answer helpful?

0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-09-30T08:16:53+00:00

    The one I always use is 

    Application.OnTime Now + TimeValue("00:00:01"), "HavePatience"

    Yes, 1 second is probably too short.  You need to wait for the document to load then its template to load before the VBA gets compiled.  Try 2 seconds.

    MacWord's "Events" are generally known to be less than reliable.  You might like to re-code to avoid using events.

    Hope this helps

    Was this answer helpful?

    0 comments No comments
  2. John Korchok 232.8K Reputation points Volunteer Moderator
    2015-09-29T23:52:44+00:00

    While the particulars of your situation are doubtless very familiar to you, we know nothing about the problem you're trying to solve. A little background would be helpful. What problem is caused by calling myform.show in Document_New? And why would you think that AutoNew will do something different than Document_New? Why do you think you need a time delay?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-09-29T22:53:57+00:00

    Both of the aforementioned threads indicated that the reason, Document_New and Document_Open were not being called was due to a timing issue. They both suggested adding delay tactics into Auto_New and Auto_Open to ensure that the Document_* subroutines would get called.

    As a workaround, I have been forced to check if my initialization code was run (and run it if it wasn't) each time I run a command on my toolbar.

    What do others use to start up a form, for example, in a macro enabled template if you cannot call myform.show in Document_New?

    Was this answer helpful?

    0 comments No comments
  4. John Korchok 232.8K Reputation points Volunteer Moderator
    2015-09-29T21:33:41+00:00

    While I'm not clear what you're trying to accomplish, you also might try this sub:

    Sub Delay(Length As Long)

      Dim ThisTime As Long, EndTime As Long

      ThisTime = Now

      EndTime = Now + Length

      Do Until ThisTime = EndTime

        DoEvents

        ThisTime = Now

      Loop

    End Sub

    Then call it with

    Delay (0.1)

    to get about a 1 second delay.

    Was this answer helpful?

    0 comments No comments