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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

launch pdf file

Status
Not open for further replies.

elibb

Programmer
Oct 22, 2001
335
MX
hi.. how can i open a file in acrobat reader from my vb6.0 application??
i tried to use an OLE object, but i couldnt set the file path at run time...

thank you very much
Eli
 
A bit crude but here is what I'm using for an applcation....

'Print Event sheets
Private Sub outevent(strWrdDoc As String)
On Error GoTo err_ev
Dim lngReturnNumber As Long
Dim strPath As String
Dim intLen As Integer
intLen = Len(strRptLoc)
If intNode = 621 Then
strPath = Mid(strRptLoc, 1, intLen - 8) & "documentation\" & strWrdDoc & ".pdf"

ElseIf intNode = 631 Then
strPath = Mid(strRptLoc, 1, intLen - 8) & "documentation\" & strWrdDoc & ".pdf"

ElseIf intNode = 632 Then
strPath = Mid(strRptLoc, 1, intLen - 8) & "documentation\" & strWrdDoc & ".pdf"



Else
strPath = Mid(strRptLoc, 1, intLen - 8) & "documentation\" & strWrdDoc & ".doc"
End If
'Launch File
lngReturnNumber = ShellExecLaunchFile(strPath, "", "")

'Check for Errors
If lngReturnNumber < 33 Then
Call ShellExecLaunchErr(lngReturnNumber, True)
Exit Sub
Exit Sub

err_ev:
Exit Sub

End Sub



====================================more ====================

Option Explicit

