Extra page copied over when I do a copy from a merged doc

H

Hotwheels

When I do Sections.First.Range.Copy and then do a paste into a new document,
I get an extra page at the end of the new document. The template has two
pages, the merged document has two pages, but the new document gets three
pages. I also noticed that it has two sections? I tried the same code with
a simple document and did the same copy and paste without a merge and only
two pages came over and one section. How can I get rid of this second
section?
Why am I creating two sections when I do a merge and a copy and paste?

Mike

Dim oWrd As Object
Dim oMergeDoc As Object
Dim oDoc As Object

Set oWrd = CreateObject("Word.Application")
Set oDoc = oWrd.Documents.Open("c:\mikestest\TempTemplate.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
oMergeDoc.Sections.First.Range.Copy
oMergeDoc.Activate
oWrd.Documents.Add
oWrd.Selection.Paste
oWrd.ActiveDocument.SaveAs FileName:="c:\mikestest\mikestest2"
oWrd.ActiveDocument.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
 
D

Doug Robbins

Set a Range object to the .Range of the Section and then use

[Rangeobject].End = [Rangeobject].End - 1

so that the Section break is excluded from the range.

Here's an example where that is done:

Sub splitter()

' splitter Macro

' Macro created by Doug Robbins to save each letter created by a mailmerge
as a separate file.

Dim i As Long, Source as Document, Target as Document, Letter as Range
Set Source = ActiveDocument
For i = 1 to Source.Sections.Count
Set Letter = Source.Sections(i).Range
Letter.End=Letter.End-1
Set Target = Documents.Add
Target.Range=Letter
Target.SaveAs FileName:="Letter" & i
Target.Close
Next i

End Sub


Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 

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