vb.netprintinginteropword-2007

How to set printer properties using vb.net


I am using EDRAW to browse a Microsoft Word and can be Print , Preview , ETC... And I am planning to add some features to it. I am adding 2 buttons for Print Short (8.5 by 11 inches) and Print Long (8.5 by 13 inches) and I have 2 printers for long and short. How to set the properties of the printers in each button?. If I click button short it will print short using printer 1 same as the button long but it is in printer 2.

I am following the code in the Link provided above.

Anyone has an idea about it?. Any suggestion well help and well be accepted. Thanks.. Cheers.. I will give a 50 bounty to it after two days..

Code as @Hadi request

Here is my button code for print.

Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
    AxEDOffice1.SetActivePrinter("Printer Name")
    AxEDOffice1.ActiveDocument.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4
    AxEDOffice1.PrintDialog()
End Sub

and getting an error Object variable or With block variable not set in the line code of AxEDOffice1.ActiveDocument.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4


Solution

  • After Checking the library all you have to do is using SetActivePrinter Method to change your default printer like the following:

     AxEDOffice1.SetActivePrinter("Adobe PDF")
    

    And to change PaperSize you have to use the following

    AxEDOffice1.ActiveDocument.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4
    

    AxEDOffice1.ActiveDocument is an instance of Microsoft.Office.Interop.Word.WordDocumentClass

    Code tested it and it works fine.

    EDIT 1:

    Object variable or With block variable not set

    Read more about it on this MSDN article there are many suggestions.

    EDIT 2:

    To Print your document directly without showing PrintDialog you have to use PrintOut Function.

    AxEDOffice1.PrintOut(EDOfficeLib.WdPrintOutRange.wdPrintAllDocument)