抑制VBA中的超链接相关错误

时间:2014-01-07 09:13:31

标签: excel excel-vba vba

我有下面的代码,它将打开单元格范围内的文件并刷新并保存工作簿。但我想知道如何处理运行时错误,如果有拼写错误放在单元格值中。我已经使用了Application.Screenupdating函数,但我仍然收到与超链接相关的错误的弹出消息。任何帮助将受到高度赞赏。

Sub openfilesandsave()

    On Error GoTo WriteLog  'proceed to error log file and print the error information.
    On Error Resume Next    'procedd to next step if any error occurs

        For i = 2 To 4
        Application.ScreenUpdating = False

    On Error GoTo WriteLog
    With Workbooks.Open(Range("B" & i)) 'Open the workbook in the cell range of i in B column


    strFileFullName = ActiveWorkbook.FullName ' stores the current opened link into strFileFullName variable

    Open "\Documents\Error Log.txt" For Append As #1
    Print #1, strFileFullName ' Printing strFileFullName values into log file.
    Close #1

           .RefreshAll     'Refresh current workbook
           .Save           'Save the current opened workbook
           .Saved = True

           'Check if the current workbook saved or not and print the result into log file
            If ActiveWorkbook.Saved = False Then

                 Open "\\Documents\Error Log.txt" For Append As #1
                 Print #1, " File Not Saved"
                 Print #1, "----------------------------------------------------"
                 Close #1

                 Else

                 Open "\\Documents\Error Log.txt" For Append As #1
                 Print #1, "File Saved Successfully"
                 Print #1, "----------------------------------------------------"
                 Close #1
                 End If


                 .Close 0    ' Close current workbook

            End With

WriteLog:
    'Open the errorlog file and print the error discription

    Open "\\Documents\Error Log.txt" For Append As #1
    Print #1, Err.Description & Format$(Now(), " mm/dd/yy hh:mm:ss")
    Print #1, "----------------------------------------------------"
    Close #1

    Next
        Application.ScreenUpdating = True
        End
End Sub

1 个答案:

答案 0 :(得分:1)

我能够重现您的错误,但无法解释。但我怀疑这与非传统的错误处理有关。

您的代码还存在许多其他问题,一旦初始问题得到解决,这些问题就会变得明显。

我建议您重新考虑代码以使用更常规的错误处理 关于改进结构的其他建议也包括在内。

试试这个

Option Explicit

Sub OpenFilesAndSave()
    Dim i As Long '<-- Declare all variables
    Dim strFileFullName As String
    Dim wb As Workbook
    Dim strErrorLog As String

    strErrorLog = "C:\Users\Chris\Documents\Scratch\Error Log.txt"

    For i = 2 To 4
        Application.ScreenUpdating = False

        strFileFullName = Range("B" & i) '<-- Move to here. stores the current opened link into strFileFullName variable

        Set wb = Nothing
        On Error Resume Next ' <-- use a Try/Catch style error handler
        Set wb = Workbooks.Open(strFileFullName) ' <-- set reference to opend workbook
        If wb Is Nothing Then
            WriteLog strErrorLog, Err.Description & Format(Now(), " mm/dd/yy hh:mm:ss"), True
            On Error GoTo 0
        Else
            On Error GoTo 0
            With wb 'Open the workbook in the cell range of i in B column

                'strFileFullName = .FullName ' <-- moved

                'Open "\Documents\Error Log.txt" For Append As #1  '<-- need to use \\Documents...
                WriteLog strErrorLog, strFileFullName, False

                .RefreshAll     'Refresh current workbook
                .Save           'Save the current opened workbook
                '.Saved = True  '<-- Why? will be true if save succeeded.

                'Check if the current workbook saved or not and print the result into log file
                ' <-- use with reference
                If .Saved = False Then
                    WriteLog strErrorLog, "File Not Saved"
                Else
                    WriteLog strErrorLog, "File Saved Successfully"
                End If
                .Close SaveChanges:=False    ' Close current workbook
            End With
        End If
    Next
    Application.ScreenUpdating = True
    ' End '<-- don't do this.
End Sub

Private Sub WriteLog(LogFile As String, Message As String, Optional Divider As Boolean = True)
    Open LogFile For Append As #1
    Print #1, Message
    If Divider Then
        Print #1, "----------------------------------------------------"
    End If
    Close #1
End Sub