【问题标题】:Use Access VBA to add formatted/justified header and footer to Word doc使用 Access VBA 将格式化/对齐的页眉和页脚添加到 Word doc
【发布时间】:2021-08-03 23:09:01
【问题描述】:

有没有一种方法可以使用 VBA 为 Word 文档添加页眉和页脚,如下所述?

标题应该是以下组合:

  • “新项目报告”为左对齐的 16 磅粗体,并且
  • 当前日期和时间,非粗体,12 pt 字体,右对齐。 (我在下面的代码中定义了一个 myDateTime 变量用于日期/时间字符串。)

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

我很清楚如何在 Word 中手动执行此操作,但尽管使用 VBA 进行许多 Google 搜索都无法完全破解(或真正弄清楚从哪里开始)。下面是我目前用来创建文档的 Access 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 模板,而不是对所有这些内容进行编码。然后,只需通过现有模板引用该模板即可。文档。添加代码。
  • 好吧,我们需要这个来在许多不同用户的计算机上吐出 Word 文档,这些用户使用我们的前端数据库。其中一些有自己的 .dot 文件,我不想弄乱它们。但是,是的,如果这只是在我的电脑上,我不会打扰这个。
  • 这不会改变任何事情。您可以使用自己的模板而不影响客户的模板。第 101 字。
  • 好的,谢谢,我也会调查一下。所以我会在某处的网络驱动器上有一些其他模板并在 Document.Add 语句中引用该新位置?我会说,虽然所有这些员工都可以访问同一个 SQL Server,但我不确定他们都可以访问同一个网络驱动器。所以不完全确定如果这是这个想法会奏效。
  • 由于这一切都是通过代码完成的,我倾向于将 Word 模板存储在与数据库相同的文件夹中。因此:设置 doc = .Documents.Add(CurrentProject.Path & "\MyTemplate.Dotx")。然后,您的用户将能够将新文档保存到他们具有写入权限的任何驱动器/文件夹中。

标签: vba ms-word


【解决方案1】:

在 Word 中设置标题以使文本左/右对齐可能很棘手,因为与 Excel 不同,没有左/中/右标题 - 它是一个标题。

在下面的代码中,我使用 Tabs 来“证明”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





【讨论】:

    【解决方案2】:

    如果您使用对齐选项卡在 Word 中设置标题以使文本左/右对齐一点也不难。如果您使用普通标签,您更有可能遇到问题。

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

    这可以通过使用 IIRC 在 Word 2010 中引入的对齐选项卡来避免。这些使您可以设置与段落缩进或页边距对齐的选项卡。

    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
    

    【讨论】:

    • InsertAlignmenTab - 我认为这是可以使用但无法理解的东西。:)
    猜你喜欢
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2017-06-14
    • 2012-12-18
    • 2019-01-07
    相关资源
    最近更新 更多