I have the following code. I want to check for any ODBC or other connections and ask the user if they want to refresh when the file opens. In the debugging MsgBoxes that I have below the ThisWorkbook.Connections.Count is 0. The Len(Trim(.....)) never shows up. I then get the "This workbook contains" MsgBox. My guess is that if there are truly no connections then Connections(1) doesn't exist and throws an exception. However, if that's the case, how is it making it into the If block? ThisWorkbook.Connections.Count is 0. We've already established that through the first MsgBox. Confused.
Thanks!
Private Sub Workbook_Open()
Dim result As VbMsgBoxResult
Dim result2 As VbMsgBoxResult
On Error Resume Next
MsgBox (ThisWorkbook.Connections.Count)
MsgBox (Len(Trim(ThisWorkbook.Connections(1).ODBCConnection.CommandText)))
If ThisWorkbook.Connections.Count > 0 And Len(Trim(ThisWorkbook.Connections(1).ODBCConnection.CommandText)) > 0 Then
result = MsgBox("This workbook contains external data connections which can be refreshed. Refresh now?", vbYesNo, "Refresh Data?")
If result = vbYes Then
result2 = MsgBox("This may take a few minutes. Continue?", vbYesNo, "Refresh Data?")
If result2 = vbYes Then
ThisWorkbook.RefreshAll
End If
End If
End If
AllHeaders
End Sub
Bookmarks