在VBA中显示超链接相关的错误

我有下面的代码,将打开单元格范围内的文件,并刷新和保存工作簿。但我想知道如何处理运行时错误,如果有一个拼写错误放在单元格值。 我已经使用Application.Screenupdating函数,但我仍然popup信息的超链接相关的错误。 任何帮助将不胜感激。

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 

我能够重现你的错误,但不能解释它。 但我怀疑这是与非传统的error handling。

代码中还有其他一些问题,一旦解决了最初的问题就会变得明显。

我build议你重新考虑你的代码,以使用更常规的error handling。
下面还包括改进结构的其他build议。

尝试这个

 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