lookingforK
Registered User.
- Local time
- Today, 03:40
- Joined
- Aug 29, 2012
- Messages
- 48
How to use VBA to check whether an Excel .xlsx file is open and close it if it is open?
Hi,
I am using MS Access 2007 to export data to an MS Excel 2007 wookbook (.xlsx).
The code looks like:
......
Private Sub GenerateReport(ReportPath As String, Q4 As String)
Dim xl As New Excel.Application
Dim wkbDest As Excel.Workbook
Dim wkbSource As Excel.Workbook
(How to write the VBA code here: if the Excel .xlsx file for taking data from Access is open, then close it; if the Excel .xlsx file for taking data from Access is not open, then continue)
' Check whether the Excel exists in the folder. If it already exists, pop up a message for an option of replacing it or not
If Len(Dir(ReportPath & "\" & "the Excel .xlsx file name for taking data from Access")) > 0 Then
If MsgBox("[" & ReportPath & "\" & "the Excel .xlsx file name for taking data from Access" & "]" & " already exists." & _
Chr(13) & Chr(10) & Chr(13) & Chr(10) & " Replace it?", vbYesNo + vbQuestion) = vbNo Then
Exit Sub
Else
Kill ReportPath & "\" & "the Excel .xlsx file name for taking data from Access"
End If
End If
' Export data to the Excel wookbook
DoCmd.OutputTo acOutputQuery, ......
.....
How to write the VBA code for the part: if the Excel .xlsx file for taking data from Access is open, then close it; if the Excel .xlsx file for taking data from Access is not open, then continue? :banghead:
Thank you in advance.
Hi,
I am using MS Access 2007 to export data to an MS Excel 2007 wookbook (.xlsx).
The code looks like:
......
Private Sub GenerateReport(ReportPath As String, Q4 As String)
Dim xl As New Excel.Application
Dim wkbDest As Excel.Workbook
Dim wkbSource As Excel.Workbook
(How to write the VBA code here: if the Excel .xlsx file for taking data from Access is open, then close it; if the Excel .xlsx file for taking data from Access is not open, then continue)
' Check whether the Excel exists in the folder. If it already exists, pop up a message for an option of replacing it or not
If Len(Dir(ReportPath & "\" & "the Excel .xlsx file name for taking data from Access")) > 0 Then
If MsgBox("[" & ReportPath & "\" & "the Excel .xlsx file name for taking data from Access" & "]" & " already exists." & _
Chr(13) & Chr(10) & Chr(13) & Chr(10) & " Replace it?", vbYesNo + vbQuestion) = vbNo Then
Exit Sub
Else
Kill ReportPath & "\" & "the Excel .xlsx file name for taking data from Access"
End If
End If
' Export data to the Excel wookbook
DoCmd.OutputTo acOutputQuery, ......
.....
How to write the VBA code for the part: if the Excel .xlsx file for taking data from Access is open, then close it; if the Excel .xlsx file for taking data from Access is not open, then continue? :banghead:
Thank you in advance.