2011-09-02 98 views
3

我有尝试设置的11x17纸作为默认的一些代码...2第二个“对错误转到”语句被忽略

 On Error GoTo PageSizeErr 
     ActiveSheet.PageSetup.PaperSize = xlPaperTabloid 

'更多代码在这里

PageSizeErr: 
    On Error GoTo PageErr2 
    ActiveSheet.PageSetup.PaperSize = xlPaper11x17 'try another 11x17 driver definition 
    GoTo resumePrinting 
PageErr2: 
    MsgBox ("There's a problem setting Tabloid paper for the printer you have selected." & Chr(10) _ 
    & "If you have an 11x17 printer selected, please contact EMBC, otherwise, try a different printer.") 
    Exit Sub 

- -------------代码结束示例-----------------

当它到达第二个'ActivateSheet.PageSetup .. 。行,而不是去PageErr2标签,我得到一个错误对话框。 (我选择了不支持11x17的打印机,这是我试图测试的。)

需要多个错误处理程序,因为不同的打印机驱动程序似乎处理不同的设置。

为什么第二个'On Error goto'语句被识别?

+1

只要改变你的“对错误转到”第二到一个goto就大功告成了。 – Kevin

回答

3

您不能在错误处理程序中使用错误goto。 见http://www.cpearson.com/excel/errorhandling.htm

也许尝试这样的事:

Sub Tester() 

Dim pSize As XlPaperSize 

    pSize = xlPaperTabloid 


    On Error GoTo haveError: 
    ActiveSheet.PageSetup.PaperSize = pSize 
    'print stuff... 

    Exit Sub 

haveveError: 
    If pSize = xlPaperTabloid Then 
     pSize = xlPaper11x17 
     Resume 
    End If 
    MsgBox ("Couldn't print using tabloid or 11x17") 

End Sub