Is there a way via VBA to determine 1) if Excel is open and 2) if a given file is open?
I have a process that blows up if the user has certain workbooks open.
Thanks!
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Is Excel Running? Is a given workbook open?
Is there a way via VBA to determine 1) if Excel is open and 2) if a given file is open?
I have a process that blows up if the user has certain workbooks open.
Thanks!
You can use this:
Dim xlApp As Object Dim xlWbk As Object On Error Resume Next Set xlApp = GetObject(, "Excel.Application") If xlApp Is Nothing Then MsgBox "Excel is not running", vbInformation Else MsgBox "Excel is running", vbInformation End If Set xlWbk = GetObject("C:DocsMyWorkbook.xls") If xlWbk Is Nothing Then MsgBox "Workbook is not open", vbInformation Else MsgBox "Workbook is open", vbInformation End If On Error GoTo 0
I even tried this
Function ExcelCheck2(strFileIn As String) As Boolean On Error Resume Next Set xlApp = GetObject(, "Excel.Application") If xlApp Is Nothing Then MsgBox "Excel is not running", vbInformation Else MsgBox "Excel is running", vbInformation End If Set xlApp = GetObject(strFileIn) If xlApp Is Nothing Then MsgBox "Workbook is not open", vbInformation ExcelCheck2 = False Else MsgBox "Workbook is open", vbInformation ExcelCheck2 = True End If On Error GoTo 0 End Function
but never get a correct response on the workbook being open…
I apologize – GetObject will check whether Excel is running, but it will open the workbook if it wasn’t open yet.
Try this function:
Function ExcelCheck(strFileIn As String) As Boolean Dim xlApp As Object Dim xlWbk As Object On Error Resume Next Set xlApp = GetObject(, "Excel.Application") If Not xlApp Is Nothing Then For Each xlWbk In xlApp.Workbooks If xlWbk.FullName = strFileIn Then ExcelCheck = True Exit For End If Next xlWbk End If End Function
Warning: it may fail if the user has more than one instance of Excel.exe running (this is not the same as having multiple workbooks open).
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications