Consulting

Results 1 to 3 of 3

Thread: Solved: One Document From Many Code Doesn't Work

  1. #1
    VBAX Regular
    Joined
    Aug 2004
    Posts
    20
    Location

    Solved: One Document From Many Code Doesn't Work

    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?

    [VBA]
    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
    [/VBA]

  2. #2
    VBAX Regular
    Joined
    Aug 2004
    Posts
    20
    Location
    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.

  3. #3
    VBAX Regular village_alchemist's Avatar
    Joined
    Jul 2004
    Location
    Michigan, US
    Posts
    32
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •