printing to a certain printer
i have this code :
VB Code:
Sub Command1_Click()
Set IESource = New InternetExplorer
Set IETarget = New InternetExplorer
With IESource
.navigate "http://www.walla.co.il"
.Visible = True
End With
End Sub
Private Sub IESource_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (pDisp Is IESource) Then
Dim a As HTMLAnchorElement
IESource.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
For Each a In IESource.document.anchors
bolContinue = False
IETarget.navigate a.href
Do While Not bolContinue
DoEvents
Loop
Next
End If
End Sub
which prints a loaded html page into default printer!
how an i tell it to print to another printer that is installed and connected?
thnaks i nadvance
peleg
Re: printing to a certain printer
Use common dialog control... put one of the form name is cd1 then simple before the print out actually takes place enter cd1.showprinter select the printer you want to print to then click print.
Take care
Re: printing to a certain printer
but all the point is hat i DONT!!! want to promt the user for the printer
i want to put it by myself!
by the way : i didnt understand execlly your suggestion , so just for knowledge can you explain execly what to do?
thnaks in advance
peleg
Re: printing to a certain printer
whether you use the commondialog or not you still have to withe the code
the basic idea is to loop through all the printers available to select the one you want
Dim X As Printer
For Each X In Printers
If X.Orientation = vbPRORPortrait Then
' Set printer as system default.
Set Printer = X
' Stop looking for a printer.
Exit For
End If
Next
depanding on how you want to select the printer you want use, you might use if x.devicename = "myprintername" then set printer = X
hope this helps pete
Re: printing to a certain printer
will this line :
will set the default printer only for the time the exe is running
or it will change the computer default printer - as like i myself and cahnged the default printer?
thnaks in advance
peleg
Re: printing to a certain printer
this changes the printer for your program
it does not change the windows default printer for all programs,
you should have no need to change it back
pete
Re: printing to a certain printer
Sorry if your printing is actually done by internet explorer, then you will need to change the windows default printer for the duration of your printing, then change back afterwards
pete
Re: printing to a certain printer
to change default printer is different in windows 9x to XP / NT windows
in sub
VB Code:
p1 = Printer.DeviceName
p2 = Printer.DeviceName & "," & Printer.DriverName & "," & Printer.Port
For Each p3 In Printers
If Left(p3.DeviceName, 11) = "ElectraSoft" Then Exit For 'p.DeviceName
Next
SetDefaultPrinter p3.DeviceName 'for windows 9x
p4 = p3.DeviceName & "," & p3.DriverName & "," & p3.Port
r = WriteProfileString("windows", "Device", p4) 'for XP/NT
for XP/Nt you need the writeprofilestring api
for windows 9x you need all the other code below, which i have in a module
VB Code:
Type PRINTER_INFO_5
pPrinterName As String
pPortName As String
Attributes As Long
DeviceNotSelectedTimeout As Long
TransmissionRetryTimeout As Long
End Type
''''''Type PRINTER_INFO_3
''''''pSecurityDescriptor As SECURITY_DESCRIPTOR
''''''End Type
Public Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Public Const PRINTER_ACCESS_ADMINISTER As Long = &H4
Public Const PRINTER_ACCESS_USE As Long = &H8
Public Const PRINTER_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
Public Const PRINTER_ATTRIBUTE_DEFAULT As Long = &H4
Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, hPrinter As Long, ByVal pDefault As Long) As Long
Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, lpPrinter As Any, ByVal Command As Long) As Long
Declare Function ClosePrinter Lib "winspool.drv" (ByVal hKey As Long) As Long
Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
Function SetDefaultPrinter(pname) As Integer
Dim lpPrinter As PRINTER_INFO_5, dwLevel&, dwCommand&, Res&
Dim iOK&, hKey&, p As Printer
'Const PRINTER_ATTRIBUTE_DEFAULT As Long = &H4
For Each p In Printers
If p.DeviceName = pname Then Exit For
Next
dwLevel = 5
dwCommand = 1
With lpPrinter
.pPrinterName = pname
.pPortName = p.Port
.Attributes = PRINTER_ALL_ACCESS 'PRINTER_ATTRIBUTE_DEFAULT
.DeviceNotSelectedTimeout = 15
.TransmissionRetryTimeout = 45
End With
' ** If return value = 0 then function call generally unsuccessful **
Res = OpenPrinter(lpPrinter.pPrinterName, hKey, 0)
If Res <> 0 Then
iOK = SetPrinter(hKey, dwLevel, lpPrinter, dwCommand)
End If
Res = ClosePrinter(hKey)
If iOK <> 0 Then
SetDefaultPrinter = -1 ' True
End If
End Function
i think i copied everything in, cos i had a lot of other stuff in that module
don't forget to declare all variables as required
rgds pete
Re: printing to a certain printer
there is a problem with
VB Code:
p1 = Printer.DeviceName
p2 = Printer.DeviceName & "," & Printer.DriverName & "," & Printer.Port
For Each p3 In Printers
If Left(p3.DeviceName, 11) = "ElectraSoft" Then Exit For 'p.DeviceName
Next
SetDefaultPrinter p3.DeviceName 'for windows 9x
p4 = p3.DeviceName & "," & p3.DriverName & "," & p3.Port
r = WriteProfileString("windows", "Device", p4) 'for XP/NT
beacuse if you exit the loop p3 looses is data
Re: printing to a certain printer
He does an exit so that P3 DOES NOT lose its data.
If it finished, it would lose the correct value.
Re: printing to a certain printer
well i debuged it
found the printer i want and when i go to the SetDefaultPrinter line
i get an error and check and see that p3 is empty
Re: printing to a certain printer
Here is my code, called from clicking on a text box.
VB Code:
Private Sub txtPrinter_Click()
Dim prtPrinter As Printer
Dim oCDBForm As New frmCommonDialog
x = lblSetup(1).Left + lblSetup(1).Width
y = lblSetup(1).Top
oCDBForm.Move x, y
With oCDBForm.CommonDialog1
.DialogTitle = "Select Printer to use"
.Flags = cdlPDNoPageNums _
Or cdlPDNoSelection _
Or cdlPDPrintSetup _
Or cdlPDReturnDC
.ShowPrinter
txtPrinter.Text = Printer.DeviceName
End With
For Each prtPrinter In Printers
If prtPrinter.DeviceName <> txtPrinter.Text Then
Set Printer = prtPrinter
txtPrinter.Text = prtPrinter.DeviceName
Exit For
End If
Next
Unload oCDBForm
Set oCDBForm = Nothing
End sub