Search code examples
vbams-word

Word VBA - How to delete TimeStamp in Footer?


How do I delete a timestamp in a word footer using VBA?

I do not know what the timestamp says. Only that there is a DD/MM/YYYY timestamp.

There is other content in the footer, besides the timestamp, that I do not want to be deleted.

The timestamp is part of a statement in one cell of the only table in the footer

I have tried editing the range of text in the footer, but it only breaks the document. I don't think you can do this on footers from my experience:

Dim footr As Word.HeaderFooter
For Each footr In ActiveDocument.Sections(1).Footers
    'With footr.Range
        If footr.Range.Text <> "" Then
            footr.Range.Delete unit:=wdCharacter, Count:=-20
        End If
    'End With
Next footr

Along the same lines, is there a way to search with wildcards in the footer? If so, that would be an easy fix. I have tried using wildcards but it didn't work on footer text.... maybe I was doing it wrong?


Solution

    • Assumes the timestamp is the only content in footers, and no table in footers
    Option Explicit
    
    Sub RemoveFooter()
        Dim oSec As Section
        For Each oSec In ActiveDocument.Sections
            oSec.Footers(wdHeaderFooterPrimary).Range.Text = ""
        Next
    End Sub
    

    Update:

    Question: The timestamp is part of a statement in one cell of the only table in the footer

    Option Explicit
    
    Sub RemoveTimeStamp()
        Dim oSec As Section, oRng As Range
        For Each oSec In ActiveDocument.Sections
            Set oRng = oSec.Footers(wdHeaderFooterPrimary).Range
            With oRng.Find
                .ClearFormatting
                .MatchWildcards = True
                .Text = "[0-9]{2}/[0-9]{2}/[0-9]{4}"
                .Replacement.ClearFormatting
                .Replacement.Text = ""
                .Execute Replace:=Word.wdReplaceAll
            End With
        Next
    End Sub