Results 1 to 14 of 14

Thread: How to print to file using Printer object

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    How to print to file using Printer object

    Hi

    Can anyone please help me out as to how we can print to file using the printer object?

    Thanks in Advance
    Dex

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to print to file using Printer object

    The Printer Object will send a file to the printer to be printed.

    Are you asking how to do that, or are you asking how to add something to a text file?

  3. #3
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526

    Re: How to print to file using Printer object

    You could install the Generic Text Only printer driver, and set it to print to File, or set whatever printer driver you want to generate output for to print to File instead of to the printer. It might help if we knew exactly what you want to accomplish with this.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to print to file using Printer object

    To add to what JDC said...

    You go into PRINTERS and SETTINGS - go into the properties of the printer...

    Under the PORTS tab you will see FILE: as a port. If you select FILE: then output directed at that printer will go to a file - you will get a dialog box asking for a file name and you will get a .PRN file for output. The print driver associated with that printer will "make" the .PRN file - so it's not going to be a universal file - but rather one associated with that specific printer and driver.

    I would imagine you could set the "FILE:" association with an API call - I've never seen it exposed as a PRINTER object property in VB - but I could be wrong.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: How to print to file using Printer object

    Hi All

    Let me ellaborate on what I am looking for.

    Actually I am creating an application which has a functionality for Printing Reports. This I am doing using the Printer object of VB. The printed reports show Page Headers (which includes Page Number also).

    Now, one more functionality that I need to put into this application is 'Print to File' which print the report to a '.prt' file on the system. I was wondering if we can do this with the same Printer object which can save me some time.
    This something very much like what 'szlamany' has stated. However when I try printing a normal .txt file in my system after setting the PORT as FILE: it creates a file but with some wierd contents ....

    Also, with the FileSystemObjects I don't know how to insert a Page Break and check for beginning of a page to put a header.

    Hope I am able to state my problem better this time.

    Waiting for your help...

    Thanks
    Dex
    Last edited by DexySeeksHelp; Oct 19th, 2005 at 11:27 PM.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to print to file using Printer object

    I copied and made a slight addition to this example from the API Guide. My change is noted and it allows you to tell if Print To File is selected. Note that it does not use the CommonDialog control. You won't need 90% of it but I'll leave that up to you.
    Attached Files Attached Files

  7. #7
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,526

    Re: How to print to file using Printer object

    If what you want is a straight text file that has the report contents, you may need to use the Generic Text Only printer driver. If you use a driver for another printer, say an HP for example, you may find that you also get printer control strings embedded in the file for that printer. That may be what the "weird contents" that you were referring to consist of. Remember that sending a file to the Generic Text Only printer driver will just output the text. If you are using any formatting, like bold fonts or different font sizes or colors, none of that will appear in the file.

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to print to file using Printer object

    I'm confused as to why you are using the FSO (filesystemobject) to create a file.

    You must use the PRINTER object to produce output - either to a printer or to a file (to a file by selecting FILE: as the PORT, or using what MARTINLISS posted - I'm assuming that will select FILE: as the PORT for a PRINTER object).

    The GENERIC TEXT ONLY driver is just another printer driver - but one that removes formatting so that only the TEXT is in the file.

    But it still isn't a .TXT (notepad-like) file.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: How to print to file using Printer object

    Hi All

    Thanks for all the useful information .

    But my problem still lies unresolved. I can't compromise on the formatting applied in the report file and in fact the output needs to be generated as a '.prt' file.

    Any input ont this...

    Thanks again
    Dex

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to print to file using Printer object

    I looked tonight, and couldn't find a generic printer in XP Home. You have to pick a printer off a list, or use an installation disk. It's not the same as it was a few years ago. I wonder if SP2 had anything to do with it. I thought I did it with XP, but it may have been W2K.

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: How to print to file using Printer object

    Hi

    I guess I am not able to make a way out of this problem of printing to file.
    And friends I badly need your help on this.
    I want to use the Printer object to print the contents of a report to a '.prt' file directly. My concerns are also to maintain a partcular font-size and style too.

    So I'll highly thankful if someone can suggest as to how can I execute this print to file option.


    Regards
    DEX

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to print to file using Printer object

    I think several of us have already told you that the method to use is:

    1) Change the PRINTER to PRINT TO FILE
    2) Select that PRINTER from VB
    3) You will get a dialog box asking for a file name
    4) This will create a .PRN file - which is a PRINT FILE associated with that printer

    That is the really important part. You are using the PRINT DRIVER to create the .PRN file - so you will get all your formatting - BOLD and stuff like that.

    Have you tried this yet???

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    8

    Re: How to print to file using Printer object

    Hi szlamany

    Yes, actually I have already tried out those things.
    In fact, I faced a couple of issues with that:
    1. The file that gets generated has a lot of driver information and the formatting is also not maintained any more.
    2. The user of this application may have just one printer installed. Then in that case I'll have to change the Printer Port to "FILE:" programmatically and then once the printing is over I'll have to change it back to the original port. Again here also I'm still looking for a method to change the PORT programmatically!

    So can you please suggest something on these.

    PS: The attached Print.zip has two versions of the same report. 'Printer.MDI' is the printer version of the program where 'PrintToFile' is the one generated using the 'Print To File' option (This can be viewed using wordpad or MS Word).


    Thanks
    DEX
    Attached Files Attached Files

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to print to file using Printer object

    Sounds to me like the only way you are going to get a generic formatted output that can go to any printer is to use PDF.

    PDF print drivers are available for free from places like www.cutepdf.com

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width