How to set printer properties using vb.net

只愿长相守 提交于 2019-12-13 08:47:51

问题


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


回答1:


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)



回答2:


It looks like Change printername in PrintDialog through code deals with making sure a print dialog box pre-selects the printer based on printer name in vb.net.



来源:https://stackoverflow.com/questions/40835828/how-to-set-printer-properties-using-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!