I export data from Access database to an excel file using a macro. It works fine if the excel file is not opened, but it gives an error when it is. I would like to close the excel file before the data export. How can I do that?
I'm using this function
and call it like
to check if the file is open, but I'm unable to find a way to close it if it is.
I would appreciate some advice on how to close the excel file.
I'm using this function
Code:
Public Function IsWorkbookOpen(ByVal strWorkBookName As String) As Boolean
Dim objExcel As Object
Dim varWorkbook As Variant On Error GoTo ExitFunction
Set objExcel = GetObject(, "Excel.Application")
For Each varWorkbook In objExcel.Workbooks
If varWorkbook.Name = strWorkBookName Then
IsWorkbookOpen = True
Exit For
End If
Next
Exit Function:
Set objExcel = Nothing
End Function
Code:
If IsWorkbookOpen("Text.xlsx") = True Then ......
I would appreciate some advice on how to close the excel file.