我有一个模板,它有一个页面,其中包含标题中的图像。我想将这些图像复制到我的ActiveDocument。我使用以下代码:
Set doc = ActiveDocument
strTemplate = "C:\Users\rajtilak\Desktop\Report.dotx"
Set docTemplate = Documents.Open(strTemplate)
Set hdr1 = docTemplate.Sections(1).headers(wdHeaderFooterPrimary)
Set hdr2 = doc.Sections(3).headers(wdHeaderFooterPrimary)
hdr1.Range.Copy
hdr2.Range.PasteAndFormat wdFormatOriginalFormatting
docTemplate.Close False
这工作正常,除了它不是复制第1节的标题,而是复制模板的第5部分。有没有其他方法可以使用VBA从Word文件中复制标题?
感谢Kazimierz Jawor,我得到了代码。这是更新的代码:
Dim docTemplate As Document
Dim strTemplate As String
Dim hdr1 As headerfooter
Dim hdr2 As headerfooter
Dim doc As Document
Set doc = ActiveDocument
strTemplate = "C:\Users\rajtilak\Desktop\Report.dotx"
Set docTemplate = Documents.Open(strTemplate)
Set hdr1 = docTemplate.Sections(1).headers(wdHeaderFooterFirstPage)
Set hdr2 = doc.Sections(3).headers(wdHeaderFooterPrimary)
hdr1.Range.Copy
hdr2.Range.Paste
docTemplate.Close False