首页 > 解决方案 > 让我的 Word 模板出现在每个新页面上

问题描述

现在我使用代码来填写一份文件:

Sub UpdateDocuments()
    Application.ScreenUpdating = False
    Dim strFolder As String, strFile As String, wdDoc As Document
    strFile = Dir(strFolder & "\*.docx", vbNormal)
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
        .Bookmarks.Item("Company_Name").Range.InsertAfter Prefix & " " & Company_Name
        .Bookmarks.Item("Company_Street").Range.InsertAfter Company_Street
        .Bookmarks.Item("Company_Street_Nr").Range.InsertAfter Company_Street_Nr
        .Bookmarks.Item("Company_PostCode").Range.InsertAfter Company_PostCode
        .Bookmarks.Item("Company_City").Range.InsertAfter Company_City
        .Bookmarks.Item("Company_Country").Range.InsertAfter Company_Country
        ' here need to do the same thing from 1 to 100 times again, but with other data
        .Close SaveChanges:=True
    End With
    Set wdDoc = Nothing
    Application.ScreenUpdating = True
End Sub

但是如何在同一个打开的实例中再次重用这个模板,所以在填充第一个书签后,在与下一页相同的文档中创建新的相同模板并再次填充它们?

所以我需要类似的东西

Sub UpdateDocuments()
    Application.ScreenUpdating = False
    Dim strFolder As String, strFile As String, wdDoc As Document
    strFile = Dir(strFolder & "\*.docx", vbNormal)
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
        fillbookmarks ' with new data
        add new page from the same strFile
        fillbookmarks ' with new data
        add new page from the same strFile
        fillbookmarks ' with new data
        .Close SaveChanges:=True
    End With
    Set wdDoc = Nothing
    Application.ScreenUpdating = True
End Sub

标签: excelvbams-word

解决方案


推荐阅读