Microsoft.Office.Interop.Excel打印输出引发错误 [英] Microsoft.Office.Interop.Excel PrintOut Throws Error

查看:96
本文介绍了Microsoft.Office.Interop.Excel打印输出引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在64位Windows 7桌面工作站上的.Net Framework 4.5 Windows Service项目中使用Microsoft.Office.Interop.Excel,除了PageSetup属性,更重要的是Worksheet对象的PrintOut方法,其他所有东西似乎都在工作

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.

代码如下:

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()错误: 未安装打印机.要安装打印机,请单击文件"选项卡,然后单击打印".单击未安装打印机",然后单击添加打印机".请按照添加打印机"对话框中的说明进行操作.

我肯定在我的设备和打印机中定义了打印机.我将网络打印机设置为默认打印机.我什至在其他帖子中也看到了此打印机的推荐,但我将此打印机添加到Win.ini,但无济于事.

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.

推荐答案

我在写问题时想到了这一点.

I figured this out while writing up the question.

确保在具有设置打印机和指定默认打印机的帐户的情况下运行Windows服务.我是在本地服务帐户下运行该服务的,一旦我将其切换为以我的个人帐户运行,则PageSetup属性和PrintOut()方法都可以正常工作!

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!

这篇关于Microsoft.Office.Interop.Excel打印输出引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