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 / March 2005

Tip: Looking for answers? Try searching our database.

2nd Word Document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim - 15 Mar 2005 02:09 GMT
In an application i can open a specific document through code (VB.NET).  How
would i then add pages to that document without creating another document
or, if i have to create a document, how to then copy the text on that
document, paste it at the end of the other document, and then close that
document(not the one first created but the second)

Thanks
TIm
Chuck - 15 Mar 2005 14:09 GMT
Here's three variations that should give you some ideas / get you started ...

Sub AddPagesToDoc()

   Dim doc1 As Document
   Dim doc2 As Document
   Dim rng As Range
   
   Set doc1 = Documents.Open("c:\temp\testdoc.doc")
   
   Set rng = doc1.Range
   
   With rng
       
       'position insertion point at
       'end of document
       .Collapse wdCollapseEnd
       
       'insert a page break
       .InsertBreak Type:=wdPageBreak
       
       'insert a section break
       .InsertBreak Type:=wdSectionBreakNextPage
       
   End With
   
   doc1.Close wdDoNotSaveChanges
   
   Set doc1 = Nothing

End Sub

Sub CopyTextToEndOfNewDoc()

   Dim doc1 As Document
   Dim doc2 As Document
   Dim rng As Range
   
   Set doc1 = Documents.Open("c:\temp\doc1.doc")
   Set doc2 = Documents.Open("c:\temp\doc2.doc")
   
   Set rng = doc2.Range
   
   With rng
       
       'position insertion point at
       'end of document
       .Collapse wdCollapseEnd
       
       'insert a page break
       .InsertBreak Type:=wdPageBreak
       
       .Text = doc1.Range.Text
       
   End With
   
   doc1.Close wdDoNotSaveChanges
   doc2.Close wdSaveChanges
   
   Set doc1 = Nothing
   Set doc2 = Nothing

End Sub

Sub InsertDoc1AtEndDoc2()
   Dim doc1 As Document

   Dim doc2 As Document
   Dim rng As Range
   
   Set doc2 = Documents.Open("c:\temp\doc2.doc")
   
   Set rng = doc2.Range
   
   With rng
       
       'position insertion point at
       'end of document
       .Collapse wdCollapseEnd
       
       'insert a page break
       .InsertBreak Type:=wdPageBreak
       
   End With
   
   'insert doc1 at the insertion point
   'you just created with the rng object
   rng.InsertFile _
       FileName:="doc1.doc", _
       Range:="", _
       ConfirmConversions:=True, _
       Link:=False, _
       Attachment:=False

   doc2.Close wdSaveChanges
   
   Set doc2 = Nothing

End Sub

> In an application i can open a specific document through code (VB.NET).  How
> would i then add pages to that document without creating another document
[quoted text clipped - 4 lines]
> Thanks
> TIm
Chuck - 15 Mar 2005 17:49 GMT
Whoops!

In the code I posted, in Sub AddPagesToDoc():

doc1.Close wdDoNotSaveChanges

should instead be

doc1.Close wdSaveChanges

> Here's three variations that should give you some ideas / get you started ...
>
[quoted text clipped - 104 lines]
> > Thanks
> > TIm
 
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.