Walking through a directory of Word Docs picking out some formfield values

G

Gordon Filby

Hallo,

can anyone help me with this?

I have a directory (say C:\Reviews) full of Word documents (normally about
40). Each has the same structure ie they all consist of filled out forms
containing a number of formfields. I would like to walk through this
directory, grabbing the values of certain fields and stringing them together
before dumping the whole lot into a new Word document.

Any tips, especially how to walk through a directory using VBA, will be
greatly appreciated.

Gordon Filby
 
D

Doug Robbins - Word MVP

Hi Gordon,

The following article will show you how to access all of the files in a
folder

“How to Find & ReplaceAll on a Batch of Documents in the Same Folder” at:

http://www.mvps.org/word/FAQs/MacrosVBA/BatchFR.htm

In place of the Edit/Replace routine in that article, with each document
open, you will need to access the .Result property of the formfields that
you are interested in. I am not sure how you want the information arranged
in the document that you want to create, but you might be better to start
creating that document and write the information to it as each of the files
is opened using something like

Dim SourceFile As String
Dim PathToUse As String
Dim Source As Document
Dim Target As Document

Set Target = Documents.Add
PathToUse = "C:\Test\"
'Set the directory and type of file to batch process
SourceFile = Dir$(PathToUse & "*.doc")
While SourceFile <> ""
'Open document
Set Source = Documents.Open(PathToUse & SourceFile)
Target.Range.InsertAfter Source.FormFields("FFName").Result & vbCr
'repeat as necessary for each formfield
'Close the source document
Source.Close wdDoNotSaveChanges
'Next file in folder
SourceFile = Dir$()
Wend
Target.Activate

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 
G

Gordon Filby

Hallo Doug,

that works a treat.

My running version now looks like this:
***********************************************************
Set Target = Documents.Add(Template:="C:\Documents and Settings" _
& "\Administrator\Application Data\Microsoft\Templates\PrtkllEntwurf.dot")

PathToUse =
"D:\AllBWPStuff\ProjRäte\SitzungNr12July03\RtdG8terVrschlgForms\"
'Set the directory and type of file to batch process

SourceFile = Dir$(PathToUse & "*.doc")
While SourceFile <> ""
'Open document
Set Source = Documents.Open(PathToUse & SourceFile)
Target.Range.InsertAfter Source.FormFields("BWPNr").result & ", " _
& Source.FormFields("ATNachname").result & ", " _
& Source.FormFields("ATAddrLine1").result & vbTab & "(wgf)" _
& vbCrLf & Source.FormFields("Title").result & vbCrLf & vbCrLf & vbCrLf

'repeat as necessary for each formfield
'Close the source document
Source.Close wdDoNotSaveChanges
'Next file in folder
SourceFile = Dir$()
Wend
Target.Activate
****************************************************************

The bit:

Target.Range.InsertAfter Source.FormFields("BWPNr").result & ", " _
& Source.FormFields("ATNachname").result & ", " _
& Source.FormFields("ATAddrLine1").result & vbTab & "(wgf)" _
& vbCrLf & Source.FormFields("Title").result & vbCrLf & vbCrLf & vbCrLf

doesn't look too pretty but it works. One more thing is bothering me - the
constant updating of the taskbar as the docs are deleivered to the target
file. Can this be switched off?
 
D

Doug Robbins - Word MVP

Hi Gordon,

If you wanted to, you could pretty up

Target.Range.InsertAfter Source.FormFields("BWPNr").result & ", " _
& Source.FormFields("ATNachname").result & ", " _
& Source.FormFields("ATAddrLine1").result & vbTab & "(wgf)" _
& vbCrLf & Source.FormFields("Title").result & vbCrLf & vbCrLf &
vbCrLf--

by building a string of each of the components and then insert that string,
but it won't improve performance.

I am not sure what task bar you are referring to. If it's the Windows task
bar, you can right click on that and select its Properties itema and set it
to Autohide.

If it's the Word Status Bar, you could use

Application.StatusBar = ""

or

Application.StatusBar = "Please wait."

or

Application StatusBar = "Go and get a cup of coffee and come back when I'm
finished"

I would do the latter, leaving the status bar alone. <g>

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
Doug Robbins - Word MVP
 
G

Gordon Filby

HI Doug,

I'll give that a try. One more thing - what does dir$ do?

I can find help on Dir but nothing on the latter. It would be nice if it
sorted the files alphatically .

Thanks.

Gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top