使用 Access VBA 添加 formatted/justified header 和页脚到 Word 文档

Use Access VBA to add formatted/justified header and footer to Word doc

是否可以使用 VBA 向 Word 文档添加 header 和页脚,如下所述?

header 应该是以下组合:

页脚应该是居中的“第 X 页,共 Y 页”(其中 X 和 Y 分别是第 # 页和第 # 页的字段)。

我很清楚如何在 Word 中手动执行此操作,但尽管有很多 Google 搜索无法使用 VBA 完全破解此问题(或真正弄清楚从哪里开始)。以下是我目前用于创建文档的访问 VBA 代码。

感谢任何解决方案或指示。

' declare vars and set up

    Dim objWord As Word.Application
    Dim doc As Word.Document
    Dim WordHeaderFooter As HeaderFooter
    Dim myDateTime As String

    ' variable to be used as Date/Time in header
    myDateTime = Format(Now(), "Long Date") & " " & Format(Now(), "Medium Time")
    
    Set objWord = CreateObject("Word.Application")

' create doc and insert sample text

    With objWord
        
        .Visible = True
    
        Set doc = .Documents.Add
        doc.SaveAs CurrentProject.Path & "\TestDoc.doc"
        
    End With
                  
    With objWord.Selection
          
        .Font.Name = "Calibri (Body)"
        .Font.Size = 12
        .TypeText "Here is an example line of text." 
      
    End With
    
    doc.Save
    doc.Activate

在 Word 中设置 headers 使文本 left/right 对齐可能很棘手,因为与 Excel 不同,没有 left/center/right headers -这是一个 header.

在下面的代码中,我使用 Tabs 'justify' header 到 New Items Report 在左边,日期在右边。

Option Explicit

Sub AddWordHeaderAndFooter()
    ' declare vars and set up

    Dim objWord As Word.Application
    Dim doc As Word.Document
    Dim rngHeader As Word.Range
    Dim rngFooter As Word.Range
    Dim myDateTime As String

    ' variable to be used as Date/Time in header
    myDateTime = Format(Now(), "Long Date") & " " & Format(Now(), "Medium Time")
    
    Set objWord = CreateObject("Word.Application")

    ' create doc and insert sample text

    With objWord
        
        .Visible = True
    
        Set doc = .Documents.Add
        doc.SaveAs CurrentProject.Path & "\TestDoc.doc"
        
    End With
                  
    With objWord.Selection
          
        .Font.Name = "Calibri (Body)"
        .Font.Size = 12
        .TypeText "Here is an example line of text."
      
    End With
    
    Set rngHeader = doc.Sections(1).Headers(wdHeaderFooterPrimary).Range
    
    With rngHeader
        With .Font
            .Bold = True
            .Size = 16
        End With
        .Text = "New Items Report"

        .Collapse wdCollapseEnd
        .MoveEnd wdCharacter, 0
        .InsertAfter vbTab
        .InsertAfter vbTab
        .InsertAfter myDateTime
 
        With .Font
            .Bold = False
            .Size = 12
        End With
    End With
    
    Set rngFooter = doc.Sections(1).Footers(wdHeaderFooterPrimary).Range
    
    With rngFooter

        .InsertAfter vbTab & "Page "
        .Fields.Add .Characters.Last, wdFieldEmpty, "PAGE", False
        .InsertAfter " of "
        .Fields.Add .Characters.Last, wdFieldEmpty, "NUMPAGES", False

    End With
            
    doc.Save
    doc.Activate
    
End Sub





如果您在 Word 中使用对齐制表符设置 headers 让文本 left/right 对齐一点也不难。如果您使用普通标签,您更容易遇到问题。

默认情况下,Word 中的 Header 和页脚样式具有与默认页边距相对应的制表位。如果您的页面布局与默认布局不同,制表位将位于错误的位置。

这可以通过使用 Word 2010 中引入的 IIRC 对齐制表符来避免。这些使您能够设置与段落缩进或页边距对齐的制表符。

Sub AddWordHeaderAndFooter()
   ' declare vars and set up

   Dim objWord As Word.Application
   Dim doc As Word.Document
   Dim rngHeader As Word.Range
   Dim rngFooter As Word.Range
   Dim myDateTime As String

   ' variable to be used as Date/Time in header
   myDateTime = Format(Now(), "Long Date") & " " & Format(Now(), "Medium Time")
    
   Set objWord = CreateObject("Word.Application")

   ' create doc and insert sample text

   With objWord
        
      .Visible = True
    
      Set doc = Documents.Add
      doc.SaveAs CurrentProject.Path & "\TestDoc.doc"
        
   End With
                  
   With doc.Content
          
      .Font.Name = "Calibri (Body)"
      .Font.Size = 12
      .Text = "Here is an example line of text."
      
   End With
    
   Set rngHeader = doc.Sections(1).Headers(wdHeaderFooterPrimary).Range
    
   With rngHeader
      With .Font
         .Bold = True
         .Size = 16
      End With
      .Text = "New Items Report"

      .Collapse wdCollapseEnd
      .InsertAlignmentTab Alignment:=wdRight, RelativeTo:=wdMargin
      With .Characters.Last
         With .Font
            .Bold = False
            .Size = 12
         End With
         .InsertAfter myDateTime
      End With
   End With
    
   Set rngFooter = doc.Sections(1).Footers(wdHeaderFooterPrimary).Range
    
   With rngFooter

      .InsertAlignmentTab Alignment:=wdCenter, RelativeTo:=wdMargin
      .InsertAfter "Page "
      .Fields.Add .Characters.Last, wdFieldEmpty, "PAGE", False
      .InsertAfter " of "
      .Fields.Add .Characters.Last, wdFieldEmpty, "NUMPAGES", False

   End With
            
   doc.Save
   doc.Activate
    
End Sub