Results 1 to 4 of 4
  1. #1
    Star Lounger
    Join Date
    Jul 2002
    Location
    Petersburg, Virginia
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Open Word document from Access 97 (sr2)

    I'd like to open a Word document or an Excel spreadsheet by clicking a command button on an Access form. What command/syntax would I use?

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

    Re: Open Word document from Access 97 (sr2)

    One way is to use the WinAPI function ShellExecute:

    Put the following declarations in a standard module:

    <img src=/w3timages/blueline.gif width=33% height=2>
    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long

    Public Const SW_SHOWNORMAL = 1
    Public Const SW_SHOWMAXIMIZED As Long = 3
    <img src=/w3timages/blueline.gif width=33% height=2>

    Use code like this in the OnClick event procedure of the command button:

    <img src=/w3timages/blueline.gif width=33% height=2>
    Dim lngResult As Long

    lngResult = ShellExecute(hWndAccessApp, "Open", _
    "C:WordTest.doc", 0&, 0&, SW_SHOWMAXIMIZED)
    If lngResult <= 32 Then
    MsgBox "Can't open document.", vbExclamation
    End If
    <img src=/w3timages/blueline.gif width=33% height=2>

    Another way to open documents is to create a hyperlink.

  3. #3
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Open Word document from Access 97 (sr2)

    Here is an alternative bit of code:

    Dim wrd As Word.Application
    Set wrd = New Word.Application
    wrd.Visible = True
    wrd.Documents.Open fileName:="fullpathfilename.doc", ReadOnly:=false
    wrd.Documents("filename.doc").Activate
    wrd.ActiveWindow.WindowState = wdWindowStateMaximize


    this requires a reference to Word. In any code window, go to Tools...References and add a reference to Microsoft Word object library.
    Regards
    John



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

    Re: Open Word document from Access 97 (sr2)

    John,

    Your code will certainly work. But if Word is already active, it will start a new instance of Word. This might be a problem if the system is low on resources. You can get around this by using GetObject to test if Word is active, and if not, using CreateObject to start Word. Search for GetObject and/or CreateObject and you'll find examples, if you're interested. If you want to do something with a document in code, this is definitely the best way to do it.

    But for this situation, where the poster only wants to open and display a Word document, it's overkill IMHO.

    The ShellExecute code is the exact equivalent of double clicking a Word document in Windows Explorer.

    Regards,
    Hans

Posting Permissions

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