Ik onderzoek in onderstaande procedure of het Excel-bestand nog open is (hidden). Ik doe dit voor ik voordat ik het werkboek heb toegewezen aan oWerkboek. Maar als het Excel-bestand nog open is hoe spreek ik dit dan aan om te sluiten.
Dit is de functie die aangesproken wordt.
Code:
Private Sub Knop0_Click()
Dim oExcel As Excel.Application
Dim oWerkboek As New Workbook
Dim oTabblad As New Worksheet
If fFileOpen("C:\TestAccess\TestExportCSV.xlsx") = True Then
[COLOR="#FF0000"][B]oWerkboek[/B][/COLOR].Close True '>>> oWerkboek is niet gedefinieerd!!
MsgBox "File is open"
Else
MsgBox "File is closed"
End If
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "TestExportCSV", "C:\TestAccess\TestExportCSV.xlsx", True
Set oExcel = CreateObject("Excel.Application")
Set oWerkboek = oExcel.Workbooks.Open("C:\TestAccess\TestExportCSV.xlsx")
Set oTabblad = oWerkboek.Worksheets("TestExportCSV")
oTabblad.SaveAs "C:\TestAccess\TestExportCSV.csv", xlCSV
oExcel.Visible = True
Set oTabblad = Nothing
Set oWerkboek = Nothing
Set oExcel = Nothing
End Sub
Dit is de functie die aangesproken wordt.
Code:
Function fFileOpen(strFile As String) As Boolean
On Error Resume Next
Dim intFile As Integer
If Dir(strFile) = "" Then Exit Function
intFile = FreeFile()
Open strFile For Input Lock Read As intFile
Close intFile
If Err <> 0 Then
fFileOpen = True
End If
End Function