PDA

View Full Version : Solved: One Document From Many Code Doesn't Work



DonCard
09-02-2004, 09:40 AM
I have tried using the following code that merges several separate docs into one. In my case, each separate doc is one full page. The code works but the resulting doc contains a blank page between each full page. Is there anyway to eliminate the blanks?


Sub MergeAll()
Dim wrd As Word.Application
Set wrd = CreateObject("word.application")
wrd.Visible = True
AppActivate wrd.Name
fname = Dir("C:\Documents and Settings\don\My Documents\MergeDocs\*.doc")
Do While (fname <> "")
With wrd
Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Selection.InsertFile FileName:=("C:\Documents and Settings\don\My Documents\MergeDocs\" & fname)
End With
fname = Dir
Loop
Set wd = Nothing
End Sub

DonCard
09-03-2004, 02:23 PM
Never mind. I tinkered with it and found that by removing the line "Selection.InsertBreak Type:=wdPageBreaK" my extra pages disappeared. Sorry for throwing such a soft-ball.

village_alchemist
09-03-2004, 04:20 PM
Hey, happens to all of us, so don't apologize for asking.

I'm assuming this is part of a larger app, but you might want to check to see if word is already running before you start a new instance of it.

As for the wdPageBreak, you could check the number of lines in the file that you are importing and if it's not a full page, then use the wdPageBreak (after inserting instead of before) otherwise skip it. For that matter, you could check where the end of the document you are building is and if it is in the middle of the page, you could do the wdPageBreak then insert the next file.