Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: Need info to combine documents

  1. #1

    Need info to combine documents

    Need a macro that will combine a large group of recipes in one folder into a new blank document. So I can go through and apply Heading 1 style to all the recipe names, generate a table of contents, and then see your recipes, and go to outline view and move them around.

  2. #2
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Hi, Pineapple! Sounds familiar! Give me a few minutes, or perhaps someone else will reply before me.

    Glad to see you could make it!

    For the record, we're assuming that you have a bunch of recipes in different Word documents, and that they're all in a folder by themselves, right? No extra files in that folder? Or...you can put them all into one folder, right?
    ~Anne Troy

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Okay, here we go.

    [vba]
    Sub RunMe()
    Dim doc As Document
    Dim num_files As Integer
    Dim file_name As String
    Dim dir_path As String
    Dim file_ext As String

    Set doc = Application.Documents.Add

    dir_path = "C:\Documents and Settings\Your Name\My Documents\"
    file_ext = "doc"

    file_name = Dir(dir_path & "*." & file_ext)
    Do While Len(file_name) > 0
    With Selection
    .InsertFile dir_path & file_name
    .EndKey Unit:=wdStory
    .InsertBreak Type:=wdPageBreak
    End With
    file_name = Dir()
    Loop
    End Sub
    [/vba]

    Instructions:

    Open a blank document.
    Copy the code above.
    Go back to Word and hit Alt+F11 to bring up the Visual Basic Editor (VBE).
    Double-click ThisDocument on the left side of the page, then paste the code into the right side of the page.

    Change the path in the code above to match the one that contains all your recipes. If you're not sure, just go right-click one of your filenames and hit Properties. You'll see the path in there, and it will probably be very similar to the one in the code, especially if you're running Windows XP.

    Then close the VBE using the X on the window.

    With that document in front of you now, hit Tools-Macro-Macros, and double-click "RunMe'. Within a moment you should have all your recipes in one file.

    Save your file. If you like, you can now hit Alt+F11 again, and delete all the code; save your file again, and close the VBE.

    Go through each recipe. When you find the title for each one, click somewhere in the middle of it, and choose Heading 1 from the styles dropdown.

    Let's see if we can get you that far.

    Good luck!

    PS: This code written by BCF, not me!
    ~Anne Troy

  4. #4

    new challenge

    when i try to run macro, i get the message that macros are not enabled. how do i enable the macros?

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    go to Tools -> Macros -> Security. set to Medium. when you open a document that will prompt you to Enable or Disable macros. you have the option of LOW but that will not give you an option. Medium is recommended.

    hth

  6. #6
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Any luck, Pineapple?
    ~Anne Troy

  7. #7
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Any luck here?
    ~Anne Troy

  8. #8
    VBAX Regular
    Joined
    Feb 2005
    Posts
    37
    Location
    Hello,
    I'm new to this thread. This works GREAT. I would like to know if in anyway when documents are added that they keep the original layout. I have different layouts in the folder. Some of the documents are portrait and others are landscape. Any ideas or suggestion on combining them in one document?

  9. #9
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by espencer
    Hello,
    I'm new to this thread. This works GREAT. I would like to know if in anyway when documents are added that they keep the original layout. I have different layouts in the folder. Some of the documents are portrait and others are landscape. Any ideas or suggestion on combining them in one document?
    Hi and welcome to VBAX!

    What you want is possible but there can be many Cave-at's on the way! (And they all have to do with sections and document formatting)

    I think this will get you on your way:[VBA]
    Sub CombineDocuments()
    Dim oApp As New Word.Application
    Dim oTarget As Word.Document: Dim oInsert As Word.Document
    Dim sFile As String: Dim sPath As String: Dim sExt As String
    Dim iOrient As Integer
    Set oTarget = oApp.Documents.Add

    sPath = "C:\Documents and Settings\Admin\Desktop\Test\"
    sExt = "doc"
    sFile = Dir(sPath & "*." & sExt)

    Do While Len(sFile) > 0
    Set oInsert = oApp.Documents.Open(FileName:=sPath & sFile)
    iOrient = oInsert.PageSetup.Orientation
    oInsert.Close False
    Set oInsert = Nothing

    With oApp.Selection
    If iOrient = 0 Then
    .PageSetup.Orientation = wdOrientPortrait
    Else
    .PageSetup.Orientation = wdOrientLandscape
    End If

    .InsertFile FileName:=sPath & sFile
    .Collapse Direction:=wdCollapseEnd

    sFile = Dir()
    If Len(sFile) > 0 Then
    .InsertBreak Type:=wdSectionBreakNextPage
    End If
    End With
    Loop

    With oApp
    .Visible = True
    .Activate
    End With

    Set oTarget = Nothing
    Set oApp = Nothing
    End Sub
    [/VBA]
    This Sub will run a lot slower because we have to open each document we like to insert to retrieve the page orientation! (So we know how to deal with sections page setup)

    There are probably better ways of doing this but this is a good starting point!

    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  10. #10
    VBAX Regular
    Joined
    Feb 2005
    Posts
    37
    Location
    This seems to work but like you said I'll need to address the sections and document formatting of the documents. Thanks for the help. If you could, would you point me in some direction on sections and document formatting. I would appreciate it.
    Again THANKS.

  11. #11
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by espencer
    This seems to work but like you said I'll need to address the sections and document formatting of the documents. Thanks for the help. If you could, would you point me in some direction on sections and document formatting. I would appreciate it.
    Again THANKS.
    Hi,

    You're Welcome!

    I've done quick tests with this code and everything was merged in a neat fashion without losing formatting.

    I couldn't say exactly wich cave-at's you will encounter exactally.

    If you come about pertical problems than you can place them over here (Clearly explained) so we can try to catch the cause of this problem.

    My code places each document into it's own section in Word and that normally should be enough to ensure formatting integrity!

    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  12. #12
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    When you run Joost's code, what is NOT kept?
    ~Anne Troy

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location

    Smile

    Hi Joost. Interesting, in that this is the WORD Help forum, that you have the code creating a new Word application. Care to comment why?

    In any case, here is an alternative that adjusts Anne's original code. It is true, unfortunately, that to maintain file orientation you have to open it first. But you CAN maintain it by using a Section break, rather than Anne's original PageBreak. I also notice you open the file, check the orientation, CLOSE the file, then insert the file.

    If the file is open, you may as well just copy it..... then close it.

    [vba] Dim doc As Document
    Dim r As Range
    Dim num_files As Integer
    Dim file_name As String
    Dim dir_path As String
    Dim file_ext As String
    Dim lngPage As Long

    Set doc = Application.Documents.Add

    dir_path = "C:\Documents and Settings\gerry.knight\My Documents\gerry\"
    file_ext = "doc"

    file_name = Dir(dir_path & "*." & file_ext)
    Do While Len(file_name) > 0
    Documents.Open FileName:=dir_path & file_name
    lngPage = ActiveDocument.PageSetup.Orientation
    Set r = Selection.Range
    r.WholeStory
    r.Copy
    ActiveDocument.Close wdDoNotSaveChanges
    With Selection
    With .Sections(1).PageSetup
    .Orientation = lngPage
    .SectionStart = wdSectionNewPage
    End With
    .PasteAndFormat wdFormatOriginalFormatting
    .InsertBreak Type:=wdSectionBreakNextPage
    .EndKey unit:=wdStory
    End With
    file_name = Dir()
    Loop[/vba]

  14. #14
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fumei
    Hi Joost. Interesting, in that this is the WORD Help forum, that you have the code creating a new Word application. Care to comment why?
    Hi Gerry,

    Well the main reason was to keep everything invisible to the eye off the one who's executing the macro.

    Word has a habbit off screen flickering when opening a lot off documents...(That screenupdating = false don't always fixxes)

    If that was the right way to go...you tell me?

    Like I said there are better ways of coding this...the best in my opion is the one that runs reliable and fast...(Will test you're code wich is shorter so if it's faster...then it's better)

    In any case, here is an alternative that adjusts Anne's original code. It is true, unfortunately, that to maintain file orientation you have to open it first.
    That's what I hate about the code the most as I mentioned...It slow's things down to much..

    But you CAN maintain it by using a Section break, rather than Anne's original PageBreak. I also notice you open the file, check the orientation, CLOSE the file, then insert the file.

    If the file is open, you may as well just copy it..... then close it.
    Like we said before you have to read the orientation so on that we agree.

    I don't like copy/Paste (Cause I believe I had problems in the past with that because it relies on the default paste options in higher version off Word..That is if I recall well)
    So basically I don't like copy/paste in code that was my reason for doing so...

    The other thing was that the Insertfile command is so fast that it doesn't matter in speed.
    Ah now I run your code I see you forgot to test if there is a new file to insert like I do in the code. This leaves you with a blank page to many on the end of your code!

    Like:[VBA]
    sFile = Dir()
    If Len(sFile) > 0 Then
    .InsertBreak Type:=wdSectionBreakNextPage
    End If
    [/VBA]
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes, I forgot to test. At the end of the Dir, move the Selection to the end of the document, check if it is a Chr(12), if so, delete. Actually, the way the code is, it always WILL be a Chr(12), so really no need to test, just delete it.

    Alternatively, add logic that if the return from Dir has a Len(filename) = 0 delete the Selection.

  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Gerry,

    Yes you could delete it..it doesn't seam to make any difference for me.

    As for logics we all have our own for me it's more sensible to test if there are more files if so add a break if not don't...

    To me there could be one improvent to the code and that's to check for trailing Chr$(13) after you've inserted the documents and before we insert a break..(To tiddy things up)
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  17. #17
    VBAX Regular
    Joined
    Feb 2005
    Posts
    37
    Location
    Thanks for all of the help so far. I've enclosed the three documents in questioned. Note02 is in portrait, note03 is in portrait and note04 is in landscape. I think the problem is in the formatting of the documents. Feel free to run the notes through the macro and give me you take on this. THANKS.

  18. #18
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    Did you try Gerry's Sub?

    It run's faster then mine and has a better result then my sub!
    I've altered it a little bit so that the last blank page is no longer there

    Here it is by Dreamboat/Gerry/and little old me:[VBA]
    Sub test()
    Dim doc As Document
    Dim r As Range
    Dim num_files As Integer
    Dim file_name As String
    Dim dir_path As String
    Dim file_ext As String
    Dim lngPage As Long

    Application.ScreenUpdating = False
    Set doc = Application.Documents.Add

    dir_path = "C:\Documents and Settings\Admin\Desktop\Test\"
    file_ext = "doc"

    file_name = Dir(dir_path & "*." & file_ext)
    Do While Len(file_name) > 0
    Documents.Open FileName:=dir_path & file_name
    lngPage = ActiveDocument.PageSetup.Orientation
    Set r = Selection.Range
    r.WholeStory
    r.Copy
    ActiveDocument.Close wdDoNotSaveChanges
    With Selection
    With .Sections(1).PageSetup
    .Orientation = lngPage
    .SectionStart = wdSectionNewPage
    End With
    .PasteAndFormat wdFormatOriginalFormatting
    file_name = Dir()
    If Len(file_name) > 0 Then
    .InsertBreak Type:=wdSectionBreakNextPage
    End If
    .EndKey unit:=wdStory
    End With
    Loop

    End Sub
    [/VBA]
    Have a try just change the path of dir_path
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  19. #19
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I tried it with your test files. Seems to work fine for me. Are you having problems?

  20. #20
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by espencer
    Thanks for all of the help so far. I've enclosed the three documents in questioned. Note02 is in portrait, note03 is in portrait and note04 is in landscape. I think the problem is in the formatting of the documents. Feel free to run the notes through the macro and give me you take on this. THANKS.
    Hi,

    Did you have time to try the code in my previous post? (Seams to work well for me and Gerry)

    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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