Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2004

Tip: Looking for answers? Try searching our database.

Append a range to the end of another document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hotwheels - 22 Nov 2004 20:35 GMT
I need to know how to take a range of a section of one document and append it
to the end of another document .  I have an example subroutine that I have
been testing with that shows my problem.  See *** below.   Do I need too move
my selection in the second document to the end and how do I put the text in
the range from the first document  at that place?

Mike

Private Sub MergeASection(iSection As Integer)
Dim oWrd As Word.Application
Dim oMergeDoc As Word.Document
Dim oDoc As Word.Document
Dim oNewDoc As Word.Document
Dim oRange As Word.Range

   Set oWrd = CreateObject("Word.Application")
   Set oDoc = oWrd.Documents.Open("c:\mikestest\TempTemplate.doc")
   Set oNewDoc = oWrd.Documents.Open("c:\mikestest\NewDoc.doc")
   oDoc.MailMerge.MainDocumentType = wdFormLetters
   oDoc.MailMerge.OpenDataSource Name:="c:\mikestest\data2.dat"
   oWrd.Visible = True
   oWrd.Activate
   Screen.MousePointer = vbDefault
   oDoc.MailMerge.Destination = wdSendToNewDocument
   oDoc.MailMerge.Execute
   Set oMergeDoc = oWrd.ActiveDocument
   Set oRange = oMergeDoc.Sections(iSection).Range
   oRange.End = oRange.End - 1

  *** 'Need to know how to append oRange into the end of   oNewDoc here

   oNewDoc.Saved
   oNewDoc.Close
   oMergeDoc.Saved = True
   oMergeDoc.Close
   oDoc.MailMerge.MainDocumentType = wdNotAMergeDocument
   oDoc.Save
   oDoc.Close
   oWrd.Quit
   
   Set oMergeDoc = Nothing
   Set oDoc = Nothing
   Set oWrd = Nothing
End Sub
Leigh Webber - 22 Nov 2004 22:14 GMT
The easiest way to do this is to use the clipboard:

oRange.Copy
oNewDoc.Bookmarks("\EndOfDoc").Range.Paste

This clobbers your clipboard, of course, so be careful. If you take over the
clipboard inside your code, the user may experience strange things. E.g. if
the user copies some text, then runs your macro, then tries to paste their
text, they will instead paste whatever *your code* copied internally. You
can overcome this with more complex coding if necessary.

> I need to know how to take a range of a section of one document and append it
> to the end of another document .  I have an example subroutine that I have
[quoted text clipped - 40 lines]
>     Set oWrd = Nothing
> End Sub

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.