【问题标题】:Distinguish between two error occurring with `on error goto` and separately handle them区分使用 `on error goto` 发生的两个错误并分别处理它们
【发布时间】:2019-02-11 11:39:44
【问题描述】:

我正在与三个Word Documents 合作。

  1. 粗糙
  2. 中东、中东和非洲和拉丁美洲
  3. Ticker 墓地

在开始时只打开Rough 文档。我需要将数据从 Rough 转移到 CEEMEA & LATAM 并使用 Ticker Graveyard 来匹配一些查询,所以 CEEMEA & LATAM Ticker Graveyard 应该已经打开了。

为此,我检查了其他两个文档是否已打开。如果没有,那么我需要使用Documents.Open 打开特定文档。为此,我正在执行以下更新代码

sub ErrHandling()

On Error GoTo PROBLEM
Windows("CEEMEA & LATAM").Activate ''''' Here checking CEEMEA & LATAM status
'''' after checking STATUS do some work on CEEMEA & LATAM '''''

Set doc = Application.Windows("Ticker Graveyard").Document ''''' Here checking Ticker Graveyard status
'''' after checking do some work on Ticker Graveyard '''''

Exit Sub
PROBLEM:
If Err.Number = 5941 Then
    Documents.Open FileName:="C:\Users\dell\Desktop\EMEA CEEMEA\CEEMEA & LATAM.docx" --- handling only CEEMEA & LATAM document

'''''' These lines handle Date in document while opening document''''
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.EndKey Unit:=wdLine
Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.TypeText Format(Date, "MMMM dd, yyyy")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Else
MsgBox "UNEXPECTED ERROR = " & Err.Number
Exit Sub
End If

Resume Next
End Sub

但问题是两个检查点触发相同的Error Number 我无法使用简单的on error goto 语句区分它们。

现在我的问题是如何区分这两个错误?我该如何分别处理它们? 如果有其他方法可以处理这种情况,请分享。

【问题讨论】:

  • 请也向我们展示(作为minimal reproducible example 在问题中调用sub 的代码。就目前而言,断章取义,很难给出建议。马上,不过,我想说代码的结构应该不同,通过检查 Documents 集合是否存在文档。通常,使用 Activate 处理特定文档是一个坏主意 - 最好使用 @987654335 @对象。

标签: error-handling ms-word runtime-error goto onerror


【解决方案1】:

有许多策略可以处理代码需要引用可能存在或不存在的特定对象的情况。哪种方法是“最好的”,部分是哲学考虑。基本方法是:

  1. 触发错误,然后处理它。这就是问题中代码的设计方式。当使用 VBA 时,它也是一种有效的方法,因为它可以相对较快。但是,对于任何 .NET 语言,它都会显着降低代码速度,如果有任何其他可能性,则不应使用它。
  2. 迭代集合以确定相关对象是否可用。如果集合包含很多项目,这在 VBA 中可能比错误处理方法慢。另一方面,它往往使代码更容易理解(在我看来)。

下面根据问题中的代码概述了这两种方法的策略。

错误处理

在一个过程中可以使用多个“标签”(错误处理程序要跳转到的目标),因此可以为每个单独的文档使用一个“标签”。因此,对于每个文档,都有一个不同的错误处理程序,它有自己的代码“分支”。

On Error GoTo PROBLEM_CEEMA
Windows("CEEMEA & LATAM").Activate ''''' Here checking CEEMEA & LATAM status
On Error GoTo General
'''' after checking STATUS do some work on CEEMEA & LATAM '''''

On Error GoTo PROBLEM_TICKER
Set doc = Application.Windows("Ticker Graveyard").Document ''''' Here checking Ticker Graveyard status
On Error GoTo General
'''' after checking do some work on Ticker Graveyard '''''

Exit Sub

PROBLEM_CEEMA:
  If Err.Number = 5941 Then
    Documents.Open FileName:="C:\Users\dell\Desktop\EMEA CEEMEA\Ticker Graveyard.docx" --- handling only Ticker document    
  Else
    MsgBox "UNEXPECTED ERROR = " & Err.Number
    Exit Sub
  End If

Resume Next 

PROBLEM_TICKER:
  If Err.Number = 5941 Then
    Documents.Open FileName:="C:\Users\dell\Desktop\EMEA CEEMEA\CEEMEA & LATAM.docx" --- handling only CEEMEA & LATAM document    
  Else
    MsgBox "UNEXPECTED ERROR = " & Err.Number
    Exit Sub
  End If

Resume Next 

PROBLEM_General:
  'General, non-document-related error handling
End Sub

迭代 Documents 集合

Word 应用程序在Documents 集合中保留一个已打开文档的运行列表。可以使用For...Each(或For)循环遍历集合并比较文档名称。这可以在单独的函数中完成,这样代码就不需要重复了。

Sub Test
  Dim doc1 as Document, file1 as String
  Dim doc2 as Document, file2 as String

  file1 = "C:\Users\dell\Desktop\EMEA CEEMEA\CEEMEA & LATAM.docx"
  file2 = "C:\Users\dell\Desktop\EMEA CEEMEA\Ticker Graveyard.docx"

  If not IsDocumentOpen(file1) Then
    Set doc1 = Documents.Open(FileName:=file1)
  End If

  If not IsDocumentOpen(file2) Then
    Set doc2 = Documents.Open(FileName:=file2)
  End If
End Sub

Function IsDocumentOpen(fileName as String) as Boolean
  Dim doc as Word.Document
  Dim isOpen as Boolean

  isOpen = false
  For Each doc in Documents
     IF doc.Name = fileName Then
        isOpen = true
     End If
  Next
  IsDocumentOpen = isOpen
End Function

【讨论】:

  • 我对你的第二个建议很感兴趣,因为它更有效,但我在实施它时遇到了问题。打开文档后,我需要向该特定文档添加其他代码(即插入Date)。如果文档已经打开,那么我不需要这些额外的代码。我会在我原来的问题中提到这种情况。你能适应你的第二个建议吗? Function IsDocumentOpen也可以合并到sub test()吗?
猜你喜欢
  • 2012-12-18
  • 2019-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多