【发布时间】:2013-05-29 18:11:25
【问题描述】:
我已经继承了一个 Web 应用程序的责任,该应用程序使用 Access 作为数据库并使用 Excel 进行报告(我很幸运,我知道)。使用 Office 2003 一切正常,但该公司已开始推出 Office 2007,但现在引起了问题。到目前为止,Excel 2007 似乎在生成的报告之一存在问题。
报告是通过 Access 数据库中的 VBA 代码生成的,有问题的行是这样的:
sht.PageSetup.PrintArea = strPrintArea
在这种情况下,strPrintArea 的值为"$C$2:$G$57",这对我来说是有效的。 sht 是传入此函数的 Excel 工作表。
但是该行将失败并出现以下错误:
Run-time error '-2147352560(80020010)':
Method 'PrintArea' of object 'PageSetup' failed
我很困惑为什么同一行代码在 Excel 2003 中可以正常工作,而在 Excel 2007 中却失败了。我发现 Microsoft 针对 Excel 2010 发布了针对此问题的修补程序 (http://support.microsoft.com/kb/2553436),但我还没有找到Excel 2007 的任何类似内容。
我不知道下一步该去哪里。非常感谢任何帮助!
不确定它会有多大帮助,但这是发生错误的完整功能:
Sub SetPrintProperty(ByRef sht As Excel.Worksheet, ByRef strPrintArea As String, ByRef douLeftMargin As Double, ByRef douRightMargin As Double, _
ByRef douTopMargin As Double, ByRef douBottomMargin As Double, _
ByRef douHeaderMargin As Double, ByRef douFooterMargin As Double)
'*******************************************************************************************************************
'set the print area for a worksheet
'Arguments:
' sht: the spreadsheet needed to set print area
' strPrintArea: the address of the print area on a spreadsheet
' douLeftMargin: left margin
' douRightMargin: right margin
' douTopMargin: top margin
' douBottomMargin: bottom margin
' douHeaderMargin: header margin
' douFooterMargin: footer margin
'********************************************************************************************************************
sht.PageSetup.PrintArea = strPrintArea
With sht.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
With sht.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Excel.Application.InchesToPoints(douLeftMargin)
.RightMargin = Excel.Application.InchesToPoints(douRightMargin)
.TopMargin = Excel.Application.InchesToPoints(douTopMargin)
.BottomMargin = Excel.Application.InchesToPoints(douBottomMargin)
.HeaderMargin = Excel.Application.InchesToPoints(douHeaderMargin)
.FooterMargin = Excel.Application.InchesToPoints(douFooterMargin)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
' .PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
'.PaperSize = xlPaperLetter
.PaperSize = xlPaperLegal
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
End With
End Sub
【问题讨论】:
-
您从哪里运行此代码?检查您是否没有混淆对 Access 和 Excel 的引用(您是否在 Access 中正确设置了对 Excel 的引用)。我认为某处有你的问题......
-
此代码是从通过 Web 应用程序中的 ActiveX 调用的 VBA 宏执行的。我对 VBA 很陌生,我将如何检查参考资料?
-
尝试更换打印机再试一次。我遇到了类似的问题,将打印机更改为“软件”打印机(即 Microsoft Office Document Image Writer),进行了所有页面设置更改,然后将打印机改回了真正的打印机。似乎可以解决问题。除此之外,它比直接与打印机驱动程序通信要快得多。
-
刚刚尝试将默认打印机从办公室的打印机更改为 Microsoft 的软件 XPS 打印机。没有骰子。我还尝试在分配给 PageSetup.PrintArea 之前在代码中设置它,它仍然抛出相同的错误:
For Each prt In Application.Printers If (prt.DeviceName = "Microsoft XPS Document Writer") Then Set Application.Printer = prt End If Next prt sht.PageSetup.PrintArea = strPrintArea