Compartilhar via


How to set duplex printing for Microsoft Word Automation clients in C#, VB.Net

Microsoft Word Automation clients cannot set the duplex print flag before a print job starts in Microsoft Office Word. Although a parameter in the PrintOut method indicates that there is support for duplex printing, the parameter does not provide true duplex printing. To work around this limitation on Microsoft Windows systems, you must change the duplex flag for the active printer driver before the PrintOut function is called in Word.

This issue and it's workaround is described in KB828638 (https://support.microsoft.com/kb/828638). However the sample code on KB is in VB6 which developers find hard to convert to VB.Net or C#.

Attached with this blog post is a .Net solution with two projects (.Net Console applications), one for C# and another for VB.Net. Each project has a class "DuplexSettings” which expose methods to set (or get) duplex print flag.

 

Here is the sample code to use DuplexSettings class:

 

In C# (DuplexSettings.cs):

 MyDuplexSettings.DuplexSettings ds = new MyDuplexSettings.DuplexSettings();
 short status = 0;
 string errorMessage = string.Empty;
 status = ds.GetPrinterDuplex("<<Printer Name>>", out errorMessage);
 if (status == 0)
 {
 //Console.WriteLine("Error occured. Error Message is : " + errorMessage);
 //Some error occured, errorMessage is available in string errorMessage
 }
 else
 {
 //Console.WriteLine("Current Duplex Setting is : " & status);
 //Call successfull, Current duplex flag is set to status
 }
 
 status = 2; //set duplex flag to 2
 ds.SetPrinterDuplex("<<Printer Name>>", status, out errorMessage);
 //Console.ReadKey();

 

 In VB.Net (DuplexSettings.vb):

  Dim ds As New MyDuplexSettings.DuplexSettings()
 Dim status As Short
 Dim errorMessage As String = String.Empty
 status = False
 status = ds.GetPrinterDuplex("<<Printer Name>>", errorMessage)
 If (status = 0) Then
 'Console.WriteLine("Error occured. Error Message is : " + errorMessage)
 ' Some error occured, errorMessage is available in string errorMessage
 Else
 'Console.WriteLine("Current Duplex Setting is : " & status)
 'Call successfull, Current duplex flag is set to status
 End If
 status = 2 'set duplex flag to 2
 status = ds.SetPrinterDuplex("<<Printer Name>>", status, errorMessage)
 'Console.ReadKey()

 

 

PrinterDuplexSettings.zip

Comments

  • Anonymous
    May 23, 2017
    Hi, great article.than you a lot!