Hi all,
Last year I developed an access module that allowed me to open, refresh connections, save and close an excel file. I need to do the same again, with almost identical projects / workbooks, so thought it would be a simple case of copying and pasting the old access file, renaming it and changing the code to the new file. I have done this, and all documents are saved in the same location, but its not working.
Here is the code that worked, and still works on the old excel file:
Function fRefreshWorkbook() As Boolean
On Error GoTo Err_fRefreshWorkbook
Dim objXL As Object, objWbk As Object, blNewInst As Boolean, _
strPathToFile As String
Const c_strFile As String = "Document.xlsx"
On Error Resume Next
' See if we can grab an instance of Excel already running
Set objXL = GetObject(, "Excel.Application")
If Err <> 0 Then
' No existing instance of Excel, let's create one
Set objXL = CreateObject("Excel.Application")
' Set a flag so we know to destroy the instance after we're done
blNewInst = True
Err = 0
End If
On Error GoTo Err_fRefreshWorkbook
' Define the Excel file we want to open
strPathToFile = CurrentProject.Path & "" & c_strFile
' Open the file setting the parameter to RefreshLinks = True
Set objWbk = objXL.Workbooks.Open(strPathToFile, True)
With objWbk
.Connections("ECWL Report").Refresh
.Connections("ECWL Report1").Refresh
.Save
.Close
End With
Exit_fRefreshWorkbook:
If Not objWbk Is Nothing Then Set objWbk = Nothing
If Not objXL Is Nothing Then
If blNewInst Then objXL.Quit
Set objXL = Nothing
End If
Exit Function
Err_fRefreshWorkbook:
Select Case Err.Number
Case Else
MsgBox "Error No.: " & Err.Number & vbNewLine & vbNewLine & _
"Description: " & Err.Description & vbNewLine & vbNewLine & _
"Procedure: fRefreshWorkbook" & vbNewLine & _
IIf(Erl, "Line No: " & Erl & vbNewLine, "") & _
"Module: basTest", , "Error: " & Err.Number
End Select
Resume Exit_fRefreshWorkbook
End Function
I thought it would be a simple case of renaming the Const c_strFile As String = "Document.xlsx" to "Document1.xlsx"
The old code still works perfectly and does the job required.
However, when I try the above on the new files I get the error:
Error No.: 9
Description: Subscript out of range
Procedure: fRefreshWorkbook
Module: basTest
Many thanks in advance for any suggestions!
Last year I developed an access module that allowed me to open, refresh connections, save and close an excel file. I need to do the same again, with almost identical projects / workbooks, so thought it would be a simple case of copying and pasting the old access file, renaming it and changing the code to the new file. I have done this, and all documents are saved in the same location, but its not working.
Here is the code that worked, and still works on the old excel file:
Function fRefreshWorkbook() As Boolean
On Error GoTo Err_fRefreshWorkbook
Dim objXL As Object, objWbk As Object, blNewInst As Boolean, _
strPathToFile As String
Const c_strFile As String = "Document.xlsx"
On Error Resume Next
' See if we can grab an instance of Excel already running
Set objXL = GetObject(, "Excel.Application")
If Err <> 0 Then
' No existing instance of Excel, let's create one
Set objXL = CreateObject("Excel.Application")
' Set a flag so we know to destroy the instance after we're done
blNewInst = True
Err = 0
End If
On Error GoTo Err_fRefreshWorkbook
' Define the Excel file we want to open
strPathToFile = CurrentProject.Path & "" & c_strFile
' Open the file setting the parameter to RefreshLinks = True
Set objWbk = objXL.Workbooks.Open(strPathToFile, True)
With objWbk
.Connections("ECWL Report").Refresh
.Connections("ECWL Report1").Refresh
.Save
.Close
End With
Exit_fRefreshWorkbook:
If Not objWbk Is Nothing Then Set objWbk = Nothing
If Not objXL Is Nothing Then
If blNewInst Then objXL.Quit
Set objXL = Nothing
End If
Exit Function
Err_fRefreshWorkbook:
Select Case Err.Number
Case Else
MsgBox "Error No.: " & Err.Number & vbNewLine & vbNewLine & _
"Description: " & Err.Description & vbNewLine & vbNewLine & _
"Procedure: fRefreshWorkbook" & vbNewLine & _
IIf(Erl, "Line No: " & Erl & vbNewLine, "") & _
"Module: basTest", , "Error: " & Err.Number
End Select
Resume Exit_fRefreshWorkbook
End Function
I thought it would be a simple case of renaming the Const c_strFile As String = "Document.xlsx" to "Document1.xlsx"
The old code still works perfectly and does the job required.
However, when I try the above on the new files I get the error:
Error No.: 9
Description: Subscript out of range
Procedure: fRefreshWorkbook
Module: basTest
Many thanks in advance for any suggestions!