excelvbams-wordpowerpointtiming

Word VBA - Force Document to Load New Timestamp before Macro executes


I have a timing issue in Word VBA. I am trying to open and update thousands of documents, each one with 2 timestamps in the footer that update upon opening to the date and time the document was recently opened.

My macro has code to remove these timestamps. This code works, but I am running into a problem: the timestamps do not have enough time to load in before the macro executes when looping through hundreds of documents, so they are not removed.

The function to open and close the documents is this:

Sub ProcessFile(ByVal f As String, ByVal m As String)
    Dim doc As Document
    Documents.Open FileName:=f, AddToRecentFiles:=False
    ActiveDocument.Repaginate
    Application.Run m
    ActiveDocument.Close SaveChanges:=True
    Set doc = Nothing
End Sub

The code I have put in my macro that attempts to force the document to load is this:

WaitUntil = Now() + TimeValue("00:00:05")
Do While Now < WaitUntil
    DoEvents
Loop

Which does not work. I just need a way to give the document a certain amount of time to load its content in before running the macro


Solution

  • I guess it is for the same project as your last post.

    OR

    Note: please backup your documents before testing.

    Sub DeleteDateFields()
        Dim section As section
        Dim footer As Range
        Dim field As field
        For Each section In ActiveDocument.Sections
            Set footer = section.Footers(wdHeaderFooterPrimary).Range
            For Each field In footer.Fields
                If field.Type = wdFieldDate Then
                    field.Delete
                    MsgBox "Found and deleted a date field in the footer."
                End If
            Next field
        Next section
    End Sub