Microsoft.Office.Interop.Excel PrintOut Throws Error

限于喜欢 提交于 2021-02-08 06:57:22

问题


I'm using Microsoft.Office.Interop.Excel in a .Net Framework 4.5 Windows Service project on my 64bit Windows 7 desktop workstation and everything appears to be working except the PageSetup properties and more importantly the PrintOut method of the Worksheet object.

Here's the code:

Microsoft.Office.Interop.Excel.Application ExcelApp = null;
Microsoft.Office.Interop.Excel.Workbook WBook = new Microsoft.Office.Interop.Excel.Workbook;
Microsoft.Office.Interop.Excel.Worksheet WSheet = new Microsoft.Office.Interop.Excel.Worksheet);

ExcelApp = new Microsoft.Office.Interop.Excel.Application();

WBook = ExcelApp.Workbooks.Add();

WSheet = WBook.Worksheets[1];

//Do some stuff with the sheet

//None of this works, throws an error: Unable to set the Orientation property of the PageSetup class
WSheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
WSheet.PageSetup.Zoom = false;
WSheet.PageSetup.FitToPagesWide = 1;
WSheet.PageSetup.FitToPagesTall = false;

//Throws Error (see below)
WSheet.PrintOut(null, null, null, null, null, null, null);

PrintOut() Error: No printers are installed. To install a printer click the File tab, and then click Print. Click No Printers Installed, and then click Add Printer. Follow the instructions in the Add Printer dialog box.

I definitely have printers defined in my Devices and Printers. I have a network printer set as my Default. I have even added this printer to Win.ini as I've seen this recommended in other posts but to no avail.


回答1:


I figured this out while writing up the question.

Make sure you are running your Windows Service under an account that has printers set up and a specified Default printer. I was running the service under Local Service account, once I switched it to run under my personal account both the PageSetup properties and the PrintOut() method worked!



来源:https://stackoverflow.com/questions/23503998/microsoft-office-interop-excel-printout-throws-error

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