Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vba code to append one document to another 2

Status
Not open for further replies.

BitZero

Programmer
Mar 11, 2008
100
US
I have some VBA code that copies the contents of one word document to the end of another. The code looks like this:

Code:
dim obj_Work_Doc as Word.Document
dim obj_Output_Doc as Word.Document

obj_Work_Doc.Activate
obj_Work_Doc.Application.Selection.WholeStory
obj_Work_Doc.Application.Selection.Copy

obj_Output_Doc.Activate
obj_Output_Doc.Application.Selection.EndOf Unit:=wdStory, Extend:=wdMove
obj_Output_Doc.Application.Selection.PasteAndFormat (wdPasteDefault)
obj_Output_Doc.Application.Selection.InsertBreak Type:=wdPageBreak

This code works, but is there a more direct or better way to do this? Something like this:

obj_Output_Doc.Append obj_Work_Doc.WholeThing

 
There is no need to activate or select anything. I'd be inclined to do something like:
Code:
Dim obj_Work_Doc As Word.Document
Dim obj_Output_Doc As Word.Document
obj_Work_Doc.Range.Copy
With obj_Output_Doc.Range
  .InsertAfter Chr(12)
  .Collapse wdCollapseEnd
  .PasteAndFormat (wdPasteDefault)
End With

Cheers
Paul Edstein
[MS MVP - Word]
 
I have written a macro to merge several docs into one.
This is the main part of my code for doing so:
Code:
        With doc.Content
[COLOR=#729FCF]            .InsertAfter "{%file: " & myNextDoc & "%}" & vbNewLine
            .Collapse wdCollapseEnd[/color]
            [b].InsertFile myNextDoc [/b]
        End With

Works nicely for me and no need to paste anything. The light blue part is for identifying the start of the next merged document because I need to split them into individual files again later. You probably won't need those two lines.
:)

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Thanks for the quick replies. I don't think .InsertFile will work in my case because I'm not copying from a File but a Document object, but I will definitely remember that one. Looks like I'm stuck with the copy/paste, although my code is now a little simpler.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top