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

Error on opening/printing pdf using shellexecute

Status
Not open for further replies.

neemi

Programmer
May 14, 2002
519
GB
I am using the code below within a vba module to print a pdf document.

I have Adobe Reader 7 installed.

Code:
 lRet = apiShellExecute(hWndAccessApp, strOperation, _
            stFile, vbNullString, vbNullString, lShowHow)

where stFile is set to full path and file name of file to print
strOperation is set to "Print"
and lShowHow is set to WIN_NONE

However when I run the function I get an error do do with adobe reader. stating Adobe reader encountered a problem and needs to close. We are sorry for the inconveinience. etc etc

So I am presuming the code is working correctly but it just does not like adobe. I have reinstalled a fresh copy of Adobe in case there was a problem with it and it still occurs.

I know (or I think) the code works as it should as when I try to print a word doc using the function it does actually send it to the printer.

the full code is below which "opens" a file instead of "Printing" a file and the same error occurs with adobe only.

Code:
Option Compare Database
Option Explicit

Private Declare Function apiShellExecute 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 WIN_NONE = 0           'Doesn't open the file - use with print Operation
Public Const WIN_NORMAL = 1         'Open Normal
Public Const WIN_MAX = 2            'Open Maximized
Public Const WIN_MIN = 3            'Open Minimized

'// If error is below 32 then an error has occurred
Private Const ERROR_SUCCESS = 32&
Private Const ERROR_NO_ASSOC = 31&
Private Const ERROR_OUT_OF_MEM = 0&
Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const ERROR_PATH_NOT_FOUND = 3&
Private Const ERROR_BAD_FORMAT = 11&

Function fHandleFileOpen(stFile As String, strOperation As String, lShowHow As Long)
Dim lRet As Long, varTaskID As Variant
Dim stRet As String

'//
'// ==================================================================================
'// stFile          = full path and file name of file to open
'// strOperation    = vbNullString
'// lShowHow        = WIN_NORMAL, WIN_MAX or WIN_MIN (pls see above)
'// ==================================================================================

    'First try ShellExecute
    lRet = apiShellExecute(hWndAccessApp, strOperation, _
            stFile, vbNullString, vbNullString, lShowHow)
            
    If lRet > ERROR_SUCCESS Then
        stRet = vbNullString
        lRet = -1
    Else
        Select Case lRet
            Case ERROR_NO_ASSOC:
                'Try the OpenWith dialog
                varTaskID = Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " _
                        & stFile, lShowHow)
                lRet = (varTaskID <> 0)
            Case ERROR_OUT_OF_MEM:
                stRet = "Error: Out of Memory/Resources. Couldn't Execute!"
            Case ERROR_FILE_NOT_FOUND:
                stRet = "Error: File not found.  Couldn't Execute!"
            Case ERROR_PATH_NOT_FOUND:
                stRet = "Error: Path not found. Couldn't Execute!"
            Case ERROR_BAD_FORMAT:
                stRet = "Error:  Bad File Format. Couldn't Execute!"
            Case Else:
        End Select
    End If
    fHandleFileOpen = lRet & _
                IIf(stRet = "", strOperation, ", " & stRet)
End Function

Has anyone else had this problem before?

Help appreacited.
Cheers,
Neemi
 
Have you tried putting some wait code after the shellexecute call? A lot of things don't work well when they're spawned off of a process.

something like:

repeat
Process System Messages
Get Exit Code of Shell Process
until process is not active;

?

Only other thing I can think of is to try making sure your parms to the shellexecute call are correct.
 
all seemed to work when using acrobat reader version 5.0 and not 6.0 or 7.0.

strange but true. the same was with a lot of user pc's here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top