Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-16-2014, 08:22 AM
jpb103's Avatar
jpb103 jpb103 is offline Determining merge order Windows 7 64bit Determining merge order Office 2007
Advanced Beginner
Determining merge order
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default Determining merge order

Hey fellas,

I've got a macro that grabs the data from the first table in every .docm file in a network folder, and then stitches all the data together into one table. It works fine, the problem I'm having is in determining in what order the files in this folder are opened. At the moment it seems to be random, though I know this cannot be the case. The files are named to reflect their position in the merged document (i.e. "A) Health and Safety", "B) Environment", etc...). I would think it would open them in alphabetical sequence (A, B, C, and so on), but it does not. The current order is (D, C, A, B, F, K, B, E, J ,G, L, C, I, H, C, B) keeping in mind that there are three B files and three C files whos inside order does not matter. Code follows:
Code:
Sub GetJournalData()
'///////////////////////////////////////////////////////////////////////////////////
'/////This function stitches together the data from the first table in all .docm////
'///////////////////////files in the listed directory.//////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////
    Dim Target As Word.Document
    Dim Source As Word.Document
    Dim RowTarget As Row
    Dim RowCount As Integer
    Dim iCounter As Integer
    Dim Folder As String
    Dim File As String
    'Set current document as Source file
    Set Source = ActiveDocument
    'Clear table of previous data, if it exists
    Do While Source.Tables(1).Rows.Count > 2
        Source.Tables(1).Rows.Last.Delete
    Loop
    'Set target folder
    Folder = "\\SharePoint@Port\teamsites\subsite\subsite\site\library\"
    'Find any and all .docm files in target folder
    File = Dir(Folder & "\*.docm")
    'Loop while there are still files to process
    Do While File <> ""
        'Open target and set Target variable
        Documents.Open FileName:=Folder & File
        Set Target = ActiveDocument
        RowCount = Target.Tables(1).Rows.Count
        For iCounter = 3 To RowCount
            'Add row and set elements
            Set RowTarget = Source.Tables(1).Rows.Add
            RowTarget.Cells(1).Range.Text = Target.Tables(1).Rows(iCounter).Cells(1).Range.Text
            RowTarget.Cells(2).Range.Text = Target.Tables(1).Rows(iCounter).Cells(2).Range.Text
            RowTarget.Cells(3).Range.Text = Target.Name
            RowTarget.Cells(4).Range.Text = Target.BuiltInDocumentProperties("Last Save Time")
        Next iCounter
        'Close target file
        Target.Close savechanges:=False
        File = Dir
    Loop
'//////////////////END//////////////////////////////////////////////////////////
End Sub
The apparent order does not appear to conincide with any property of the file (created date/time, last modified date/time, size, etc..). Any ideas, Word VBA community?

Last edited by jpb103; 06-16-2014 at 08:49 AM. Reason: Additional information
Reply With Quote
  #2  
Old 06-16-2014, 05:17 PM
gmaxey gmaxey is offline Determining merge order Windows 7 32bit Determining merge order Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

DIR won't return files in alphabetical order. You can try filesystemobject:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFSO As FileSystemObject
Dim oFolder As Folder
Dim oFile As File
  Set oFSO = New FileSystemObject
  Set oFolder = oFSO.GetFolder("Your folder path")
  For Each oFile In oFolder.Files
    Debug.Print oFile.Name
  Next oFile
  
  
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 06-16-2014, 05:29 PM
macropod's Avatar
macropod macropod is online now Determining merge order Windows 7 32bit Determining merge order Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gmaxey View Post
DIR won't return files in alphabetical order.
It does for me. Indeed, that seems to be the only order in which it returns files.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 06-16-2014, 05:43 PM
gmaxey gmaxey is offline Determining merge order Windows 7 32bit Determining merge order Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Paul,

I misspoke. My experience is that DIR can't guarantee files are returned in alphabetically order. I've experience it before and from what little I've read (via Google search) there isn't much that can be done about it.

Sorry for sounding dogmatic.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 06-16-2014, 07:23 PM
macropod's Avatar
macropod macropod is online now Determining merge order Windows 7 32bit Determining merge order Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Does using FileSystemObject guarantee the sort order? Another way of handling this, without resorting to FileSystemObject, would be to use:
Code:
    Dim FileArray() As String
    Dim i As Long
    'Loop while there are still files to process
    Do While File <> ""
        i = i + 1
        ReDim Preserve FileArray(i)
        FileArray(i) = File
        File = Dir
    Loop
    WordBasic.SortArray FileArray
    For i = 1 To UBound(FileArray)
        Set Target = Documents.Open(FileName:=Folder & FileArray(i), AddToRecentFiles:=False, Visible:=False, ReadOnly:=True)
...
    Next
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 06-16-2014, 07:51 PM
gmaxey gmaxey is offline Determining merge order Windows 7 32bit Determining merge order Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Paul,

