extract data from multiple word forms into excel (revisited)

  • Thread starter Thread starter kyle
  • Start date Start date
K

kyle

I found this post and tried the method described using the following
macro code:

________________________________________

Dim mydoc As Document
Dim target As Document
Dim i As Long


'let user select a path
With Dialogs(wdDialogCopyFile)
If .Display() <> -1 Then Exit Sub
MyPath = .Directory
End With


'strip quotation marks from path
Set target = Documents.Add
If Len(MyPath) = 0 Then Exit Sub


If Asc(MyPath) = 34 Then
MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If


'get files from the selected path
'and insert them into the doc
MyName = Dir$(MyPath & "*.*")
Do While MyName <> ""
Set mydoc = Documents.Open(MyPath & MyName)
For i = 1 To mydoc.FormFields.Count - 1
target.Range.InsertAfter mydoc.FormFields(i).result & vbTab
Next i
target.Range.InsertAfter mydoc.FormFields(i).result & vbCr
mydoc.Close wdDoNotSaveChanges
MyName = Dir$
Loop

__________________________________________

But have recieved an error when trying to run it. The error is:
______________________________
Compile Error:

Invalid outside procedure
______________________________

which then highlights this line of code:
______________________________
With Dialogs(wdDialogCopyFile)
______________________________

I am using excel 2003 on xp pro with all the latest updates and such.

Anyone have an idea why the
 
That error indicates you have missed the Sub MacroName() statement above the
Dim statements, and the EndSub statement at the end.

In other words, the code is not yet encapsulated as a procedure. With those
two lines added top and bottom, your code runs for me.

Cheers


Sorry, last line should read "Anyone have any idea why this is
happening?

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Microsoft MVP, Word and Word for Macintosh. Consultant Technical Writer
Sydney, Australia +61 (0) 4 1209 1410
 
The OP indicated
Will this run in Excel VBA without Dim'ing an instance of the Word
application?

Ed
 
Back
Top