下面的代码应该对您有所帮助。只需取消注释您要编辑的页脚部分的相关代码块
Sub EditFooterText(ByVal Doc As Document, NewText As String)
Dim footerRange As Range
Dim leftTab As Long, rightTab As Long, footerLength As Long
Set footerRange = Doc.Sections(1).Footers(wdHeaderFooterPrimary).Range
With footerRange
'find the tabs
leftTab = InStr(.Text, vbTab)
rightTab = Len(.Text) - InStrRev(.Text, vbTab)
'to edit the left text
' .Collapse wdCollapseStart
' .MoveEnd wdCharacter, leftTab - 1
'to edit the centre text
' .Collapse wdCollapseStart
' .Move wdCharacter, leftTab
' .MoveEnd wdCharacter, rightTab
'to edit the right text
.Collapse wdCollapseEnd
.MoveStart wdCharacter, (0 - (rightTab - 1))
.Text = NewText
End With
End Sub
如果您从 BeforeSave 事件中调用它,您可以使用以下内容:
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
EditFooterText Doc, "my value"
End Sub
如果页脚由对齐选项卡分隔,它看起来与打开显示/隐藏的普通选项卡相同,但具有不同的字符代码,那么您可以使用以下例程:
Sub EditFooterTextWithAlignmentTabs(ByVal Doc As Document, NewText As String)
Dim footerRange As Range
Dim leftTab As Long, rightTab As Long, footerLength As Long
Set footerRange = Doc.Sections(1).Footers(wdHeaderFooterPrimary).Range
With footerRange
'find the tabs
leftTab = InStr(.Text, Chr(48))
rightTab = Len(.Text) - InStrRev(.Text, Chr(48))
Debug.Print leftTab
Debug.Print rightTab
'to edit the left text
' .Collapse wdCollapseStart
' .MoveEnd wdCharacter, leftTab - 1
'to edit the centre text
' .Collapse wdCollapseStart
' .Move wdCharacter, leftTab
' .MoveEnd wdCharacter, rightTab
'to edit the right text
.Collapse wdCollapseEnd
.MoveStart wdCharacter, (0 - (rightTab - 1))
.Text = NewText
End With
End Sub
您可以通过在即时窗口中输入?AscW(Selection.Text) 来获取所选字符的 unicode 值