Consulting

Results 1 to 3 of 3

Thread: Solved: From Excel Macro - Access a Word Document

  1. #1
    VBAX Regular
    Joined
    Jan 2006
    Posts
    20
    Location

    Solved: From Excel Macro - Access a Word Document

    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.

    [vba]
    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
    [/vba]

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

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    WordApp is not visible by default so no need to set the Visible property to False.

    Replace

    [vba]Documents.Open (fileToOpen)[/vba]

    With

    [vba]WordApp.Documents.Open fileToOpen[/vba]

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

    [vba]
    Dim Doc As Object

    Set Doc = WordApp.Documents.Open (fileToOpen)
    [/vba]

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

  3. #3
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •