簡體   English   中英

Word宏插入頁腳

[英]Word Macro to insert footer

我想運行一個Word宏,該宏將插入一個帶有文件名,日期和頁碼的頁腳。

下面的宏在文件正文中插入文件名,日期和頁碼。 如何重寫它以將其插入頁腳? 非常感謝您的幫助:)

'AddFooter宏

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "FILENAME  \p ", PreserveFormatting:=True
Selection.HomeKey Unit:=wdLine
Selection.TypeParagraph
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.TypeText Text:=vbTab
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "PAGE  \* Arabic ", PreserveFormatting:=True
Selection.TypeText Text:=vbTab
Selection.InsertDateTime DateTimeFormat:="M/d/yyyy H:mm", InsertAsField:= _
    True, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
    InsertAsFullWidth:=False
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Size = 8
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Size = 8

結束Sub`

這是我的方法:

Sub AddFooter()

Dim Filename As String
Dim Sec As Section

Filename = ThisDocument.FullName

For Each Sec In ActiveDocument.Sections
    With Sec.Footers(wdHeaderFooterPrimary)
        .Range.InsertDateTime DateTimeFormat:="M/d/yyyy H:mm", InsertAsField:= _
            True, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern
        .Range.Text = .Range.Text & Filename
        .PageNumbers.Add
    End With
Next Sec

End Sub

我嘗試了這個:

Sub AddFooterText()
Dim Filename As String

Filename = ThisDocument.FullName

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.TypeText Text:=Filename
Selection.TypeText Text:=" "
Selection.InsertDateTime DateTimeFormat:="M/d/yyyy H:mm"
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

嘗試:

Sub AddFooter()
With ActiveDocument.Sections.First.Footers(wdHeaderFooterPrimary).Range
  .Text = vbTab
  .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
  .InsertAfter vbTab
  .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="DATE \@ ""M/D/YYYY H:mm""", PreserveFormatting:=False
  .InsertAfter vbCr
  .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="FILENAME  \p", PreserveFormatting:=False
End With
End Sub

注意1:要保留任何現有內容,請將“ .Text = vbTab”更改為“ .InsertAfter vbTab”。

注意2:為防止以后更改日期,請更換:

.InsertAfter vbTab
.Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="DATE \@ ""M/D/YYYY H:mm""", PreserveFormatting:=False
.InsertAfter vbCr

與:

.InsertAfter vbTab & Format(Now,"M/D/YYYY H:mm") & vbCr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM