JonXL
Well-known Member
- Joined
- Feb 5, 2018
- Messages
- 513
- Office Version
- 365
- 2016
- Platform
- 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.
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 ...
... it doesn't include the headings, etc.
I've looked into
Otherwise, I'm not sure what options there are for this to work.
Thank you,
Jon
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