From: Simone Bosio on
Hi everybody.

With VB6, how can i print to specific printer?
I need to print a Microsoft Data Report to a specific printer "PDFCreator"
to build a PDF file without prompt information to user.
I tried to do this with:
- Read default printer
- Change default printer
- Print
- Set default printer to original default printer
(but this doesn't work with Domain User account - only Adminisrator in
Windows Server 2008 R2 64bit Terminal Services)

So, anyone know how can i print in VB6 directly to "PDFCreator" printer my
Microsoft Data Report?

Thank you

Simone


From: Helmut Meukel on
"Simone Bosio" <simone.bosio(a)evolit.net> schrieb im Newsbeitrag
news:91E2792C-6A00-4853-ADFB-766038DCB5F4(a)microsoft.com...
> Hi everybody.
>
> With VB6, how can i print to specific printer?
> I need to print a Microsoft Data Report to a specific printer "PDFCreator" to
> build a PDF file without prompt information to user.
> I tried to do this with:
> - Read default printer
> - Change default printer
> - Print
> - Set default printer to original default printer
> (but this doesn't work with Domain User account - only Adminisrator in Windows
> Server 2008 R2 64bit Terminal Services)
>
> So, anyone know how can i print in VB6 directly to "PDFCreator" printer my
> Microsoft Data Report?
>
> Thank you
>
> Simone
>


Hmmm,
and how *exactly* did you try this?
Show code please!
And some more info about your environment, please.
Where is your app running? In the context of a
user using Terminal Services?

Helmut.



From: Simone Bosio on
> And some more info about your environment, please.
> Where is your app running? In the context of a
> user using Terminal Services?

The VB6 application run on a Windows Server 2008 R2 64bit with Remote
Desktop Services role enabled.
With a non-admin user, this scenario and the following code, the default
printer doesn't change.
Here's what i like in order of preference:
- Or a dll/tool to transform Microsoft Data Report to PDF
- Or a function to print directly to "PDFCreator" printer (without change
default printer and restore it after print)
- Or other suggestions

> Hmmm,
> and how *exactly* did you try this?
> Show code please!

Friend Function SetPrinter(ByRef NomePrt As String) As Boolean
Dim Buffer As String
Dim DriverName As String
Dim PrinterPort As String
Dim r As Long
If NomePrt <> "" Then
Buffer = Space(1024)
r = GetProfileString("PrinterPorts", NomePrt, "", Buffer,
Len(Buffer))
Call GetDriverAndPort(Buffer, DriverName, PrinterPort)
If DriverName <> "" And PrinterPort <> "" Then
SetPrinter = SetDefaultPrinter(NomePrt, DriverName, PrinterPort)
Else
SetPrinter = False
End If
End If
End Function

Private Sub GetDriverAndPort(ByVal Buffer As String, ByRef DriverName As
String, ByRef PrinterPort As String)
Dim iDriver As Integer
Dim iPort As Integer
DriverName = ""
PrinterPort = ""
'Il nome del driver � la prima stringa delimitata dalla ","
iDriver = InStr(Buffer, ",")
If iDriver > 0 Then
DriverName = Left(Buffer, iDriver - 1)
'Il numero di porta � tra le due ","
iPort = InStr(iDriver + 1, Buffer, ",")
If iPort > 0 Then
PrinterPort = Mid(Buffer, iDriver + 1, iPort - iDriver - 1)
End If
End If
End Sub

Private Function SetDefaultPrinter(ByVal DeviceName As String, ByVal
DriverName As String, ByVal PrinterPort As String) As Boolean
Dim DeviceLine As String
Dim r As Long
DeviceLine = DeviceName & "," & DriverName & "," & PrinterPort
r = WriteProfileString("windows", "Device", DeviceLine)
If r Then
SetDefaultPrinter = True
Else
SetDefaultPrinter = False
End If
End Function

> Helmut.

Thank you very much

Simone


From: David Youngblood on
"Simone Bosio" <simone.bosio(a)evolit.net> wrote in message
news:e9y03qTELHA.1868(a)TK2MSFTNGP05.phx.gbl...
>> And some more info about your environment, please.
>> Where is your app running? In the context of a
>> user using Terminal Services?
>
> The VB6 application run on a Windows Server 2008 R2 64bit with Remote
> Desktop Services role enabled.
> With a non-admin user, this scenario and the following code, the default
> printer doesn't change.
> Here's what i like in order of preference:
> - Or a dll/tool to transform Microsoft Data Report to PDF
> - Or a function to print directly to "PDFCreator" printer (without change
> default printer and restore it after print)
> - Or other suggestions
>
>> Hmmm,
>> and how *exactly* did you try this?
>> Show code please!
>
> Code snipped <

Win2000 added an API that directly sets the default printer by name. I've
test on Win7, you might give it a try,

Option Explicit

Private Declare Function GetDefaultPrinter Lib "winspool.drv" _
Alias "GetDefaultPrinterA" ( _
ByVal lpPrinterName As String, _
pcbBytes As Long) As Long

Private Declare Function SetDefaultPrinter Lib "winspool.drv" _
Alias "SetDefaultPrinterA" ( _
ByVal lpPrinterName As String) As Long

Private Sub Command1_Click()

Dim sPrinter As String

On Error Resume Next

'* Get current default printer
sPrinter = Space(255)
If GetDefaultPrinter(sPrinter, Len(sPrinter)) Then
sPrinter = TrimNulls(sPrinter)
Debug.Print sPrinter
End If

'* Set new default printer
If SetDefaultPrinter("Microsoft XPS Document Writer") Then
'* Do print stuff
Printer.Print "Hello World"
'* Restore previous default printer
Call SetDefaultPrinter(sPrinter)
End If

End Sub

Private Function TrimNulls(ByVal sBuffer As String) As String
Dim iPos As Long
iPos = InStr(sBuffer, vbNullChar)
TrimNulls = Left$(sBuffer, iPos - 1)
End Function


David