vb.net 无法设置 PageSetup 类的 PaperSize 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15572525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to set the PaperSize property of the PageSetup class
提问by Andre Lombaard
I'm trying to print an excel spreadsheet using VB.NET but I'm getting an error
我正在尝试使用 VB.NET 打印 Excel 电子表格,但出现错误
Unable to set the PaperSize property of the PageSetup class
无法设置 PageSetup 类的 PaperSize 属性
Here is my code,
这是我的代码,
Dim oldCI As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")
With application
.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
.Visible = False
.EnableEvents = False
.DisplayAlerts = False
.ScreenUpdating = False
End With
Dim workbook As Excel.Workbook
Dim worksheet As Excel.Worksheet
'Open as readonly and do not update links
workbook = application.Workbooks.Open(_fileName, 2, True)
For Each worksheet In workbook.Worksheets
worksheet.PageSetup.PaperSize = _paperSize
Next
workbook.PrintOutEx()
workbook.Close(False)
application.Quit()
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI
worksheet = Nothing
application = Nothing
This code works on my development machine, as soon as I deploy to the test server the code fails. There is already a default printer driver installed on the server.
这段代码在我的开发机器上工作,一旦我部署到测试服务器,代码就会失败。服务器上已经安装了默认的打印机驱动程序。
回答by X Sham
You need to install printer drivers. The PageSetup class of Excel must interact with the printer drivers
您需要安装打印机驱动程序。Excel 的 PageSetup 类必须与打印机驱动程序交互
I'm having similar issue running related code on Windows 8 operating under Mac Parallels (Virtual Machine). For anyone who are doing Visual Studio deployment on Mac Parallels, here's what I'm doing:
我在 Mac Parallels(虚拟机)下运行的 Windows 8 上运行相关代码时遇到了类似的问题。对于在 Mac Parallels 上进行 Visual Studio 部署的任何人,这就是我正在做的事情:
- Disable Printers sharing between Mac and Windows. Go to Parallels (VM) > Configure > Hardware and click Print. Uncheck 'Add all Mac printers' and 'Synchronize Default Printer'
- Install printer's driver on Windows
- In order to get assigned IP address by the same wireless router (if you need to connect to printer wirelessly), you need to set up networking as bridged. Go to Parallels (VM) > Configure > Hardware > 'Network 1' and choose Networking type as Airport and DHCP server as Auto
- 禁用 Mac 和 Windows 之间的打印机共享。转至 Parallels (VM) > 配置 > 硬件,然后单击打印。取消选中“添加所有 Mac 打印机”和“同步默认打印机”
- 在 Windows 上安装打印机驱动程序
- 为了获得同一无线路由器分配的 IP 地址(如果您需要无线连接打印机),您需要将网络设置为桥接。转到 Parallels (VM) > 配置 > 硬件 > '网络 1' 并选择网络类型作为机场和 DHCP 服务器作为自动
回答by Leo Gurdian
The issue at the core is in the _paperSizeconstant. In my case, it was giving the same error on: (forgive c#)
核心问题在于_paperSize常量。在我的情况下,它给出了同样的错误:(原谅 c#)
worksheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperLedger;
depending on the printer, for example the "ledger" size paper may be defined as 11x17 or Tabloid. Find out what the print driver refers to the page sizing and then
取决于打印机,例如“分类帐”尺寸的纸张可能被定义为 11x17 或小报。找出打印驱动程序指的是页面大小,然后
DIDN'T WORK:
没用:
excel.ActiveSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperLedger
excel.ActiveSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaper11x17
WORKED:
工作:
excel.ActiveSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperTabloid
I hope this can help anyone having frustrating issues with funny inconsistencies of Microsoft Excel's enums.
我希望这可以帮助任何人因 Microsoft Excel 枚举的有趣不一致而遇到令人沮丧的问题。
回答by Walter Stabosz
Confirmation of XSham's answer from Microsoft:
微软对 XSham 回答的确认:
Symptoms
When you run a Microsoft Visual Basic for Applications macro that attempts to set or get the page setup properties for any sheet in a workbook in Microsoft Excel, you may receive either of the following error messages:
Run-time error '1004': Unable to set the x property of the PageSetup class Run-time error '1004': Unable to get the x property of the PageSetup class
Cause
This problem occurs when there are no printer drivers installed on your computer. Excel cannot set or get page setup properties if no printer drivers are installed.
Resolution
To prevent this problem from occurring, install a printer driver on your computer. Use the Printers option in Control Panel to add and remove printer drivers.
症状
当您运行 Microsoft Visual Basic for Applications 宏尝试设置或获取 Microsoft Excel 工作簿中任何工作表的页面设置属性时,您可能会收到以下错误消息之一:
运行时错误“1004”:无法设置 PageSetup 类的 x 属性运行时错误“1004”:无法获取 PageSetup 类的 x 属性
原因
当您的计算机上没有安装打印机驱动程序时会出现此问题。如果未安装打印机驱动程序,Excel 将无法设置或获取页面设置属性。
解析度
为防止发生此问题,请在您的计算机上安装打印机驱动程序。使用控制面板中的打印机选项添加和删除打印机驱动程序。
回答by I kiet
You can use office automation to change the page size something like this
您可以使用办公自动化来更改页面大小,如下所示
worksheet.PageSetup.PaperSize = WdPaperSize.wdPaperLetter
or choose any other paper type from the dropdown list on "WdPaperSize."
或从“WdPaperSize”的下拉列表中选择任何其他纸张类型。
hope it will help you a bit.
希望它会帮助你一点。

