VB and VBA Users Source Code: Return the error message associated with Err.LastDllError
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Return the error message associated with Err.LastDllError
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, November 13, 2000
Hits:
727
Category:
Windows API
Article:
The following code will return an error message associated with a particular DLL error number: Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long 'Purpose : Return the error message associated with LastDLLError 'Inputs : [lLastDLLError] The error number of the last DLL error (from Err.LastDllError) 'Outputs : Returns the error message associated with the DLL error number 'Author : Andrew Baker 'Date : 13/11/2000 10:14 'Notes : 'Revisions : Public Function ErrorDescriptionDLL(Optional ByVal lLastDLLError As Long) As String Dim sBuff As String * 256 Dim lCount As Long Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100, FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000 Const FORMAT_MESSAGE_FROM_HMODULE = &H800, FORMAT_MESSAGE_FROM_STRING = &H400 Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000, FORMAT_MESSAGE_IGNORE_INSERTS = &H200 Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF If lLastDLLError = 0 Then 'Use Err object to get dll error number lLastDLLError = Err.LastDllError End If lCount = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, 0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0) If lCount > 2 Then ErrorDescriptionDLL = Left$(sBuff, lCount - 2) 'Remove line feeds End If End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder