2nd Word Document

T

Tim

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
 
C

Chuck

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
 
C

Chuck

Whoops!

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

doc1.Close wdDoNotSaveChanges

should instead be

doc1.Close wdSaveChanges
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top