Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printer Object Error Handling

Status
Not open for further replies.

hkung

Programmer
Jan 9, 2001
19
0
0
MY
hi all,

I'm using a Printer object to do some printing job in my VB application. However, i need to trap the error when the printer is not available or the printer settings is not right. I tried to put in some error catcher, but it seems that the code will go on executing before the printer object returns an error. Most of the time, the following codes will have finished executing before the error is trapped. My question is, how can i make sure that the printer is available? Is it any codes that i can use to check the printer availability before the codes proceed to the next statements? Thanks in advance.


Rgds,
hkung
 
Get Default Printer Name

'Add a module to your project (In the menu choose Project -> Add Module, Then click Open)
'Insert this code to the module :

Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey _
As Long, ByVal lpSubKey As String, ByVal dwReserved As Long, ByVal samDesired _
As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName$, ByVal lpdwReserved As Long, _
lpdwType As Long, lpData As Any, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Public Const HKEY_CURRENT_CONFIG As Long = &H80000005

'Insert the following code to your form:

Function RegGetString$(hInKey As Long, ByVal subkey$, ByVal valname$)
Dim RetVal$, hSubKey As Long, dwType As Long, SZ As Long
Dim R As Long
RetVal$ = ""
Const KEY_ALL_ACCESS As Long = &HF0063
Const ERROR_SUCCESS As Long = 0
Const REG_SZ As Long = 1
R = RegOpenKeyEx(hInKey, subkey$, 0, KEY_ALL_ACCESS, hSubKey)
If R <> ERROR_SUCCESS Then GoTo Quit_Now
SZ = 256: v$ = String$(SZ, 0)
R = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ)
If R = ERROR_SUCCESS And dwType = REG_SZ Then
RetVal$ = Left$(v$, SZ - 1)
Else
RetVal$ = &quot;--Not String--&quot;
End If
If hInKey = 0 Then
R = RegCloseKey(hSubKey)
End If
Quit_Now:
RegGetString$ = RetVal$
End Function


Private Sub Form_Load()
Dim GetCurrPrinter As String
GetCurrPrinter = RegGetString$(HKEY_CURRENT_CONFIG, _
&quot;System\CurrentControlSet\Control\Print\Printers&quot;, &quot;Default&quot;)
MsgBox GetCurrPrinter
End Sub

Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX.

Download Demo version on my Site:
Promotions before 02/28/2001 (free source codebook),visite my site
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top