PDA

View Full Version : How do you extract only specific FormFields from multiple Word documents into Excel?



bangy
08-02-2014, 05:41 AM
Good afternoon,

I have really tried to solve this problem myself through reading hundreds of pages and threads and playing around with code that i have found. Unfortunately, I cannot seem to get the desired result. I have resulted therefore to finding help and i hope not to annoy in the process. I have found very decent code provided by macropod to import form field data from multiple Word documents (templates) into Excel:

Sub CollateForms()
Dim myPath As String
Dim myWord As New Word.Application
Dim myDoc As Word.Document
Dim myField As Word.FormField
Dim n As Long, m As Long
Dim fs, f, f1, fc
Range("A2").Select
myPath = InputBox("mypath")
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(mypath)
Set fc = f.Files
m = 0
For Each f1 In fc
n = 0
MsgBox f1.Name
Set myDoc = myWord.Documents.Open(mypath & "\" & f1.Name)
For Each myField In myDoc.FormFields
ActiveCell.Offset(m, n).Value = myField.Result
n = n + 1
Next
myDoc.Close wdDoNotSaveChanges
m = m + 1
Next
Set myField = Nothing
Set myDoc = Nothing
Set myWord = Nothing
End Sub


If you want all the form fields imported into an Excel document its perfect, however i only want a few of the form fields to be imported. Is this possible?

I would really appreciate any help. The Excel i am using is Microsoft 2010.

Thank you
Kind regards

mike

bangy
08-02-2014, 06:22 AM
I forgot to mention that i have bookmarked the form fields in Word and named them as so:

("txtSurname")
("txtDOB")
("txtReqDate")
("txtURN")

:)

westconn1
08-02-2014, 03:33 PM
try like

for each f in mydoc.formfields
select case f.name
case "txtSurname", "txtDOB", "txtReqDate", "txtURN"
ActiveCell.Offset(m, n).Value = f.Result
end select
n = n + 1
next
if you do not like the default order of the columns, you can use a select for each field name, with a specific value for column in place of n

snb
08-03-2014, 12:18 PM
Sub M_snb()
with getobject("G:\OF\example.doc")
sheet1.cells(1).resize(,4)=array(.formfields("txtsurname"),.formfields("txtDOB"),.formfields("txtReqDate"),.formfields("txtURN"))
.close 0
end with
End Sub

bangy
08-03-2014, 01:56 PM
Thank you very much for your replies, very much appreciated!

I will be trying both of these codes tomorrow.