Using vbscript to check if excel workbook is open

JimmyWright

New Member
Joined
Jun 13, 2014
Messages
5
Good morning to all,

I have been tasked with replacing an old report using excel 2000 with excel 2013. We are in the process of upgrading old (old!) computers that run all our reports. One of the reports is a daily shift report, and depending on the time of day it saves as a day shift report or a night shift report. The problem is that it is a blank excel workbook with no macros, and another excel has all the macros in it to preform all the functions needed. The excel workbook with the macros is called from the schedule task manager, runs the macros and closes itself. I would like to do away with having to use another excel workbook to do all the functions needed using a vbscript. I have everything I need to open the workbook and close the workbook, but I need a way to check if the workbook is open and if it is to close is so that I can open it again to run the macros. I find that leaving workbooks open all the time is not the best way of doing things.

My question is what script can I use to check if the workbook is open, and if so save and close it. I'm very green behind the ears with vb scripting and macros, I have been searching some but my time frame is getting shorter. Also this workbook is not and won't be shared. Everything will be done on the same PC.

Here are some of the places I've gone to get an idea but I cant seem to get something to work.
Determine if Excel workbook is already open
vbscript - Check if a specific Excel file is open when several Excel files are open and activate it - Stack Overflow
How to Close Excel file from VBScript without being prompted? - Stack Overflow

Examples of what I've tried:

ExcelFileName = "DayShiftRpt.xlsm"

On Error Resume Next
Set xl = GetObject("DayShiftRpt").Application 'attach to running Excel instance
If Err Then
If Err.Number = 429 Then
WScript.Echo "Workbook not open (Excel is not running)."
Else
WScript.Echo Err.Description & " (0x" & Hex(Err.Number) & ")"
End If
WScript.Quit 1
End If
On Error Goto 0

Set wb = Nothing
For Each obj In xl.Workbooks
If obj.Name = ExcelFileName Then 'use obj.FullName for full path
Set wb = obj
Exit For
End If
Next
If wb Is Nothing Then
WScript.Echo "Workbook not open."
WScript.Quit 1
End If

And:

Function IsWorkBookOpen(ByVal OWB As String) As Boolean
IsWorkBookOpen = False
Dim WB As Excel.Workbook
Dim WBName As String
Dim WBPath As String
Err.Clear
On Error Resume Next
OWBArray = Split(OWB, "\")
Set WB = Application.Workbooks(OWBArray(UBound(OWBArray)))
WBName = OWBArray(UBound(OWBArray))
WBPath = WB.Path & "\" & WBName
If Not WB Is Nothing Then
If UBound(OWBArray) > 0 Then
If LCase(WBPath) = LCase(OWB) Then IsWorkBookOpen = True
Else
IsWorkBookOpen = True
End If
End If
Err.Clear
End Function
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hey I've found this (part of another code) on another site and its works for what I need to start off with.

OWB = "C:\Users\RED\Documents\~$DayShiftRpt.xlsm"
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(OWB) Then
WScript.Echo "File open"
Else
WScript.Echo "File not open"
End If


How can I get the script to close the workbook in the if statement instead of (WScript.Echo "File open"). I'm having a difficult time figuring out how to come by closing the work book.
 
Upvote 0
See if this works for you:
Code:
Sub IfOpenThenClose()
Dim sF As String
sF = "DayShiftRpt.xlsm"  '<-- Enter workbook Name.FileExtension between the quote marks
If WorkbookOpen(sF) Then
   With Workbooks(sF)
        .Save
        .Close
    End With
End If
End Sub

Function WorkbookOpen(WorkBookName As String) As Boolean
    WorkbookOpen = False
    On Error GoTo WorkBookNotOpen
    If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
        WorkbookOpen = True
        Exit Function
    End If
WorkBookNotOpen:
End Function
 
Upvote 0
See if this works for you:
Code:
Sub IfOpenThenClose()
Dim sF As String
sF = "DayShiftRpt.xlsm"  '<-- Enter workbook Name.FileExtension between the quote marks
If WorkbookOpen(sF) Then
   With Workbooks(sF)
        .Save
        .Close
    End With
End If
End Sub

Function WorkbookOpen(WorkBookName As String) As Boolean
    WorkbookOpen = False
    On Error GoTo WorkBookNotOpen
    If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
        WorkbookOpen = True
        Exit Function
    End If
WorkBookNotOpen:
End Function

Hey JoeMo, Thank you for the reply, is this a script that is within excel? or can it it be a separate vbscript ran in CMD?
Also "Enter workbook Name.FileExtension between the quote marks" does this mean that it should be written like "C:\User\RED\Documents\DayShiftRpt.xlsm"
 
Upvote 0
Hey JoeMo, Thank you for the reply, is this a script that is within excel? or can it it be a separate vbscript ran in CMD?
Also "Enter workbook Name.FileExtension between the quote marks" does this mean that it should be written like "C:\User\RED\Documents\DayShiftRpt.xlsm"
It's a VBA script for use within Excel. You only need the workbook name and file extension like "DayShiftRpt.xlsm" not the full path & filename.
 
Upvote 0
Ok I see let me try it, one more question, This is to work in a separate excel workbook or in the same one?
You can run it from any open workbook, but from what you describe as your objective, it seems to me you want it in some workbook other than the one you are testing for being open.
 
Upvote 0

Forum statistics

Threads
1,215,825
Messages
6,127,111
Members
449,359
Latest member
michael2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top