是否有方法使用VBA向Word文档添加页眉和页脚,如下所述?
标头应为以下各项的组合:
- "新项目报告";以左对齐的16 pt粗体字体显示,以及
- 当前日期和时间,采用12磅非粗体字体,右对齐。(我在下面的代码中定义了一个myDateTime变量,用于日期/时间字符串。(
页脚应该是居中的";第X页,共Y页";(其中X和Y分别是页面#和页面#的字段(。
我很清楚如何在Word中手动完成这项工作,但尽管谷歌搜索了很多次,但我还是无法使用VBA完全破解(或真正弄清楚从哪里开始(。下面是我当前用于创建文档的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中的页眉和页脚样式具有与默认页边距相对应的制表位。如果您的页面布局与默认布局不同,则制表位将位于错误的位置。
这可以通过使用对齐选项卡来避免,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
在Word中设置页眉使文本左/右对齐可能很棘手,因为与Excel不同,没有左/中/右页眉,只有一个页眉。
在下面的代码中,我使用制表符来"调整"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