PDA

View Full Version : Quickest way to read word form fields in excel



gibbo1715
10-18-2006, 02:16 AM
Morning

Can anyone tell me if there is a quicker way to read the contents of some formfields in a word document into excel please

thanks

gibbo

For Each file In Folder.Files
If file.Type = "Microsoft Word Document" Then
On Error Resume Next
Set Doc = AppWrd.Documents.Open(FileName:=file.Path, ReadOnly:=True, _
PasswordDocument:="")
If Doc.FormFields("Check1").Result = 1 Then
range("A2").value = "True"
Else
range("A2").value = "False"
End If
range("A3").value = Doc.FormFields("Text1").Result
Doc.Close False
Set Doc = Nothing
End If
Next file

Charlize
10-18-2006, 03:34 AM
Are all the files in the folder Word documents ?

Charlize

gibbo1715
10-18-2006, 03:40 AM
Are all the files in the folder Word documents ?

Charlize


Charlize

Thanks for looking

There may be other types of file in there but i can set it up so it only contains word files if necessary

Gibbo

gibbo1715
10-19-2006, 02:52 AM
All

Is it possible to read the form fields without opening the document?

Paul

Charlize
10-19-2006, 03:54 PM
One of the versions that I have but I doubt that it will be shorter. I put a custom property in a file named Check1. The files are in folder C:\Data. First thing is to check how many word files and put in some sort of array.

It works (at least i believe) but if you would do it different, let me know.

Charlize

Sub test_word_fields()
Dim WordDoc As Object
Dim WordApp As Object
Dim CustPropVal As Long
Dim fs As FileSearch
Dim messagetext As String
Dim okidoki As Long
Dim i As Long

Set fs = Application.FileSearch
Set WordApp = CreateObject("Word.Application")
fs.NewSearch

With fs
.LookIn = "C:\Data"
.SearchSubFolders = False
.MatchTextExactly = True
.FileType = msoFileTypeWordDocuments
.Execute
End With
If fs.Execute() > 0 Then
MsgBox "There were " & _
fs.FoundFiles.Count & _
" file(s) found."
For i = 1 To fs.FoundFiles.Count
On Error Resume Next
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open(fs.FoundFiles(i))
CustPropVal = WordDoc.CustomDocumentProperties("Check1").Value
If CustPropVal = 1 Then
messagetext = messagetext + fs.FoundFiles(i) & vbCrLf
okidoki = okidoki + 1
End If
WordDoc.Close True
CustPropVal = 0
Next i
WordApp.Quit
If okidoki > 0 Then
MsgBox ("No. files found with Check1 - 1 : " & _
okidoki & vbCrLf & messagetext)
Else
MsgBox "There were no files found."
End If
Else
MsgBox "There were no files found."
End If
End Sub