vba 2 'On Error goto' 语句中的第二个被忽略

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7289220/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:57:20  来源:igfitidea点击:

The second of 2 'On Error goto ' statements gets ignored

excel-vbavbaexcel

提问by Greg

I have some code that tries to set 11x17 paper as a default...

我有一些代码试图将 11x17 纸张设置为默认值...

        On Error GoTo PageSizeErr
        ActiveSheet.PageSetup.PaperSize = xlPaperTabloid

' more code here

' 更多代码在这里

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

-------------- end of code sample -----------------

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

When it gets to the second 'ActivateSheet.PageSetup... line, instead of going to PageErr2 lable I get an error dialog box. (I have a printer selected that doesn't support 11x17 which is what I'm trying to test for.)

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

The multiple error handlers are needed as it seems that different printer drivers handle the setting the differently.

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

Why doesn't the second 'On Error goto ' statement get recognized?

为什么第二个“On Error goto”语句没有被识别?

回答by Tim Williams

You can't use on error goto within an error handler. See http://www.cpearson.com/excel/errorhandling.htm

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

Maybe try something like this:

也许尝试这样的事情:

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