VBS to run macro on already open .xlsm (if it's open)

Technowolf

Board Regular
Joined
Aug 28, 2014
Messages
181
Trying to figure out how to do this via VBS. First, check if the workbook is open, and if so, run the macro in it.

I found this, but this opens a readonly version in the background and runs it, which is no good for my purposes

Code:
Option Explicit

Dim xlApp, xlBook


Set xlApp = CreateObject("Excel.Application")


Set xlBook = xlApp.Workbooks.Open("C:/Temp Code Setup2.xlsm", 0, True)
xlApp.Run "RunnerFile"
xlBook.Close
xlApp.Quit


Set xlBook = Nothing
Set xlApp = Nothing




WScript.Quit

Suggestions?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Update:
Okay, I found this, which is exactly what I need, but for some reason isn't working:

Code:
wbName = "TempCodeSetup2.xlsm"
wbFullName = "C:\" & wbName


Set xl = GetObject(, "Excel.Application")
For Each wb In xl.Workbooks
  If LCase(wb.Path & "\" & wb.Name) = wbFullName Then
    wb.Application.Run wbName & "!RunnerFile"
  End If
Next
 
Last edited:
Upvote 0
Update 2: Found a solution that works (in case anyone else has this weird need)

Code:
On Error Resume NextSet objWbk = GetObject("C:\TempCodeSetup2.xlsm")


If Err.Number = 0 Then objWbk.Application.Run "TempCodeSetup2.xlsm!Runnerfile" 


wScript.Quit
 
Upvote 0
Update 2: Found a solution that works (in case anyone else has this weird need)

Code:
On Error Resume NextSet objWbk = GetObject("C:\TempCodeSetup2.xlsm")


If Err.Number = 0 Then objWbk.Application.Run "TempCodeSetup2.xlsm!Runnerfile" 


wScript.Quit

For some reason open read only for me. Can you post the code you used back then :)

Cheers,
Witek
 
Upvote 0
Hey Pitaszek! This post is pretty old, but I've gotten better with VBA, so maybe I can help you out. Now a days I would use some kind of loop to check something like this, rather than depend on error handling. There's a few ways to do this, but off the top of my head:
Code:
Dim wb as Workbook
For Each wb In Application.Workbooks
  If InStr(wbk.Name, "YourWorkBookName") > 0 Then 'Update This Line
    Application.Run ("'YourWorkbookName.xlsm'!YourMacroName") 'Update This Line
  Else
    ' Do something else if workbook isn't open. You can remove also safely remove the "Else"
  End If
Next wb

Hopefully that helps you out!
 
Upvote 0
Hey Pitaszek! This post is pretty old, but I've gotten better with VBA, so maybe I can help you out. Now a days I would use some kind of loop to check something like this, rather than depend on error handling. There's a few ways to do this, but off the top of my head:
Code:
Dim wb as Workbook
For Each wb In Application.Workbooks
  If InStr(wbk.Name, "YourWorkBookName") > 0 Then 'Update This Line
    Application.Run ("'YourWorkbookName.xlsm'!YourMacroName") 'Update This Line
  Else
    ' Do something else if workbook isn't open. You can remove also safely remove the "Else"
  End If
Next wb

Hopefully that helps you out!
i cant get your code to function, i throws an error line 1 char 8. i would like for it to fire one workbook right after another
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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