Return codes for error trap

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
I have built a utility that prints a text file from within Excel using the "NotePad" in the background, it works. The error trap may have some problems though. The return integers for the API indicate the success of the API. Above 32 and the API was a success, zero to 32 indicates an error.

The return Error list only indicated the Error Type Description and did not indicate the corresponding integer for the return. On testing I put invalid Drive:\Folder\File.ext information one at a time and even though I should get different return codes [I think?] I got "2" for each error?

Any Ideas on how to improve the Error Trap?

MS Documentation link:

http://msdn.microsoft.com/library/d...rm/Shell/reference/functions/shellexecute.asp



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

'Documentation: Properties and property values!
'Prop: hwnd= Window shows user interface or error messages. NULL = operation not associated with a window.
'Prop: lpOperation= Pointer, null-terminated string, action to performe, available verbs depends on the particular
'file or folder. Generally, the actions available: Are an object's shortcut menu available verbs, commonly used ones are:
'Val: Edit= Launches editor & opens document for editing. If lpFile is not a document file, the function fails.

'Val: explore= Explores the folder specified by lpFile.
'Val: Find= Initiates a search starting from the specified directory.
'Val: open= Opens the file specified by the lpFile parameter. The file can be an executable file,
'a document file, or a folder.

'Val: Print= Prints the document file specified by lpFile. If lpFile is not a document file, the function fails.
'Val: NULL= For systems prior to MS Win 2000, the default verb is used if it is valid and available in the registry.
'If not, the "open" verb is used.

'Note: For Win 2000 & later systems, the default verb is used if available. If not, the "open" verb is used.
'If neither verb is available, the system uses the first verb listed in the registry.
'Prop: lpFile= Pointer to a null-terminated string that specifies the file or object on which to execute the specified verb.
'To specify a Shell namespace object, pass the fully qualified parse name.

'Note: that not all verbs are supported on all objects. Example, not all document types support the "print" verb.
'Prop: lpParameters= If the lpFile parameter specifies an executable file, lpParameters is a pointer to a
'null-terminated string that specifies the parameters to be passed to the application. The format of this string
'is determined by the verb that is to be invoked. If lpFile specifies a document file, lpParameters should be NULL.

'Prop: lpDirectory= Pointer to a null-terminated string that specifies the default directory.
'Prop: nShowCmd= Flags that specify how an application is to be displayed when opened. If lpFile
'specifies a document file, the flag is simply passed to the associated application. The application decides
'how to handle it.

'Val: SW_HIDE= Hides the window and activates another window.
'Val: SW_MAXIMIZE= Maximizes the specified window.
'Val: SW_MINIMIZE= Minimizes the specified window and activates the next top-level window in the z-order.
'Val: SW_RESTORE= Activates and displays the window. If the window is minimized or maximized,
'Windows restores its original size & position. Application should specify flag when restoring a minimized window.

'Val: SW_SHOW= Activates the window and displays it in its current size and position.
'Val: SW_SHOWDEFAULT= Sets show state based on the SW_ flag specified in the STARTUPINFO structure
'passed to the CreateProcess function by the program that started the application. An application should
'call ShowWindow with this flag to set the initial show state of its main window.

'Val: SW_SHOWMAXIMIZED= Activates the window and displays it as a maximized window.
'Val: SW_SHOWMINIMIZED= Activates the window and displays it as a minimized window.
'Val: SW_SHOWMINNOACTIVE= Displays the window as a minimized window. The active window remains active.
'Val: SW_SHOWNA= Displays the window in its current state. The active window remains active.
'Val: SW_SHOWNOACTIVATE= Displays a window in its most recent size and position. The active window
'remains active.

'Val: SW_SHOWNORMAL= Activates and displays a window. If the window is minimized or maximized,
'Windows restores it to its original size & position. An application should specify this flag when
'displaying the window for the first time.




Sub myPrintTxtFile()
'Standard module code, like: Module1!
Dim HinstanceNum%
Dim HinstanceDescrip$, myDrivFolderFileExt$

'Drive:\Folder\TextFile.ext [.txt] to print!
myDrivFolderFileExt = "C:\cp\myControls.txt"

'If no error then print my Text File!
HinstanceNum = ShellExecute(Application.hwnd, "Print", myDrivFolderFileExt, vbNullString, "c:\temp", 0)

If HinstanceNum < 32 Then GoTo myErr
GoTo myEnd

myErr:
'Get Error type!
Select Case HinstanceNum
Case 0
HinstanceDescrip = "0 = The operating system is out of memory or resources."
Case 1
HinstanceDescrip = "ERROR_FILE_NOT_FOUND = The specified file was not found."
Case 2
HinstanceDescrip = "ERROR_PATH_NOT_FOUND = The specified path was not found."
Case 3
HinstanceDescrip = "ERROR_BAD_FORMAT = The "".exe"" file is invalid " & _
"(non-Microsoft Win32 "".exe"" or error in "".exe"" image)."
Case 4
HinstanceDescrip = "SE_ERR_ACCESSDENIED = The operating system denied access to the specified file."
Case 5
HinstanceDescrip = "SE_ERR_ASSOCINCOMPLETE = The file name association is incomplete or invalid."
Case 6
HinstanceDescrip = "SE_ERR_DDEBUSY = Dynamic Data Exchange (DDE) transaction not completed, " & _
"other DDE transactions being processed."
Case 7
HinstanceDescrip = "SE_ERR_DDEFAIL = The DDE transaction failed."
Case 8
HinstanceDescrip = "SE_ERR_DDETIMEOUT = The DDE transaction could not be completed " & _
"because the request timed out."
Case 9
HinstanceDescrip = "SE_ERR_DLLNOTFOUND = The specified DLL was not found."
Case 10
HinstanceDescrip = "SE_ERR_FNF = The specified file was not found."
Case 11
HinstanceDescrip = "SE_ERR_NOASSOC = No application associated with file name extension, file not printable."
Case 12
HinstanceDescrip = "SE_ERR_OOM = There was not enough memory to complete the operation."
Case 13
HinstanceDescrip = "SE_ERR_PNF = The specified path was not found."
Case 14
HinstanceDescrip = "SE_ERR_SHARE = A sharing violation occurred."

Case Else
HinstanceDescrip = "Error Description not found!"
End Select

MsgBox "This Error occurred trying to print your ""txt"" file:" & vbLf & vbLf & _
"Error Number: " & HinstanceNum & ", " & vbLf & HinstanceDescrip, _
vbOKOnly + vbCritical, "Error Printing Text File!"

myEnd:
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I tested your code in a Module with this file and path in place: "C:\cp\myControls.txt"


After this line:
'If no error then print my Text File!

Make the next line a comment:
'HinstanceNum = ShellExecute(Application.hwnd, "Print", myDrivFolderFileExt, vbNullString, "c:\temp", 0)

Add this line:
MsgBox "If no error then print my Text file! HinstanceNum = " & HinstanceNum & " : HinstanceDescrip = " & HinstanceDescrip


Run myPrintTxtFile

Watch the two MsgBox's

Change the "C:\cp\myControls.txt" to "C:\cpa\myControls.txt"

Same response from both paths?????


Have a great day,
Stan
 
Upvote 0
Thanks Stan, that is why I posted:

"On testing I put invalid Drive:\Folder\File.ext information one at a time and even though I should get different return codes [I think?] I got "2" for each error? "

It returns 42 if it prints and if I mess-up any part of the drive, folder file or extention it returns 2, which it should not!

So I don't know what the problem is?
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,069
Members
449,092
Latest member
ipruravindra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top