从多个word文档VBA复制和粘贴

  • 本文关键字:复制 VBA 文档 word vba excel
  • 更新时间 :
  • 英文 :


将现有word文档复制粘贴到新文档时遇到问题!我可以使用下面的代码将文档1和2的内容复制并粘贴到新文档中,但当文档2粘贴到新的文档中时,它会直接粘贴到从文档1复制的材料下面。我想把从文档2中复制的材料粘贴到另一个材料下面的新页面上,所以有人可以帮我一下,或者朝着正确的方向推我一下。

Sub automateword()
    Dim wordapp As Object
    Set wordapp = CreateObject("word.Application")
    ''''creates and saves new Document''''
    With CreateObject("Word.Document")
        .Windows(1).Visible = True
        .SaveAs Filename:="C:NewDocumnet.docx", FileFormat:=wdFormatDocument
    End With
    wordapp.Documents.Open "C:Document1.docx"
    wordapp.Selection.WholeStory
    wordapp.Selection.Copy
    wordapp.Documents("C:NewDocumnet.docx").Activate
    wordapp.Selection.PasteAndFormat wdInLine
    wordapp.Documents.Open "C:Document2.docx"
    wordapp.Selection.WholeStory
    wordapp.Selection.Copy
    wordapp.Documents("C:NewDocumnet.docx").Activate
    wordapp.Selection.PasteAndFormat wdInLine
    wordapp.Visible = True
End Sub

我想把从文档2中复制的材料粘贴到其他材料下面的新页面上

插入分页符并粘贴。以下是的示例

oWordApp.Selection.PasteAndFormat wdInLine
With oWordApp.Selection
    .Collapse Direction:=0
    .InsertBreak Type:=7
End With
'~~> Put here the copy code. i.e What ever you are copying
oWordApp.Selection.PasteAndFormat wdInLine

如果有不同的文件名,请查看下面的docx和docxx扩展名:

wordapp.Documents("C:NewDocumnet.docx").Activate '
wordapp.Selection.PasteAndFormat wdInLine
wordapp.Documents.Open "C:Document2.docx"
wordapp.Selection.WholeStory
wordapp.Selection.Copy
wordapp.Documents("C:NewDocumnet.docxx").Activate 'different file name
wordapp.Selection.PasteAndFormat wdInLine

您可以使用以下代码片段添加分页符最后。这样可以保持副本的清洁。希望能有所帮助。

Set MainDoc = Application.Documents.Open(strFolder & "BaseDoc.doc")
strFile = Dir$(strFolder & "*.doc") ' can change to .docx
'Loop through all .doc files in that path
Do Until strFile = ""
    Set sourcedoc = Application.Documents.Open(strFolder & "*.doc")
    Application.Selection.WholeStory
    Application.Selection.Copy
    Application.ActiveWindow.Close savechanges:=wdDoNotSaveChanges
    MainDoc.Activate
    Application.Selection.PasteAndFormat (wdFormatOriginalFormattig)
    MainDoc.InsertBreak Type:=wdSectionBreakNextPage
Loop

最新更新