si666
01-30-2017, 05:16 AM
I have the following code (not all my own work I would like to add) which I use to collect data from legacy form fields from word documents which all sit in one folder and can have any name.
Sub GetFormData()
'Note: this code requires a reference to the Word object model
'To do this, go to Tools|References in the VBE, then scroll down to the Microsoft Word entry and check it.
Application.ScreenUpdating = False
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim FrmField As Word.FormField
Dim strFolder As String, strFile As String
Dim WkSht As Worksheet, i As Long, j As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set WkSht = ActiveSheet
i = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
i = i + 1
Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
j = 0
For Each FmFld In .FormFields
j = j + 1
WkSht.Cells(i, j) = FmFld.Result
Next
End With
wdDoc.Close SaveChanges:=False
strFile = Dir()
Wend
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
What I would like to do modify the script to achieve two things.
Firstly I would like to change the script so I can point it at a directory and the script will do a recursive search for all word documents with a specific name e.g. log.doc.
Secondly, and this is certainly beyond my current VBA skills, the first filed in each of the word documents contains a unique reference which ends up in column A of the excel sheet. I would like a way in which when I run the process again when a document is encountered which has the specified file name then the value of the first field is checked against the contents of column A of the spread sheet and if it already exists then the file is skipped.
I hope this makes sense.
I know this is a big ask but any tips, pointers or guidence on how to achieve either of these would be greatly appreciated.
Regards
Simon
Sub GetFormData()
'Note: this code requires a reference to the Word object model
'To do this, go to Tools|References in the VBE, then scroll down to the Microsoft Word entry and check it.
Application.ScreenUpdating = False
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim FrmField As Word.FormField
Dim strFolder As String, strFile As String
Dim WkSht As Worksheet, i As Long, j As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set WkSht = ActiveSheet
i = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
i = i + 1
Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
j = 0
For Each FmFld In .FormFields
j = j + 1
WkSht.Cells(i, j) = FmFld.Result
Next
End With
wdDoc.Close SaveChanges:=False
strFile = Dir()
Wend
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
What I would like to do modify the script to achieve two things.
Firstly I would like to change the script so I can point it at a directory and the script will do a recursive search for all word documents with a specific name e.g. log.doc.
Secondly, and this is certainly beyond my current VBA skills, the first filed in each of the word documents contains a unique reference which ends up in column A of the excel sheet. I would like a way in which when I run the process again when a document is encountered which has the specified file name then the value of the first field is checked against the contents of column A of the spread sheet and if it already exists then the file is skipped.
I hope this makes sense.
I know this is a big ask but any tips, pointers or guidence on how to achieve either of these would be greatly appreciated.
Regards
Simon