[英]Insert Word document from template and fill bookmarks from Excel
I need to open a template file, fill its bookmarks, insert a new page from the same template, fill bookmarks again... but only on the inserted page.我需要打开一个模板文件,填充它的书签,从同一个模板插入一个新页面,再次填充书签......但仅限于插入的页面。
Sub test()
Dim WA As Object, WD As Object
Set WA = CreateObject("Word.Application")
Set WD = WA.Documents.Add(TemplatesName)
For i = 1 To 100
fillBookmarks WA, WD
With WD.Range
.Collapse 0
.InsertBreak Type:=wdSectionBreakNextPage
.End = WD.Range.End
'.Collapse 0
.InsertFile TemplatesName
End With
Next i
WD.SaveAs PdfFile, 17
WD.Close False: Set WD = Nothing
WA.Quit False: Set WA = Nothing
End Sub
Function fillBookmarks(ByVal WA As Object, ByVal WD As Object)
With WD
.Bookmarks.Item("Client_Code").Range.Text = "545"
.Bookmarks.Item("Client_Code").Delete
.Bookmarks.Item("Company_Name").Range.Text = "545"
.Bookmarks.Item("Company_Name").Delete
.Bookmarks.Item("Company_Street").Range.Text = "545"
.Bookmarks.Item("Company_Street").Delete
.Bookmarks.Item("Company_PostCode").Range.Text = "545"
.Bookmarks.Item("Company_PostCode").Delete
.Bookmarks.Item("Company_Country").Range.Text = "545"
.Bookmarks.Item("Company_Country").Delete
End With
End Function
UPDATE: the following does a good job but in reverse order (first page became last) how to reverse back it?更新:以下做得很好,但顺序相反(第一页变成了最后一页)如何反转它?
With WD.Range
.InsertBreak Type:=wdSectionBreakNextPage
.Collapse Direction:=wdCollapseEnd
.MoveEnd Unit:=wdCharacter, Count:=-1
.InsertFile TemplatesName
End With
The bookmarks are being filled only on first page (also after deleting), so how to insert page from template and fill bookmarks?书签仅在第一页上填充(删除后也是如此),那么如何从模板插入页面并填充书签?
There are a couple of issues, here: 1. A bookmark name must be unique in the document.这里有几个问题: 1. 书签名称在文档中必须是唯一的。 So whenever you insert the new section the bookmarks are being removed if the bookmark names already exist.因此,每当您插入新部分时,如果书签名称已存在,就会删除书签。 2. Getting the insertion point at the right place for new material. 2. 在新材料的正确位置获取插入点。
Working with Word Range
objects is the correct approach, but it needs to be refined a bit.使用 Word Range
对象是正确的方法,但需要稍微改进。 The code in the question always tries to work with the entire document.问题中的代码总是尝试处理整个文档。 It's necessary, however, to work with a more "pin-pointed" range.然而,有必要使用更“精确”的范围。 I've declared two Range objects: one for the entire document, one for the "pin-point" that marks where new content should be inserted.我已经声明了两个 Range 对象:一个用于整个文档,一个用于标记新内容应该插入的位置的“定位点”。
Notice the use of Duplicate
to "copy" the original document Range.注意使用Duplicate
来“复制”原始文档 Range。 If =
were used, instead, that would be an exact copy: changing one would automatically change the other.相反,如果使用=
,那将是一个精确的副本:更改一个将自动更改另一个。
Notice also the user of the Start
and End
properties of the two ranges to always position the "pin-point" range at the end of the document.还要注意两个范围的Start
和End
属性的用户始终将“精确定位”范围定位在文档的末尾。
There's no need to pass the Word application to the function that fills the bookmarks, so I've removed that in my sample code that follows.无需将 Word 应用程序传递给填充书签的函数,因此我已在后面的示例代码中删除了它。
For people who come across this question and want to use the code: If the bookmarks are the [content] rather than the I-beam type (there's text inside the bookmark) the original code will cause an error.对于遇到这个问题并想使用代码的人:如果书签是[内容]而不是工字梁类型(书签内有文本),则原始代码将导致错误。 Writing text to a [content] bookmark automatically removes the bookmark.将文本写入 [content] 书签会自动删除书签。 In that case, the function fillBookmarks_withContent
will work.在这种情况下,函数fillBookmarks_withContent
将起作用。 The function fillRemoveBookmarks
at the end will work with both types of bookmark.最后的函数fillRemoveBookmarks
将适用于两种类型的书签。
Sub test()
Dim WA As Object, WD As Object
Dim rngDoc as Object, rngInsert as Object 'both data type Word.Range
Set WA = CreateObject("Word.Application")
Set WD = WA.Documents.Add(TemplatesName)
Set rngDoc = WD.Content
Set rngInsert = rngDoc.Duplicate
For i = 1 To 100
fillBookmarks WD
With rngInsert
.Start = rngDoc.End
.InsertBreak Type:=wdSectionBreakNextPage
.Start = rngDoc.End
.InsertFile TemplatesName
End With
Next i
WD.SaveAs PdfFile, 17
WD.Close False: Set WD = Nothing
WA.Quit False: Set WA = Nothing
End Sub
Function fillBookmarks(ByVal WD As Object)
With WD
.Bookmarks.Item("Client_Code").Range.Text = "545"
.Bookmarks.Item("Client_Code").Delete
.Bookmarks.Item("Company_Name").Range.Text = "545"
.Bookmarks.Item("Company_Name").Delete
.Bookmarks.Item("Company_Street").Range.Text = "545"
.Bookmarks.Item("Company_Street").Delete
.Bookmarks.Item("Company_PostCode").Range.Text = "545"
.Bookmarks.Item("Company_PostCode").Delete
.Bookmarks.Item("Company_Country").Range.Text = "545"
.Bookmarks.Item("Company_Country").Delete
End With
End Function
Function fillBookmarks_withContent(ByVal WD As Object)
With WD
.Bookmarks.Item("One").Range.Text = "545"
.Bookmarks.Item("TWo").Range.Text = "544"
End With
End Function
Function fillRemoveBookmarks(ByVal wd As Word.Document)
With wd
.Bookmarks.Item("One").Range.Text = "545"
If Bookmarks.Exists("One") Then .Bookmarks("One").Delete
.Bookmarks.Item("TWo").Range.Text = "544"
If Bookmarks.Exists("Two") Then .Bookmarks("Two").Delete
End With
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.