+ Reply to Thread
Results 1 to 3 of 3

How do I sort files before Merging in Word? using VBA macros

  1. #1
    Registered User
    Join Date
    09-10-2010
    Location
    Connecticut, USA
    MS-Off Ver
    Excel 2007
    Posts
    11

    How do I sort files before Merging in Word? using VBA macros

    I have a macro where I merge all RTF (or doc) files in a folder into one word document and create a Table of Contents. But I am trying to figure how to get it to sort before merging.

    So my files are label as:

    Output 01 - XXXX.rtf
    Output 02 - YYYY.rtf
    Output 03 - ZZZZ.rtf
    ....
    Output 09 - AAAA.rtf

    So I have created this macro to merge all my RTF files in a folder together. But it seem to merge it out of order. With the code I posted below, it copies in this order: 09,08,07,01,02,03,05,06,04... When I actually want it to copy the file in numerical order, 01,02,03,04,05,06,07,08,09. So what can I add on to this code to get it to do that?

    Overall, I am hoping this macro can merge any number of outputs, since depending on the project the number of outputs varies. The first part of the file name is always the same with exception of the numbering (Output XX).


    Please Login or Register  to view this content.
    Last edited by pennywaltz; 10-25-2014 at 08:42 PM.

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: How do I sort files before Merging in Word? using VBA macros

    You could read the filenames in the directory into an array, sort the array and then open the files by cycling through the sorted array

  3. #3
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: How do I sort files before Merging in Word? using VBA macros

    AFAIK, the Dir function normally returns a sorted list. If it's not, you could take the approach suggested by pjwhitfield. However, Word VBA doesn't have an array sorting function. But, since this is evidently a Word macro, you could call the ancient WordBasic.SortArray procedure to do the job.

    Your code also makes extensive use of selections instead of range objects, which is quite inefficient. Try the following, which incorporates the WordBasic.SortArray procedure:
    Please Login or Register  to view this content.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 03-03-2013, 12:25 PM
  2. [SOLVED] Repeat macros in a loop and save as word files
    By amanduggal in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-07-2012, 08:39 AM
  3. Merging two files together based on col common to both files
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-05-2011, 01:06 PM
  4. Replies: 5
    Last Post: 02-17-2010, 02:02 PM
  5. Merging into Word (not all files)
    By sanya33 in forum Word Formatting & General
    Replies: 1
    Last Post: 08-30-2009, 11:34 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1