I really don't know if FileSytemObject guarantees order or not. It always has for me. However, since I apparently have no idea what I am talking about here, I'm going to withdraw from the field before you bury me ;-)
.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 06-17-2014, 05:19 AM
jpb103's Avatar
jpb103 jpb103 is offline Determining merge order Windows 7 64bit Determining merge order Office 2007
Advanced Beginner
Determining merge order
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default

Thanks guys, I'll try these solutions out today.
Reply With Quote
  #8  
Old 06-17-2014, 07:34 AM
jpb103's Avatar
jpb103 jpb103 is offline Determining merge order Windows 7 64bit Determining merge order Office 2007
Advanced Beginner
Determining merge order
 
Join Date: May 2014
Location: Thunder Bay, Ontario
Posts: 58
jpb103 is on a distinguished road
Default

Solved. I used a quicksort algorithm though. Code follows:
Code:
'///////////////////////////////////////////////////////////////////////////////////
'/////This function stitches together the data from the first table in all /////////
'///////////////////////files in the listed directory.//////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////
Sub GetJournalData()
    Dim Target As Word.Document
    Dim Source As Word.Document
    Dim RowTarget As Row
    Dim RowCount As Integer
    Dim iCounter As Integer
    Dim Folder As String
    Dim File As String
    Dim FileArray() As String
    Dim i As Long
    'Set current document as Source file
    Set Source = ActiveDocument
    'Clear table of previous data, if it exists
    Do While Source.Tables(1).Rows.Count > 2
        Source.Tables(1).Rows.Last.Delete
    Loop
    'Set UNC folder path
    Folder = "\\SharePointsite@Port#\teamsite\site\library\"
    File = Dir(Folder)
    'Load file array
    Do While File <> ""
        i = i + 1
        ReDim Preserve FileArray(i)
        FileArray(i) = File
        File = Dir
    Loop
    'Sort file array alphabetically with quicksort algorithm
    Call QuickSort(FileArray, LBound(FileArray), UBound(FileArray))
    'Cycle through array, opening files and grabbing data
    For i = 1 To UBound(FileArray)
        Set Target = Documents.Open(FileName:=Folder & FileArray(i), Addtorecentfiles:=False, Visible:=False, ReadOnly:=True)
        RowCount = Target.Tables(1).Rows.Count
        For iCounter = 3 To RowCount
            Set RowTarget = Source.Tables(1).Rows.Add
            RowTarget.Cells(1).Range.Text = Target.Tables(1).Rows(iCounter).Cells(1).Range.Text
            RowTarget.Cells(2).Range.Text = Target.Tables(1).Rows(iCounter).Cells(2).Range.Text
            RowTarget.Cells(3).Range.Text = Target.Name
            RowTarget.Cells(4).Range.Text = Target.BuiltInDocumentProperties("Last Save Time")
        Next iCounter
        Target.Close savechanges:=False
    Next
End Sub
'///////////////////////////////////////END/////////////////////////////////////////
'///////////////////////////////////////////////////////////////////////////////////
'//////This function sorts the passed array based on the start and end points///////
'///////////////////////////////////////////////////////////////////////////////////
Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)
    Dim pivot   As Variant
    Dim tmpSwap As Variant
    Dim tmpLow  As Long
    Dim tmpHi   As Long
    'Set initial bounds
    tmpLow = inLow
    tmpHi = inHi
    'Calculate pivot
    pivot = vArray((inLow + inHi) \ 2)
    'Loop while lower bound is not higher than upper bound
    While (tmpLow <= tmpHi)
        'Increment lower bound loop
        While (vArray(tmpLow) < pivot And tmpLow < inHi)
            tmpLow = tmpLow + 1
        Wend
        'Decrement upper bound loop
        While (pivot < vArray(tmpHi) And tmpHi > inLow)
            tmpHi = tmpHi - 1
        Wend
        'Sort items
        If (tmpLow <= tmpHi) Then
            tmpSwap = vArray(tmpLow)
            vArray(tmpLow) = vArray(tmpHi)
            vArray(tmpHi) = tmpSwap
            tmpLow = tmpLow + 1
            tmpHi = tmpHi - 1
        End If
    Wend
    'Recursive function calls
    If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
    If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi
End Sub
'///////////////////////////////////////END/////////////////////////////////////////
Special thanks to Paul and Greg for the help on this one!
Reply With Quote
Reply

Tags
merge tables, multiple open, quirks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining merge order Determining file path for linked image in Word 2007 samhdc Word 7 01-26-2015 04:21 PM
Determining merge order Reverse Order for Flashcards? bknollman3 PowerPoint 2 03-23-2013 06:34 AM
Determining merge order Word Merge from Excel not in same order! Coreysan Mail Merge 3 12-11-2011 04:22 PM
Determining merge order Contact order djchapple Outlook 5 09-08-2011 11:42 AM
Determining merge order Order of Service sparky3883 Word 1 11-17-2008 11:11 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft