Error Property, PageCount Property, SnapshotPath Property, and ReadyStateChange Event Examples

The following example uses a Select Case statement in a Snapshot control's ReadyStateChange event procedure to determine the status of the ReadyState property. If a snapshot file is successfully loaded into the control, a message box is displayed showing file path and page count information. If an error occurs loading the snapshot, a custom error handling procedure is called to display information about the error that occurred. At the beginning of the procedure, constants are defined for each of the possible values for the ReadyState property.

Private Sub Snapshot1_ReadyStateChange(ByVal ReadyState As Long)
    Const conSnpNotInitialized = 0
    Const conSnpLoading = 1
    Const conSnpStillDownloading = 2
    Const conSnpDownloadComplete = 4
    Select Case ReadyState
        Case conSnpNotInitialized
            ' Snapshot control is not initialized.
        Case conSnpLoading
            ' Snapshot control is loading.
        Case conSnpStillDownloading
            ' Snapshot control is loaded, but the snapshot
            ' file is still downloading.
        Case conSnpDownloadComplete
            ' Snapshot loading is complete.
        With Snapshot1
            If .Error = 0 Then
                ' Snapshot file successfully loaded into control.
                    Msgbox .SnapshotPath & _
                    " has been successfully loaded into the " _
                    & "Snapshot Viewer control." & vbCrLf & vbCrLf _
                    & "This snapshot file contains " & .PageCount _
                    & " pages."
            Else
                ' An error occurred loading the snapshot file.
                Call HandleSnapError(.SnapshotPath, .Error)
                Exit sub
            End If
        End With
    End Select
End Sub

Sub HandleSnapError(strFile As String, lngError As Long)
    Dim strError As String
    Select Case lngError
        Case 2500
            strError = "Error " & CStr(lngError) & ": Invalid value."
        Case 2501
            strError = "Error " & CStr(lngError) & ": Invalid page."
        Case 2502
            strError = "Error " & CStr(lngError) & _
                ": Missing or invalid snapshot file."
        Case 2503
            strError = "Error " & CStr(lngError) & _
                ": Property is read-only."
        Case 2504
            strError = "Error " & CStr(lngError) & _
                ": Operation canceled by user."
        Case 2505
            strError = "Error " & CStr(lngError) & _
                ": You need a newer version of the " _
                & "Snapshot Viewer control to view this snapshot file."
        Case 2506
            strError = "Error " & CStr(lngError) & _
                ": An error occurred while printing."
        Case 2507
            strError = "Error " & CStr(lngError) & _
                ": An error occurred while opening a snapshot file."
        Case 2508
            strError = "Error " & CStr(lngError) & ": Out of memory."
        Case 2509
            strError = "Error " & CStr(lngError) & _
                ": An error occurred writing to a " _
                & "temporary file while opening a snapshot file."
        Case 2510
            strError = "Error " & CStr(lngError) & _
                ": You have no printer installed, or no printer " _
                & "has been selected as the default."
        Case Else
            strError = "Error " & CStr(lngError) & _
                ": An unknown error occurred."
    End Select

    MsgBox strError & " File: " & strFile
End Sub