2014-10-20 272 views
1

从现有的word文档复制并粘贴到新文档时,我遇到了问题!我可以将文档1和文档2中的内容复制并粘贴到具有下面代码的新文档中,但是,当将文档2粘贴到新文档中时,它将直接粘贴到从文档1复制的材料下。我希望将从文档2复制的材料粘贴到其他材料下面的新页面中,这样可能有人请给我一个这样的或正确方向的微调。从多个word文档中复制和粘贴VBA

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 

回答

1

我想从书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 
+0

这正是我正在寻找的!非常感谢您的帮助 – user3538102 2014-10-20 11:06:25

+0

很高兴为您工作:) – 2014-10-20 11:11:19

0

你有不同的文件名,看的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 
+0

这是一个错字,抱歉,它不是这样在我的代码! – user3538102 2014-10-20 11:04:56

0

您可以使用下面的代码片段在末尾添加分页符 。这应该保持副本清洁。 希望它有帮助。

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