Copy Content from One Word Doc to Another

Status
Not open for further replies.

JonXL

Well-known Member
Joined
Feb 5, 2018
Messages
513
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

I'm trying to append the contents from one Word file to another. Here is more or less what I'm doing now.

VBA Code:
Sub AppendDocument()
    Dim wdDoc As Document, wdOutputDoc As Document
    
    Set wdDoc = Documents.Add
    Set wdDoc = Documents.Add
    
    With wdDoc
    
        Do
              
            'do some stuff to this document
            
            .Content.Copy
            
            With wdOutputDoc.Content
                .Collapse direction:=wdCollapseEnd
                .InsertBreak wdPageBreak
                .PasteAndFormat wdFormatOriginalFormatting
            End With
        
        Loop
    
    End With

End Sub

This works... except because it uses the clipboard, it's open to get junk in it if someone copies between the copy and paste commands of this code. (I tested this while it was running by rapidly and repeatedly copying 'blah' to the clipboard and in at least one instance the appended content was 'blah' and not the contents of the document I wanted appended.)

When I try by using like this ...
VBA Code:
            With wdOutputDoc.Content
                .Collapse direction:=wdCollapseEnd
                .InsertBreak wdPageBreak
                .InsertAfter wdDoc.Content
            End With

... it doesn't include the headings, etc.

I've looked into InsertFile but as you can see from the code, both documents are created in the code and never saved, so there isn't a file location to use. If watching the screen while it runs, you see variably Document1, Document2 or Document3, Document6, etc. - they are not saved files. But maybe there's a way with InsertFile even though they aren't saved.

Otherwise, I'm not sure what options there are for this to work.

Thank you,
Jon
 
Have you tried the code in the link in post #4? Except for your save format and naming convention (which you haven't given any details about), the code in that link does all you ask. The code even lets you choose the source folder without demanding it be a particular folder.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I have referred to your post #11, i believe that could be more useful for my solution,

To give you a background:

We have a folder which contains word documents which needs to have specific type of format, spacing, header and footer, for which i have a template in place how the word document should look like so i just need to copy all the contents from the word document to the template and save as specific naming format, this should be repeated in all the word documents available in that specific folder

my questions

Do i need to mark all the relevant fields in the template doc with"<>" so that the template gets filled automatically from the word documents available in the folder.

Please help and thank you for your patience in reviewing my question and responding to me
 
Upvote 0
What you are now describing is something different from what you first asked. Kindly don't hijack existing threads for different issues. Start your own thread with the full details of what you are trying to do.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top