Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使我的Word模板出现在每个新页面上_Excel_Vba_Ms Word - Fatal编程技术网

Excel 使我的Word模板出现在每个新页面上

Excel 使我的Word模板出现在每个新页面上,excel,vba,ms-word,Excel,Vba,Ms 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 &a

现在我使用代码填充一个文档:

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

从excel vbaTake运行的代码查看Word的
InsertFile
方法:这就是你的意思吗?这是一种可能性。另一种方法是将重复的文本作为构建块保存在真实的模板文件中(dotx或dotm-use Documents.Add,创建新文档而不是打开现有文档,这样就不必担心使用SaveAs来避免过度编写原始文档),并根据需要插入。