Is there a way to test first if MySampleAll.xls is already open

C

CRayF

I have created file called StartExcelApp.vbs
That is coded:

Dim XLApp
Dim XLWkb
Set XLApp = CreateObject("Excel.Application")
xlapp.visible = true
xlapp.workbooks.open "MySampleAll.xls"
xlapp.ActiveWorkbook.RunAutoMacros 1

Is there a way to test first if MySampleAll.xls is already open so that I am
not starting a second copy. When the MySampleAll.xls is opened it runs an
auto macro that updates some data and then immediately “saves†the workbook.
The macro errors because the second iteration is now set to read only? I only
want 1 copy open so if this VBS is runs when MySampleAll.xls is already open,
I want it to end without opening it…
 
B

Bob Phillips

On Error Resume Next
Set XLWkb = Workbooks("MySample.xls")
On Error Goto 0
If XLWkb Is Nothing Then
Set XLWkb = XLApp.Workbooks.Open("MySample.xls")
End If
 
C

CRayF

Error Line 9 Object Required...
I tried changing line 9 from
If XLWkb Is Nothing Then
To
If XLWkb = "" Then
And that runs with no error, but allows multi copies of the XLS to run...
My ultimate goal would be to just go to the already opened one but I'd
settle to just abort the script.

-------------
Dim XLApp
Dim XLWkb
On Error Resume Next
Set XLApp = CreateObject("Excel.Application")
XLApp.Visible = true

Set XLWkb = Workbooks("RaceBetting.xls")
On Error Goto 0
If XLWkb Is Nothing Then
Set XLWkb = XLApp.Workbooks.Open("RaceBetting.xls")
XLApp.ActiveWorkbook.RunAutoMacros 1
End If
-----------------------
 
B

Bob Phillips

Sorry, you need to declare your variable types

Dim XLApp As Application
Dim XLWkb As Workbook
On Error Resume Next
Set XLApp = CreateObject("Excel.Application")
XLApp.Visible = True

Set XLWkb = XLApp.Workbooks("RaceBetting.xls")
On Error GoTo 0
If XLWkb Is Nothing Then
Set XLWkb = XLApp.Workbooks.Open("RaceBetting.xls")
XLApp.ActiveWorkbook.RunAutoMacros 1
End If
 
C

CRayF

That received a Error Line 1 Char 11

Adding your lines:
Dim XLApp As Application
Dim XLWkb As Workbook
errors off with "Error Line 1 Char 11".
Before, it had these coded instead and runs with no error, but starts a
second copy instead of terminating the script.

Dim XLApp
Dim XLWkb
-------------------------------
Dim XLApp As Application
Dim XLWkb As Workbook

On Error Resume Next
Set XLApp = CreateObject("Excel.Application")
XLApp.Visible = True

Set XLWkb = XLApp.Workbooks("RaceBetting.xls")
On Error GoTo 0
If XLWkb Is Nothing Then
Set XLWkb = XLApp.Workbooks.Open("RaceBetting.xls")
XLApp.ActiveWorkbook.RunAutoMacros 1
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top