Log in

View Full Version : Solved: Problems Printing via ShellExecute



CreganTur
10-20-2009, 08:04 AM
In one of the custom classes I made in an application I'm building, I have a Sub that is supposed to print the file that gets passed to it. I placed the following ShellExecute API wrapper in a standard module so it would be universally accessible in my application:

Public 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

Here is the Sub from my class module:

Public Sub ShowMessage()
Dim MessageResponse As Integer
Dim rc As Long

MessageResponse = MsgBox("Do you want to print this Report?" & vbCrLf & vbCrLf _
& vbCrLf & InvoiceMessage, vbInformation + vbYesNo, "Invoice Run Report")

If MessageResponse = vbYes Then
rc = apiShellExecute(hWndAccessApp, "print", TextFile, vbNullChar, _
vbNullChar, 0)
End If

End Sub

The TextFile variable is a Field within the class that holds the filepath of the text file I want to print, which is passed into the object by an earlier function. When I run the application and step through the code, nothing happens- I get no errors and the text file does not print. The filepath is correct and leads to a real file on a shared drive.

I have tested this by moving the code into a regular module and hard-coding in the text file's filepath, but it still doesn't work.

Anyone have any ideas on how to solve this issue?

Thanks:thumb

PS - I have cross-posted this over at VBForums (http://www.vbforums.com/showthread.php?p=3635914#post3635914)

Tommy
10-20-2009, 10:26 AM
Randy I changed this line and it worked for me

rc = apiShellExecute(Application.hWnd, "print", TextFile, vbNullChar, _
vbNullChar, 0)

CreganTur
10-20-2009, 11:22 AM
application.hWnd isn't a valid choice for me- the closest match is Application.hWndAccessApp. What version of Access are you running? I'm using 2003. I'm also trying to test the code from a standard module.

Tommy
10-20-2009, 01:15 PM
LOL I was using Excel.
From the www.allapi.net (http://www.allapi.net)
"· hwnd
Specifies a parent window. This window receives any message boxes that an application produces. For example, an application may report an error by producing a message box."

So armed with this information I would pick a form that I was loading, get the hWnd from it and pass that to the function. Or if that is not an option I would make a "Printing......." form that displays before calling the function and send that hWnd to it.

CreganTur
10-21-2009, 08:45 AM
Finally got everything sorted out.

1) I changed hWndAccessApp to Application.hWndAccessApp
2) I was using forward slashes in my filepath- I changed them to backslashes

now everything works.