PDA

View Full Version : Solved: From Excel Macro - Access a Word Document



wagnet
05-08-2006, 12:07 PM
Geeez.....seems like it should be so easy.

From an Excel macro, I'm trying to:
1) open a Word document
2) put the FormField value into an array
3) close out Word
4) ...then from another macro, use the array values to populate an Excel Spreadsheet

Currently, I'm stuck on the Documents.Open command - runtime error 424.
The Array population routine was copy/paste from a working Word macro.


Dim MyArray(24, 2) As Integer
Sub ConvertSCQ()
Set WordApp = CreateObject("Word.Application")
fileToOpen = Application _
.GetOpenFilename("Word Files (*.doc), *doc")
WordApp.Visible = False
Documents.Open (fileToOpen)
Stop
aRay = 0
aField = 1
Do
If ActiveDocument.FormFields(aField).Name = "State" Then
aRay = aRay + 1
MyArray(aRay, 1) = ActiveDocument.FormFields(aField).Name
MyArray(aRay, 2) = ActiveDocument.FormFields(aField).Result
End If
aField = aField + 1
Loop Until aField = 25
WordApp.Quit SaveChanges:=wdDoNotSaveChanges
Set WordApp = Nothing

End Sub


Edited 9-May-06 by GeekGirlau. Reason: Insert vba tags.

Jacob Hilderbrand
05-08-2006, 12:46 PM
WordApp is not visible by default so no need to set the Visible property to False.

Replace

Documents.Open (fileToOpen)

With

WordApp.Documents.Open fileToOpen

Also, don't use ActiveDocument. Set the document you open to a variable.


Dim Doc As Object

Set Doc = WordApp.Documents.Open (fileToOpen)


The refer to Doc when you want to refer to the document that you opened.

geekgirlau
05-08-2006, 05:27 PM
Wagnet, one of the things that you might want to investigate is Word's ability to save the data only from a form - have a look at "Save the data from a form in Word for use in a database" in the Word Help.