G
Greg
I posted the following code today as an example of how you might
collect data from common formfields from a batch of files.
Basically it gets the names and a count of the files from the common
directory, builds a table in the active document to display the data,
opens each file and puts the data elements from three fields ( 1. Name
2. Age 3. Registration) into arrays, then outputs the data in the
arrays to the table. As I told the recipient of my earlier post, I am
not sure if the method that i proposed is best or even sound and would
appreciate any comments for improvement.
Spefic questions.
1) Should I attempt to use some sort of multi-dimensional array for
the data elements?
2) Is an array the best way or should I consider collections or even
worse classes. Both fill me with dread as I barely understand the
concepts.
Thanks.
Sub TallyData()
Dim oPath As String
Dim FileArray() As String
Dim oFileName As String
Dim DataArray() As String
Dim AgeArray() As String
Dim RegArray() As String
Dim i As Long
Dim j As Long
Dim oTbl As Word.Table
Dim myDoc As Word.Document
oPath = GetPathToUse
If oPath = "" Then
MsgBox "A folder was not selected"
Exit Sub
End If
'Identify and count files
oFileName = Dir$(oPath & "*.doc")
ReDim FileArray(1 To 1000) 'A number larger the expected number of
replies
'Add file name to the array
Do While oFileName <> ""
i = i + 1
FileArray(i) = oFileName
'Get the next file name
oFileName = Dir$
Loop
'Resize and preserve the array
ReDim Preserve FileArray(1 To i)
Application.ScreenUpdating = False
'Add the data table with headings
ActiveDocument.Tables.Add Selection.Range, i + 1, 3
Set oTbl = ActiveDocument.Tables(1)
With oTbl
.Cell(1, 1).Range.Text = "Name"
.Cell(1, 2).Range.Text = "Age"
.Cell(1, 3).Range.Text = "Registration"
End With
'Resize the data arrays to specific sizes
ReDim DataArray(1 To i)
ReDim AgeArray(1 To i)
ReDim RegArray(1 To i)
For i = 1 To UBound(FileArray)
Set myDoc = Documents.Open(FileName:=oPath & FileArray(i), _
Visible:=False)
With myDoc
DataArray(i) = .FormFields("Text1").Result
AgeArray(i) = .FormFields("Text2").Result
RegArray(i) = .FormFields("Text3").Result
.Close
End With
Next i
i = 1
For i = 1 To UBound(DataArray)
j = i + 1
oTbl.Cell(j, 1).Range.Text = DataArray(i)
oTbl.Cell(j, 2).Range.Text = AgeArray(i)
oTbl.Cell(j, 3).Range.Text = RegArray(i)
Next i
Application.ScreenUpdating = True
End Sub
Private Function GetPathToUse() As Variant
'Get the folder containing the files
'Note uses the "Copy Dialog" which enables the "open" option
With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
GetPathToUse = .Directory
Else
GetPathToUse = ""
Exit Function
End If
End With
If Left(GetPathToUse, 1) = Chr(34) Then
GetPathToUse = Mid(GetPathToUse, 2, Len(GetPathToUse) - 2)
End If
End Function
collect data from common formfields from a batch of files.
Basically it gets the names and a count of the files from the common
directory, builds a table in the active document to display the data,
opens each file and puts the data elements from three fields ( 1. Name
2. Age 3. Registration) into arrays, then outputs the data in the
arrays to the table. As I told the recipient of my earlier post, I am
not sure if the method that i proposed is best or even sound and would
appreciate any comments for improvement.
Spefic questions.
1) Should I attempt to use some sort of multi-dimensional array for
the data elements?
2) Is an array the best way or should I consider collections or even
worse classes. Both fill me with dread as I barely understand the
concepts.
Thanks.
Sub TallyData()
Dim oPath As String
Dim FileArray() As String
Dim oFileName As String
Dim DataArray() As String
Dim AgeArray() As String
Dim RegArray() As String
Dim i As Long
Dim j As Long
Dim oTbl As Word.Table
Dim myDoc As Word.Document
oPath = GetPathToUse
If oPath = "" Then
MsgBox "A folder was not selected"
Exit Sub
End If
'Identify and count files
oFileName = Dir$(oPath & "*.doc")
ReDim FileArray(1 To 1000) 'A number larger the expected number of
replies
'Add file name to the array
Do While oFileName <> ""
i = i + 1
FileArray(i) = oFileName
'Get the next file name
oFileName = Dir$
Loop
'Resize and preserve the array
ReDim Preserve FileArray(1 To i)
Application.ScreenUpdating = False
'Add the data table with headings
ActiveDocument.Tables.Add Selection.Range, i + 1, 3
Set oTbl = ActiveDocument.Tables(1)
With oTbl
.Cell(1, 1).Range.Text = "Name"
.Cell(1, 2).Range.Text = "Age"
.Cell(1, 3).Range.Text = "Registration"
End With
'Resize the data arrays to specific sizes
ReDim DataArray(1 To i)
ReDim AgeArray(1 To i)
ReDim RegArray(1 To i)
For i = 1 To UBound(FileArray)
Set myDoc = Documents.Open(FileName:=oPath & FileArray(i), _
Visible:=False)
With myDoc
DataArray(i) = .FormFields("Text1").Result
AgeArray(i) = .FormFields("Text2").Result
RegArray(i) = .FormFields("Text3").Result
.Close
End With
Next i
i = 1
For i = 1 To UBound(DataArray)
j = i + 1
oTbl.Cell(j, 1).Range.Text = DataArray(i)
oTbl.Cell(j, 2).Range.Text = AgeArray(i)
oTbl.Cell(j, 3).Range.Text = RegArray(i)
Next i
Application.ScreenUpdating = True
End Sub
Private Function GetPathToUse() As Variant
'Get the folder containing the files
'Note uses the "Copy Dialog" which enables the "open" option
With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
GetPathToUse = .Directory
Else
GetPathToUse = ""
Exit Function
End If
End With
If Left(GetPathToUse, 1) = Chr(34) Then
GetPathToUse = Mid(GetPathToUse, 2, Len(GetPathToUse) - 2)
End If
End Function