Solved

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

Posted on 2007-11-23
7
12,472 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
[X]
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
  • 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
Don't Miss ATEN at InfoComm 2017!

Visit booth #2167 to see the  new ATEN VM3200 32 x 32 Modular Matrix Switch. Other highlights include the VE8950 4K HDMI Over IP Extender, VS1912 12-Port DP Video Wall Media Player  and VK2100 ATEN Control System. Register now with Free Pass Code ATEN288!

 

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Six Sigma Control Plans
Progress

690 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