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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print to XPS DW without filename prompt

Status
Not open for further replies.

HughLerwill

Programmer
Nov 22, 2004
1,818
GB
I'd like to produce reports created using standard vb6 print commands to an XPS file with a specified name, without the the Microsoft XPS Document Writer prompting me for a file name.
The Microsoft XPS Document Writer printer object will accept a file name as exemplified in the following code without prompting for a filename as long we stick to api based print calls, as soon as a standard vb6 print call is made the filename prompt pops up. Is there a way to get filename into DOCINFO and then use vb6 print commands?

Code:
Option Explicit

'ref [URL unfurl="true"]http://allapi.mentalis.org/apilist/A9598B51B969935335C537D163F055AF.html[/URL] and others

Private Type DOCINFO
    cbSize As Long
    lpszDocName As String
    lpszOutput As String
    lpszDatatype As String
    fwType As Long
End Type
 
Private Declare Function StartDoc Lib "gdi32" Alias "StartDocA" (ByVal hdc As Long, lpdi As DOCINFO) As Long
Private Declare Function StartPage Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Declare Function EndPage Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function EndDoc Lib "gdi32" (ByVal hdc As Long) As Long

Private Sub Command1_Click()
    
    Dim p As Printer
    For Each p In Printers()
        If p.DeviceName = "Microsoft XPS Document Writer" Then Set Printer = p
    Next
    
    Dim DI As DOCINFO
    With DI
        .cbSize = Len(DI)
        .lpszDocName = "My Document Name"
        .lpszOutput = CurDir$ & "\MyFile.xps"
        .lpszDatatype = vbNullString
    End With
    
    StartDoc Printer.hdc, DI
    StartPage Printer.hdc
    TextOut Printer.hdc, 500, 300, "Hello World", 12
    
    'all goes well unless we start doing vb6 print commands like this
    Printer.Print "Goodbye World"      'causes the file name dialog to be displayed
    
    EndPage Printer.hdc
    EndDoc Printer.hdc
    
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top