ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Detect if an excel file is open (https://www.excelbanter.com/excel-programming/396724-detect-if-excel-file-open.html)

JCP[_2_]

Detect if an excel file is open
 
I'm working with automation. I have two questions

1. How can detect if excel is running?
2. How can detect is a specific excel file (test.xls) is open? I is open,
the code should be close the file without save.

Thanks
jcp
--
Jose

No Name

Detect if an excel file is open
 
This will close test.xls if it's open in Excel. However, since Excel can
have several instances running simultaneously, this is not smart enough to
know which one to look in. It assumes there is only 1 instance running. Does
that matter to you?

Sub CloseAWorkbook()
Dim xl As Object
Dim wkb As Object
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
On Error GoTo 0
If Not xl Is Nothing Then
For Each wkb In xl.workbooks
If wkb.Name = "test.xls" Then
wkb.Close False
End If
Next
End If
Set wkb = Nothing
Set xl = Nothing
End Sub


"JCP" wrote in message
...
I'm working with automation. I have two questions

1. How can detect if excel is running?
2. How can detect is a specific excel file (test.xls) is open? I is open,
the code should be close the file without save.

Thanks
jcp
--
Jose





All times are GMT +1. The time now is 05:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com