13.5. Err.LastDLLError

Like the VBA procedures you write, API procedures can also generate errors. These can be the result of bad or missing data, invalid data type assignments, or a variety of other conditions or failures. This section describes how to trap and retrieve these API-generated errors, so you can take remedial or other action to shield the user from their adverse effects.

LastDLLError is a property of the VBA Err object. It returns the error code produced by a call to a DLL, and always contains zero on systems that don't have DLLs (like the Macintosh).

DLL functions usually return a code that indicates whether the call succeeded or failed. Your VBA code should check the value returned after a DLL function is called and, on detecting a failure code, should immediately check the LastDLLError property and take whatever action you deem necessary. The DLL's documentation will indicate which codes to check for.

Since no exception is raised when the LastDLLError property is set, you cannot use the On Error Goto construct; therefore, use On Error Resume Next.

Modify the SetFormIcon procedure to generate a DLL error by passing an empty string as the icon path. This shows the LastDLLError property in action.

Public Sub SetFormIcon(hWnd As Long, strIcon As String)
    Dim hIcon As Long
    Dim lngReturn As Long

    On Error Resume Next

    'Pass an empty string as the icon path
    hIcon = LoadImage(0&, "", IMAGE_ICON, IMG_DEFAULT_WIDTH, _
        IMG_DEFAULT_HEIGHT, LR_LOADFROMFILE)

    'Now check for an error
    If hIcon <> 0 Then
        lngReturn = SendMessage(hWnd, WM_SETICON, ICON_SMALL, ByVal hIcon)
    Else
        'Dsplay the error
        MsgBox "The last DLL error was: " & Err.LastDllError
    End If
End Sub

Since the error is a DLL-specific error, the Err object's Description property will be empty

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.63.106