Public Declare Function GetLastError Lib "kernel32.dll" () As Long
Public 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
Public Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Public Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Public Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Public Const FORMAT_MESSAGE_FROM_STRING = &H400
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Public Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF
'___________________________________
Public Function fGetLastWinError(ByRef lngErrCode As Long, _
ByRef strErrMsg As String) As Boolean
'--- Gets the error code & msg raised by a failed API call and places
' them in the passed in variables
'--- Parameters
' [In/Out]
' lngErrCode: receives the Windows error number
' strErrMsg: receives the Windows error message
'--- Return Value
' returns True if the error code was returned from Windows else False
Dim plngRtn As Long 'Return value
Dim plngBuffLen As Long 'Error msg buffer
Dim pstrRtn As String 'Return value
lngErrCode = 0
strErrMsg = vbNullString
'Get the last error code from Windows and store it
plngRtn = GetLastError()
If Not plngRtn = 0 Then
lngErrCode = plngRtn
'Get the error text from Windows
pstrRtn = Space$(256)
plngBuffLen = 256
plngRtn = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULLPTR, _
lngErrCode, NULLPTR, pstrRtn, plngBuffLen, NULLPTR)
If Not plngRtn = 0 Then
strErrMsg = Left$(pstrRtn, plngRtn)
End If
fGetLastWinError = True
Else
GoTo ExitFail
End If
Exit Function
ExitFail:
lngErrCode = 0
strErrMsg = vbNullString
fGetLastWinError = False
End Function