简体   繁体   中英

Copy and Pasting from multiple word documents VBA

I am having a problem when it comes to copying and pasting from existing word documents into a new document! I can get the contents from documents 1 and 2 copied and pasted into the new document with the code below however when document2 is pasted into the new document it is pasted directly below the material that was copied from document1. I want the material copied from document2 to be pasted onto a new page below the other material so could somebody please give me a hand with this or a nudge in the right direction.

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

I want the material copied from document2 to be pasted onto a new page below the other material

Insert a pagebreak and do the pasting. Here is an example

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

You have different file names, look at the docx and docxx extensions below:

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

You can use the following code snippet to add a page break at the end. This should keep the copies clean. Hope it helps.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM