Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Printing a Word Document to a specific printer and tray using C#

Posted on 2007-11-23
7
12,325 Views
Last Modified: 2013-12-16
Hi,

I am trying to write some code that will print a Microsoft Word Document (or in fact any document on the file system) and allow me to specify a network printer and printer tray (and maybe other settings like duplex, etc.) in C#.NET 2.0.

I would have thought this wasn't an unusual request, but I'm finding it rather difficult and can't seem to find any samples that are doing this.

Any help would be gratefully received!!

Thanks in advance!

Nathan.
0
Comment
Question by:BoiledOnion
  • 3
  • 2
7 Comments
 
LVL 11

Expert Comment

by:Agarici
ID: 20340789
did you try setting the activeprinter on the application?
http://msdn2.microsoft.com/en-us/library/aa168383(office.10).aspx

hth,
A.
0
 

Author Comment

by:BoiledOnion
ID: 20345343
Hi Agarici,

Unfortunately, as far as I am aware, this doesn't allow me to set the tray id, duplex settings, etc. Just the printer name.

Thanks,

Nathan.
0
 
LVL 11

Expert Comment

by:Agarici
ID: 20348598
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:BoiledOnion
ID: 20364051
Hi again Agarici,

Thank you for your response - I had found this link before, but I cannot work out how I would send a word document to it... This is ok for raw data, but not formatted text.

Do you have any thoughts on this?

Thanks again!

Nathan.
0
 
LVL 9

Accepted Solution

by:
WinterMuteUK earned 125 total points
ID: 20450096
Hi Boiled,

I use the following code to print to a given tray and printer, obviously in my case the tray is hard coded, but shouldn't be taxing to change!

Mine does (also) specifically print Word docs via the Word Interop code, not sure how you would cope for images etc, though presumably the trick is still to setup the PrintDocument.

See code snippet.

Cheers
Wint.
    private void Print(_Document wordDoc, string printerName)
    {
        PrintDocument pd = new PrintDocument();
        pd.PrinterSettings.PrinterName = printerName;
        pd.DocumentName = "...Wints' Test Print...";
        PaperSource tray1 = null;
 
        foreach ( PaperSource source in pd.PrinterSettings.PaperSources )
        {
            if( source.SourceName.Trim().Equals( "Tray 1" ) )
            {
                tray1 = source;
                break;
            }
        }
 
        if(tray1 == null)
            return;
 
        int rawKind = Convert.ToInt32( tray1.GetType().GetField( "kind", BindingFlags.Instance | BindingFlags.NonPublic ).GetValue( tray1 ) );
        wordDoc.PageSetup.FirstPageTray = (WdPaperTray) rawKind;
        _wrdApp.ActivePrinter = printerName;
        _wrdApp.Visible = false;
        _wrdApp.PrintOut( ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                          ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
                          ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing );
                   
    }

Open in new window

0
 

Author Closing Comment

by:BoiledOnion
ID: 31410647
Thank you for your input Wint, that was most helpful!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question