我继承了对使用 Access 进行数据库和 Excel 进行报告的 Web 应用程序的责任(幸运的是,我知道)。使用 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