[英]Using MS Access and VBA to generate Ms Word Document
这是我第一次编码VBA。 我需要使用数据库中的数据生成Word Doc(仅限选定表)。 我设法使用XXX.Selection.TypeText
在文档中创建了一些文本的Word Doc。 但是我不知道如何为报表包括页眉和页脚。 我尝试了许多方法,但无法获得想要的结果。
我的问题是,是否可以使用/打开预定义的Word文档(带有页眉和页脚)并将数据填充到该预定义的Word文档中?
谢谢!
看下面的代码:
这将创建一个新的word文档,其中包括页眉和页脚内容以及正文内容。
注意:不要忘记为Microsoft Word对象添加引用
Dim objWord As Word.Application
Dim doc As Word.Document
Dim WordHeaderFooter As HeaderFooter
Set objWord = CreateObject("Word.Application")
With objWord
.Visible = True
Set doc = .Documents.Add
doc.SaveAs CurrentProject.Path & "\TestDoc.doc"
End With
With objWord.Selection
.Font.Name = "Trebuchet MS"
.Font.Size = 16
.TypeText "Here is an example test line, #" & " - Font size is " & .Font.Size
.TypeParagraph
'Add header and footer
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "Header"
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = "Footer"
End With
doc.Save
doc.Activate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.