【问题标题】:Word Macro to insert footerWord 宏插入页脚
【发布时间】:2017-11-09 14:56:40
【问题描述】:

我希望运行一个 Word 宏,它插入一个带有文件名、日期和页码的页脚。

以下宏在文档正文中插入文件名、日期和页码。如何重写它以将其插入页脚?非常感谢您的帮助:)

'添加页脚宏

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

结束子`

【问题讨论】:

    标签: vba ms-word formatting


    【解决方案1】:

    我就是这样做的:

    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
    

    【讨论】:

      【解决方案2】:

      我试过了:

      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
      

      【讨论】:

        【解决方案3】:

        试试:

        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
        

        【讨论】:

          猜你喜欢
          • 2017-06-12
          • 1970-01-01
          • 2021-03-22
          • 1970-01-01
          • 2017-08-22
          • 2017-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多