npnp
asked on
Print to file
I have a VB app which produces a rather complicated printed page, with a mixture of graphics and text. I would like to "print to a file" instead of directly to the printer.
Prior to all references to the printer object, I have:
CommonDialog1.PrinterDefau lt = True
CommonDialog1.ShowPrinter
And the dialog comes up with the "print to file" check box visible. I check the box, the program continues, but still prints to the printer.
I would expect to be asked for a file name.
What is the correct way to do this? I appreciate your help. -- Norm
Prior to all references to the printer object, I have:
CommonDialog1.PrinterDefau
CommonDialog1.ShowPrinter
And the dialog comes up with the "print to file" check box visible. I check the box, the program continues, but still prints to the printer.
I would expect to be asked for a file name.
What is the correct way to do this? I appreciate your help. -- Norm
You may be able to control this behavior by
Settin the PrinterDefault to False
Before opening CommonDialog1
Settin the PrinterDefault to False
Before opening CommonDialog1
The common dialog you are using is a means of getting information from the user about their intentions for the print job. One reason for the commondialog is to allow the user to make selections specific to each print device.
The CommonDialog then stores the information that the user selected. I beleive that you are to take some of that information and write code to allow for the user selections.
For example, if the user decides to print only pages 1 and 2 of a 5 page report. The CommonDialog will collect the information, but then you will have to tell the printer to only print pages 1 and 2.
In the same way, you need to see if the user checked the 'Print to file' box, and if so, write code to print it to file.
The above posted link is a good reference.
hth
The CommonDialog then stores the information that the user selected. I beleive that you are to take some of that information and write code to allow for the user selections.
For example, if the user decides to print only pages 1 and 2 of a 5 page report. The CommonDialog will collect the information, but then you will have to tell the printer to only print pages 1 and 2.
In the same way, you need to see if the user checked the 'Print to file' box, and if so, write code to print it to file.
The above posted link is a good reference.
hth
ASKER
Thank you both for your comments. I am hung up on what to do if the "PrintToFile" is checked:
===============
CommonDialog1.Flags = cdlPDPrintToFile Or etc etc ---
--other CommonDialog things go here
CommonDialog1.ShowPrinter
If cdlPDPrintToFile = True Then
-------what goes here????
Else
' all my existing stuff, as for instance:
Printer.Line (x1, y1)-(x2, y2)
' and such
End If
=================
I need help at the "what goes here" place, above.
The intention is to create a file (Word document best choice??) that I can send to somebody else via an e-mail attachment.
I'm thinking a better way would be:
====================
If cdlPDPrintToFile = True Then
printer object is replaced by a "destination" file
Else
just use the printer object as is.
End If
===================
Can that be done?
-- Norm
===============
CommonDialog1.Flags = cdlPDPrintToFile Or etc etc ---
--other CommonDialog things go here
CommonDialog1.ShowPrinter
If cdlPDPrintToFile = True Then
-------what goes here????
Else
' all my existing stuff, as for instance:
Printer.Line (x1, y1)-(x2, y2)
' and such
End If
=================
I need help at the "what goes here" place, above.
The intention is to create a file (Word document best choice??) that I can send to somebody else via an e-mail attachment.
I'm thinking a better way would be:
====================
If cdlPDPrintToFile = True Then
printer object is replaced by a "destination" file
Else
just use the printer object as is.
End If
===================
Can that be done?
-- Norm
Sure, looks fine. That link I gave you has something very similar. I would look there for reference.
ASKER
Here is a little test. The two code lines which are incorrect are indicated. What is the right way to do this please?
========================== =======
Private Sub Command1_Click()
Dim strOutFile As String
Dim myFSO As Object
strOutFile = "C:\junk\try.doc"
CommonDialog1.Flags = cdlPDPrintToFile
CommonDialog1.PrinterDefau lt = True
CoimmonDialog1.ShowPrinter
If cdlPDPrintToFile = True Then ' INCORRECT: this is a constant. How do I tell
if the PrintToFile box is checked?
Set myFSO = CreateObject(strOutFile) ' INCORRECT. How is this done?
Else
Set my FSO = Printer
End If
myFSO.currentX = and so forth and so forth
myFSO.EndDoc
Set myFSO = Nothing
==========================
Private Sub Command1_Click()
Dim strOutFile As String
Dim myFSO As Object
strOutFile = "C:\junk\try.doc"
CommonDialog1.Flags = cdlPDPrintToFile
CommonDialog1.PrinterDefau
CoimmonDialog1.ShowPrinter
If cdlPDPrintToFile = True Then ' INCORRECT: this is a constant. How do I tell
if the PrintToFile box is checked?
Set myFSO = CreateObject(strOutFile) ' INCORRECT. How is this done?
Else
Set my FSO = Printer
End If
myFSO.currentX = and so forth and so forth
myFSO.EndDoc
Set myFSO = Nothing
Ok for the FSO
You want to add this 'Reference' to your project
Goto Project > References and check the box for..
"Microsoft Scripting Runtime"
Then in code...
Dim objFSO As FileSystemObject
You want to add this 'Reference' to your project
Goto Project > References and check the box for..
"Microsoft Scripting Runtime"
Then in code...
Dim objFSO As FileSystemObject
So tell me how you are getting the data for the file...?
Here is one solution:
If CommonDialog1.flags = cdlPDPrintToFile Then
MsgBox "Print To File"
End If
If CommonDialog1.flags = cdlPDPrintToFile Then
MsgBox "Print To File"
End If
ASKER
"how am I getting data for the file":
As the user progresses through the application, a number of various files are generated. The printing process gathers all these together, and after the user enters some options, the program computes a rather complex layout which includes a lot of "line(x,y)-(x,y)" and a lot of "currentX/currentY" computed coordinates where some text is printed after some complicated formatting, the details of which are also computed.
The whole thing works just fine, and is being used by a growing number of interested people. Now, a publisher is getting tired of using the old paste pot on the printed sheet I mail to her, and is asking for it "electronically", as for instance an e-mail attachment. This means I must print to a file, instead of printing to a printer.
That's what I do not know how to do. It would seem straightforward to simply replace one object (the printer) with another (the output file). But it is more complicated than I had anticipated.
-- Norm
As the user progresses through the application, a number of various files are generated. The printing process gathers all these together, and after the user enters some options, the program computes a rather complex layout which includes a lot of "line(x,y)-(x,y)" and a lot of "currentX/currentY" computed coordinates where some text is printed after some complicated formatting, the details of which are also computed.
The whole thing works just fine, and is being used by a growing number of interested people. Now, a publisher is getting tired of using the old paste pot on the printed sheet I mail to her, and is asking for it "electronically", as for instance an e-mail attachment. This means I must print to a file, instead of printing to a printer.
That's what I do not know how to do. It would seem straightforward to simply replace one object (the printer) with another (the output file). But it is more complicated than I had anticipated.
-- Norm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just a thought....
For email you may find that generating an HTML document is an option. e.g.
dim fh as long
fh=freefile
open "C:\YourFile.HTM" for output as #fh
' open document
print #fh,"<HTML><BODY>"
print #fh, "Print your text"; ' hold line open with ;
print #fh, " add more to a line."
print #fh, "Include graphics <img src='yourpic.jpg'>"
'close document
print #fh,"</BODY></HTML>"
close fh
For email you may find that generating an HTML document is an option. e.g.
dim fh as long
fh=freefile
open "C:\YourFile.HTM" for output as #fh
' open document
print #fh,"<HTML><BODY>"
print #fh, "Print your text"; ' hold line open with ;
print #fh, " add more to a line."
print #fh, "Include graphics <img src='yourpic.jpg'>"
'close document
print #fh,"</BODY></HTML>"
close fh
Hearing...
ASKER
Thanks to everyone.
Actually - I have come to realize that a printer object and a file are not the same kind of object, and hence my naive idea of setting my own object name to one or the other will not work.
I wanted to avoid duplicating a rather large block of printing code, one version writing to a printer, and the other version writing to a file.
I have decided to come at the problem from the other direction. I will print to something like a pdf or jpeg file. After which, the user can print the thing, e-mail it, or wave it at the pelicans.
I am always glad to learn something about a nook and cranny of VB I have not gotten into before.
Thanks again. -- Norm
Actually - I have come to realize that a printer object and a file are not the same kind of object, and hence my naive idea of setting my own object name to one or the other will not work.
I wanted to avoid duplicating a rather large block of printing code, one version writing to a printer, and the other version writing to a file.
I have decided to come at the problem from the other direction. I will print to something like a pdf or jpeg file. After which, the user can print the thing, e-mail it, or wave it at the pelicans.
I am always glad to learn something about a nook and cranny of VB I have not gotten into before.
Thanks again. -- Norm
Take Control of the Printer
http://www.devx.com/upload/free/features/getstarted/1999/su99/pssu99/pssu99.asp