1.

Solve : Call pdf file from visual basic 6.0?

Answer»

Help me!

I like call always other PDF file from visual basic program
What wrong this lines?
Thank you

Private Sub Command1_Click()

Dim iResult As Double
Dim iPath As String
Dim filename
filename = "d:\2009calendar.pdf"
iPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe filename "
iResult = Shell(iPath, vbMaximizedFocus)

End SubQuote

iPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe filename "


Change the line to

Code: [Select]iPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe" & " " & filename
You can't use Variables inside double quotes. Only String values are represented by double quotes. your inserting the filename within the string:

Code: [Select]Private Sub Command1_Click()

Dim iResult As Double
Dim iPath As String
Dim filename
filename = "d:\2009calendar.pdf"

'iPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe filename "
'Fixed line below:
iPath = "C:\Program FIles\Adobe\Acrobat 7.0\Reader\AcroRd32.exe " & filename
iResult = Shell(iPath, vbMaximizedFocus)

End Sub


That should work.

However, while I'm here- how about I GIVE you some suggestions for improvement? As an example, instead of EXECUTING Acrobat reader directly, it's possible to instead execute the pdf file itself, which opens the program that is set to open PDF files.


To do this, Place this code in the Declarations section of the form (the very top, underneath "Option Explicit" if present.

Code: [Select]Private 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

And then place this at the bottom of the rest of your code...
Code: [Select]Private Function ShowFileDefault(FilePath As String, ownerHwnd As Long) As Boolean
Dim dummy As Long

'open the file using the default Editor or viewer.
dummy = ShellExecute(ownerHwnd, "Open", FilePath & Chr$(0), Chr$(0), _
Left$(FilePath, InStr(FilePath, "\")), 0)


ShowFileDefault=Dummy



End Function


As for your original code, to use the new routine:

Code: [Select]Private Sub Command1_Click()

Dim iResult As Double
Dim iPath As String
Dim filename
filename = "d:\2009calendar.pdf"

'replaced this block...
'iPath = "C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe filename "
'Fixed line below:
'iPath = "C:\Program FIles\Adobe\Acrobat 7.0\Reader\AcroRd32.exe " & filename

'iResult = Shell(iPath, vbMaximizedFocus)



'with this line:
ShowFileDefault Filename, Me.Hwnd


End Sub


I hope this helps! Thx all!while using the last code in my project and run i got the message

Private 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

-- i added this option in the general declarations option


Private Sub mnuHelpGeneral_Click()
Dim iResult As Double
Dim iPath As String
Dim filename

filename = App.Path & "\help.pdf"
ShowFileDefault filename, Me.hWnd
End Sub


-- i Added this in the in the help click option

Private Function ShowFileDefault(FilePath As String, ownerHwnd As Long) As Boolean
Dim dummy As Long

'open the file using the default Editor or viewer.
dummy = ShellExecute(ownerHwnd, "Open", FilePath & Chr$(0), Chr$(0), _
Left$(FilePath, InStr(FilePath, "\")), 0)


ShowFileDefault = dummy



End Function


-- i added this in the bottom of the code

but while i run the source and got error messages


ByRef argument type mismatch


please help me to solve this problemCode: [Select]Dim filename

dimensions a Variant type variable. ShowFileDefault wants a String. Either change the Dim to say:

Code: [Select]Dim filename as String


Or, optionally, change the showFiledefault declaration to:
Code: [Select]Private Function ShowFileDefault(Byval FilePath As String, ownerHwnd As Long) As Boolean


which will attempt to convert the given argument (in this case, a variant) to the required string.
Code: [Select] dummy = ShellExecute(ownerHwnd, "Open", FilePath & Chr$(0), Chr$(0), _
Left$(FilePath, InStr(FilePath, "\")), 0)
and if the 5th argument (lpDirectory) means WORKING directory, then InStrRev should be used instead of InStr that returns the root drive??Or, just leave it blank. That works too.

Probably change the 0 to vbnormalFocus as well; 0 appears to make it appear on the taskbar but not be able to display the window to the program.

I prefer ShellExecuteEx myself, actually.yup, correct, that works too.
from msdn reference:
http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx
Quote
lpDirectory
[in] A pointer to a null-terminated string that specifies the default (working) directory for the action. If this value is NULL, the current working directory is used. If a relative path is provided at lpFile, do not use a relative path for lpDirectory.
nShowCmd
[in] The flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file, the flag is simply passed to the associated application. It is up to the application to decide how to handle it.
SW_HIDE
SW_SHOWNORMAL

ShellExecuteEx API looks complicated, but seems to provides more control on the created process. do you have a wrapper module for ShellExecuteEx?It's not really that complicated...

Here's one that uses it to show the Open With dialog for any file:

declarations:
Code: [Select]Private Declare Function GetCurrentProcess Lib "kernel32.dll" () As Long

Private Declare Function ShellExecuteEx Lib "shell32.dll" (ByRef lpExecInfo As SHELLEXECUTEINFOA) As Long
Private Type SHELLEXECUTEINFOA
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
' fields
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type


Code:
Code: [Select]Public Function OpenWith(ByVal HwndOwner As String, ByVal FilePath As String, Optional ByVal Icon As Long = 0) As Long

Dim sei As SHELLEXECUTEINFOA
Dim verba As String
verba = "openas"
sei.hIcon = Icon
sei.hProcess = GetCurrentProcess()
sei.hInstApp = App.hInstance

sei.lpVerb = verba
sei.lpFile = FilePath
sei.nShow = vbNormalFocus
sei.cbSize = Len(sei)

OpenWith = ShellExecuteEx(sei)


'Stop

End Function

'another option, would be to allow for the verb to be specified as an argument, along with several other PORTIONS of the structure.


Discussion

No Comment Found