我继承了一个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中失败。我发现微软针对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
答案 0 :(得分:1)
所以希望这会以某种方式帮助那些遇到同样问题的人,并希望自己解决问题的麻烦,这是我的最终解决方案。
我将在前言中说我认为这是Excel 2007 VBA中的一个错误,因为该解决方案对我没有任何意义。
无论如何,在尝试弄清楚导致错误的原因时,我尝试测试sht.PageSetup.PrintArea
的硬编码值。起初我做了一些基本的事情:
sht.PageSetup.PrintArea = "A1:A3"
令我惊讶的是,它奏效了!好的我想,如果我通过strPrintArea
输入我最初传入函数的值会怎么样?
sht.PageSetup.PrintArea = "$C$2:$G$57"
令我惊讶的是,它起了作用!好的,所以此时我很困惑,如何分配strPrintArea
导致错误,但"$C$2:$G$57"
工作正常?最后我没有答案,但怀疑Excel某种程度上想要分配一个“不同”类型的字符串,这就是我最终试着看看是否调用一个函数来返回我的字符串值会工作:
sht.PageSetup.PrintArea = Replace(strPrintArea, "-", "")
由于传入的值始终采用excel范围格式,如"$C$2:$G$57"
,因此对该值基本上没有任何作用(没有"-"
要替换)。我不知道为什么这会修复错误,但我确实很高兴它确实存在!
答案 1 :(得分:0)
这适用于listobjects(excel表):
Dim ws As Worksheet, LastRow as Integer
Set ws = Sheets("Sheet1")
With ws.ListObjects("My_Table")
LastRow = .ListRows(.ListRows.Count).Range.End(xlUp).Row - 1
'If LastRow = 1 Then LastRow = .ListRows(.ListRows.Count).Range.Offset(1, 0).End(xlUp).Row
ws.PageSetup.PrintArea = ws.Range(ws.Cells(1, 1), ws.Cells(LastRow, 12)).Address
End With
在我的情况下,此备选方案会生成错误:
ws.PageSetup.PrintArea = ws.Range("My_Table[#All]").Address
更新:我现在怀疑我在打印区域中使用冲突名称的问题与在两个不同的PC中使用相同文件有关:一个使用Windows 7,另一个使用西班牙语的Winows XP。 我看到,随着Office 2007打印区域的西班牙语XP安装成为'Área_de_impresión',这可能会与'Print_Area'命名范围产生一些冲突。