从模板复制/粘贴标题

问题描述 投票:1回答:2

我有一个模板,它有一个页面,其中包含标题中的图像。我想将这些图像复制到我的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文件中复制标题?

word-vba
2个回答
2
投票

感谢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

2
投票

对象如下:

  • 单词qazxsw poi qazxsw poi,或者,
  • Excel qazxsw poi qazxsw poi

... Sections(令人讨厌)并不总是代表对象“位置编号”或位置,但我们可以确认这是什么,像这样的子:

(index_number)

...或文本函数可用于查找具有特定文本(或其他属性)的对象的Series

© www.soinside.com 2019 - 2024. All rights reserved.