Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Word Document 1

Status
Not open for further replies.

Informatic

Programmer
Oct 4, 2002
34
DE
Hi
How can I open a Word Document...
For example text.doc with VBA code in Access

and

a PowerPoint Document
For example text.ppt with VBA Code in Access
 
I use something along these lines - the function takes one string parameter which is the full path of the file you want to open.

Code:
Function fctnOpenDoc(strDoc as string)
On Error GoTo Err_fctnOpenDoc
    DoCmd.Hourglass True
    Dim RetVal
    RetVal = Shell("winword " & strDoc, vbNormalFocus)
    
Exit_fctnOpenDoc:
    DoCmd.Hourglass False
    Exit Function
    
Err_fctnOpenDoc:
    If Err.Number = 2475 Then
        Resume Next
    Else
        MsgBox Err.Number & ": " & Err.Description
        Resume Exit_fctnOpenDoc
    End If
    
End Function
[pc2]
 
Thanks a lot for the function.

But what have I to do , when my filename is 'Developer Documentation.doc'.
There is a place between Developer and Documentation.
I have tried the function it is not running with such a filename .
 
Hallo,
Here's another method. Pop this lot into a module and then call strOpenFile to open your file, like

strResult=strOpenFile("C:\My Documents\present.ppt")


Code:
'Open any file (application based on file extension)
Type SHELLEXECUTEINFO
  cbSize As Long
  fMask As Long
  hwnd As Long
  lpVerb As String
  lpFile As String
  lpParameters As String
  lpDirectory As String
  nShow As Long
  hInstApp As Long
  lpIDList As Long
  lpClass As String
  hkeyClass As Long
  dwHotKey As Long
  hIcon As Long
  hProcess As Long
End Type
'
Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400
Public Const SW_SHOWNORMAL = 1
Public Const SE_ERR_FNF = 2
Public Const SE_ERR_NOASSOC = 31
'
Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpExecInfo As SHELLEXECUTEINFO) As Long
'
Public Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal _
    dwMilliseconds As Long) As Long
Public Const INFINITE = &HFFFF
Public Const WAIT_TIMEOUT = &H102
'

Public Function strOpenFile(ByVal pstrFilename As String, Optional ByVal pstrParameters As String = "", Optional ByVal pysnWait As Boolean = False) As String
Dim SEI As SHELLEXECUTEINFO
Dim lngRetVal As Long
Dim strResult As String

  strResult = ""
  With SEI
    .cbSize = Len(SEI)
    .fMask = SEE_MASK_NOCLOSEPROCESS ' Or SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
    .hwnd = Application.hWndAccessApp
    .lpVerb = "open"
    .lpFile = pstrFilename
    .lpParameters = pstrParameters
    .lpDirectory = vbNullChar
    .nShow = SW_SHOWNORMAL
    .hInstApp = 0
    .lpIDList = 0
  End With

  lngRetVal = ShellExecuteEx(SEI)

  If lngRetVal = 0 Then
    ' The function failed, so report the error.  Err.LastDllError
    ' could also be used instead, if you wish.
    Select Case SEI.hInstApp
      Case SE_ERR_FNF
        strResult = "The file '" & pstrFilename & "' was not found."
      Case SE_ERR_NOASSOC
        strResult = "No program is associated with '" & pstrFilename & "'"
      Case Else
        strResult = "An unexpected error occured.(" & lngRetVal & ")"
    End Select
  ElseIf pysnWait Then
    ' Wait for the opened process to close before continuing.  Instead
    ' of waiting once for a time of INFINITE, this example repeatedly checks to see if the
    ' is still open.  This allows the DoEvents VB function to be called, preventing
    ' our program from appearing to lock up while it waits.
    Do
      DoEvents
      lngRetVal = WaitForSingleObject(SEI.hProcess, 0)
    Loop While lngRetVal = WAIT_TIMEOUT
  End If
  strOpenFile = strResult
End Function

Hopefully it should be self-explanatory (how to use it, rather than how it works!). It uses Windowses file extension to application mapping to work out which application to use

- Frink

PS. Hopefully I've included everything you need
 
To use the code in my earlier post with a 32-bit filename (which allows for spaces in the filename), replace

Code:
RetVal = Shell("winword " & strDoc, vbNormalFocus)

with

Code:
RetVal = Shell("winword '" & strDoc & "'", vbNormalFocus)

This is a quick and easy method, and should work okay.
[pc2]
 
Hallo,

Just found this FAQ, which would seem to answer your problem:

faq705-1971

Save us reinventing the wheel :)

- Frink
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top