Results 1 to 5 of 5
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Automation - Opening an existing word doc in VBA (office 2K pro, Win 2K pr

    This is really a VBA question, but I thought you access guys might be able to help...

    The simple question is, how to open a Word document from Access? I am running a simple merge document that pulls data from the db to set up for a preliminary report and have that working fine as a simple merge doc. I wanted to skip some steps and open the mail merge document using a form button in access and found some code to do this, but with specific application to an excel file.

    This code is more than a little arcane as it relies on API messaging to verify whether or not Excel is running and, depending on that, either starting the app and opening the file or going to the opened app and opening the file. I can understand the need to not open more instances of an application than necessary, if that is what the concern is behind the code, but I'm stuck on adapting the code to work with MSWord. I don't understand the code, either... well, not completely.

    There seems to be some "magic words" required to make it work. Here's a real simple code set for testing for Word being opened or not:

    Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowNams As Long) As Long

    Declare Function SendMessage Lib "user32" Alias _
    "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long

    Sub DetectWord()
    Const WM_USER = 1024 <-- no idea what this means!
    Dim hWnd As Long

    hWnd = FindWindow("WDMAIN", 0) <-- I guessed on this keyword, am wrong, apparently
    If hWnd = 0 Then
    MsgBox "Word not running!"
    Else
    SendMessage hWnd, WM_USER + 18, 0, 0 <-- not really sure what the heck this is supposed to do. comments state it will 'enter the running app in the Running Objects Table...
    MsgBox "Word running!"
    End If
    End Sub

    At a minimum, if you know what to use for FindWindow("XLMAIN", 0) to detect MS Word (I tried "WDMAIN") that will help. If there's some less arcane method for doing all this, that would be great too!

    TIA

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Automation - Opening an existing word doc in VBA (office 2K pro, Win 2

    It can be done a lot easier by using Application.FollowHyperlink, or by using the ShellExecute API function. Search this forum for ShellExecute and you'll find several examples.

    If you need to manipulate the document in code after is has been opened, you can use Automation - search for GetObject and Word.Application.

    By the way, the class name for the Word application window is OPUSAPP.

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Automation - Opening an existing word doc in VBA (office 2K pro, Win 2

    thanks! I was planning to attempt some manipulation of the target document in code, so I wanted to use GetObject. In order to implement this, according to the code samples in VBA help, one uses the API functions to check for existing instances of Word first, so i needed that OPUSAPP class name. Is that location-specific? It sure is weird. But it does work. Any idea why they have

    SendMessage hWnd, WM_USER + 18, 0, 0

    I don't want to cause some kind of China Syndrome problem by not using this but I don't understand it. I can detect word without using it, obviously.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Automation - Opening an existing word doc in VBA (office 2K pro, Win 2

    No, you simply do something like this:

    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If objWord Is Nothing Then
    Set objWord = CreateObject("Word.Application")
    If objWord Is Nothing Then
    ' something wrong
    Exit Sub
    End If
    End If
    On Error GoTo ErrHandler ' or whatever

  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Automation - Opening an existing word doc in VBA (office 2K pro, Win 2

    I just threw my hands up and did a hyperlink. Work OK but I can't pass a var to a macro in Word using a hyperlink AFAIK. Anyhow, the hyperlink will work for now.

Posting Permissions

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