Public Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias _
&quot;ShellExecuteA&quot; (ByVal hwnd As Long, ByVal lpszOp As _
String, ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long

Public Declare Function GetDesktopWindow Lib &quot;user32&quot; () As Long

' hWnd = Window handle to a parent window.
' This window receives any message boxes that an application produces.
'
' lpszOp = Address of a null-terminated string that specifies the operation to perform.
' The following operation strings are valid:
' open, print, explore
' This parameter can be NULL. In that case, the function opens the file specified
' by lpFile.
'
' lpFile = Address of a null-terminated string that specifies the file to open
' or print or the folder to open or explore. The function can open an executable
' file or a document file. The function can print a document file.
'
' lpParameters = If the lpFile parameter specifies an executable file,
' lpParameters is an address to a null-terminated string that specifies
' the parameters to be passed to the application.
' If lpFile specifies a document file, lpParameters should be NULL.
'
' lpDirectory = Address of a null-terminated string that specifies
' the default directory.
'
' nShowCmd = If lpFile specifies an executable file, nShowCmd specifies
' how the application is to be shown when it is opened.
' This parameter can be one of the following values:

Const SW_HIDE = 0 ' Hides the window and activates another window.
Const SW_MAXIMIZE = 3 ' Maximizes the specified window.
Const SW_MINIMIZE = 6 ' Minimizes the specified window and activates the next top-level window in the z-order.
Const SW_RESTORE = 9 ' Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
Const SW_SHOW = 5 ' Activates the window and displays it in its current size and position.
Const SW_SHOWDEFAULT = 10 ' Sets the show state based on the SW_ flag specified in theSTARTUPINFO structure passed to theCreateProcess function by the program that started the application. An application should callShowWindow with this flag to set the initial show state of its main window.
Const SW_SHOWMAXIMIZED = 3 ' Activates the window and displays it as a maximized window.
Const SW_SHOWMINIMIZED = 2 ' Activates the window and displays it as a minimized window.
Const SW_SHOWMINNOACTIVE = 7 ' Displays the window as a minimized window. The active window remains active.
Const SW_SHOWNA = 8 ' Displays the window in its current state. The active window remains active.
Const SW_SHOWNOACTIVATE = 4 ' Displays a window in its most recent size and position. The active window remains active.
Const SW_SHOWNORMAL = 1 ' Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.

' If lpFile specifies a document file, nShowCmd should be zero.
' You can use this function to open or explore a shell folder.
' To open a folder, use either
'
' ShellExecute(handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL);
' or
' ShellExecute(handle, &quot;open&quot;, path_to_folder, NULL, NULL, SW_SHOWNORMAL);
'
' To explore a folder, use the following call:
'
' ShellExecute(handle, &quot;explore&quot;, path_to_folder, NULL, NULL, SW_SHOWNORMAL);
'
' If lpOperation is NULL, the function opens the file specified by lpFile. If lpOperation is &quot;open&quot; or &quot;explore&quot;, the function will attempt to open or explore the folder.
'
' To obtain information about the application that is launched as a result of calling
'
' Returns a value greater than 32 if successful, or an error value that is less
' than or equal to 32 otherwise. The following table lists the error values.
' The return value is cast as an HINSTANCE for backward compatibility with 16-bit
' Microsoft® Windows® applications. It is not a true HINSTANCE, however.
' The only thing that can be done with the returned HINSTANCE is to cast it to an
' integer and compare it with the value 32 or one of the error codes below.

Const SE_ERR_FNF = 2 ' File not found
Const SE_ERR_PNF = 3 ' Path not found
Const SE_ERR_ACCESSDENIED = 5 ' Access denied
Const SE_ERR_OOM = 8 ' Out of memory
Const SE_ERR_DLLNOTFOUND = 32 ' DLL not found
Const SE_ERR_SHARE = 26 ' A sharing violation occurred
Const SE_ERR_ASSOCINCOMPLETE = 27 ' Incomplete or invalid file association
Const SE_ERR_DDETIMEOUT = 28 ' DDE Time out
Const SE_ERR_DDEFAIL = 29 ' DDE transaction failed
Const SE_ERR_DDEBUSY = 30 ' DDE busy
Const SE_ERR_NOASSOC = 31 ' No association for file extension
Const ERROR_BAD_FORMAT = 11& ' Invalid EXE file or error in EXE image
Const ERROR_FILE_NOT_FOUND = 2& ' The specified file was not found.
Const ERROR_PATH_NOT_FOUND = 3& ' The specified path was not found.
Const ERROR_BAD_EXE_FORMAT = 193& ' The .exe file is invalid (non-Win32® .exe or error in .exe image).


Public Function ShellExecLaunchFile(ByVal strPathFile As String, ByVal strOpenInPath As String, ByVal strArguments As String) As Long

Dim Scr_hDC As Long

'Get the Desktop handle
Scr_hDC = GetDesktopWindow()

'Launch File
ShellExecLaunchFile = ShellExecute(Scr_hDC, &quot;Open&quot;, strPathFile, &quot;&quot;, strOpenInPath, SW_SHOWNORMAL)

End Function


Public Function ShellExecLaunchErr(ByVal lngErrorNumber As Long, ByVal blnRaiseMsg As Boolean) As String

Dim msg As VbMsgBoxResult
Dim strErrorMessage As String

If lngErrorNumber < 33 Then
'There was an error
Select Case lngErrorNumber
Case SE_ERR_FNF
strErrorMessage = &quot;File not found&quot;
Case SE_ERR_PNF
strErrorMessage = &quot;Path not found&quot;
Case SE_ERR_ACCESSDENIED
strErrorMessage = &quot;Access denied&quot;
Case SE_ERR_OOM
strErrorMessage = &quot;Out of memory&quot;
Case SE_ERR_DLLNOTFOUND
strErrorMessage = &quot;DLL not found&quot;
Case SE_ERR_SHARE
strErrorMessage = &quot;A sharing violation occurred&quot;
Case SE_ERR_ASSOCINCOMPLETE
strErrorMessage = &quot;Incomplete or invalid file association&quot;
Case SE_ERR_DDETIMEOUT
strErrorMessage = &quot;DDE Time out&quot;
Case SE_ERR_DDEFAIL
strErrorMessage = &quot;DDE transaction failed&quot;
Case SE_ERR_DDEBUSY
strErrorMessage = &quot;DDE busy&quot;
Case SE_ERR_NOASSOC
strErrorMessage = &quot;No association for file extension&quot;
Case ERROR_BAD_FORMAT
strErrorMessage = &quot;Invalid EXE file or error in EXE image&quot;
Case ERROR_FILE_NOT_FOUND
strErrorMessage = &quot;The specified file was not found.&quot;
Case ERROR_PATH_NOT_FOUND
strErrorMessage = &quot;The specified path was not found.&quot;
Case ERROR_BAD_EXE_FORMAT
strErrorMessage = &quot;The .exe file is invalid (non-Win32® .exe or error in .exe image).&quot;
Case Else
strErrorMessage = &quot;Unknown error&quot;
End Select

'If the blnRaiseMsg = True then raise a MsgBox with error
If blnRaiseMsg = True Then msg = MsgBox(strErrorMessage, vbCritical, &quot;Error:&quot;)

'Return Error string
ShellExecLaunchErr = blnRaiseMsg

End If

End Function


' So the way to use all this is:
'
' Dim lngReturnNumber As Long
'
' lngReturnNumber = ShellExecLaunchFile(txtPathFile.Text, txtStartPath.Text, txtArguments.Text)
' If lngReturnNumber < 33 Then
' Call ShellExecLaunchErr(lngReturnNumber, True)
' Exit Sub
' End If
'
'==================================================================================
'
'
'Use the following runRegEntry Function to Silently Run .reg Files
'
Public Function runRegEntry(strPathFile As String) As Boolean
On Error GoTo Command1Err

Dim dblTemp As Double
dblTemp = Shell(&quot;regedit.exe /s &quot; & strPathFile, vbHide)

runRegEntry = True

Exit Function

Command1Err:
Dim msg As VbMsgBoxResult

msg = MsgBox(&quot;Error # &quot; & CStr(Err.Number) & &quot; &quot; & Err.Description & vbNewLine & &quot;With: &quot; & strPathFile, vbCritical, &quot;Error:&quot;)
Err.Clear ' Clear the error.
runRegEntry = False

End Function
 
It's a bit easier to use ShellExecute.

In a bas module :
Code:
Public Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias &quot;ShellExecuteA&quot; (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

In your form code:
Code:
Public Sub OpenThisDoc(FormName As Long, FileName As String)
    On Error Resume Next
    Dim X As Long
    X = ShellExecute(FormName, &quot;Open&quot;, FileName, 0&, 0&, SW_SHOWMAXIMIZED)
End Sub

In the event code that you want to use:
Code:
Call OpenThisDoc(Me.hwnd, myFilename)

where myFilename is the variable containing the file you want to open
Let me know if this helps
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
I can't recall where I found this, but you can use it to open any type of document...:)

Public Sub OpenDocument(DocumentWithPath As String)
Shell &quot;RUNDLL32.EXE URL.DLL, FileProtocolHandler &quot; & DocumentWithPath, vbMaximizedFocus
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top