2

World!

I am trying to automate a report task at my job and I have the following situation:

I need to execute a macro on a workbook by using a script. I tried to write a vbscript to do the job, and this is the significant part of it:

Set objWbk = GetObject("***Path***\test.xlsm")

objWbk.Application.Run "test.xlsm!test" 

WScript.Quit

The macro runs perfectly. My real problem is that I only want to do the report only if the workbook is open.

Is there a way (in vbs or vba) to determine if that workbook is open ? (by the way, it is located on another computer on my network)

Community
  • 1
  • 1
kb_sou
  • 1,059
  • 1
  • 16
  • 26

2 Answers2

1

This is not fully tested and may a need a bit of modification but see if it gets you what you need.

On Error Resume Next
Set objWbk = GetObject("***Path***\test.xlsm")

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

wScript.Quit
Scott Holtzman
  • 27,099
  • 5
  • 37
  • 72
1

Since you want to run the macro only when the workbook is already opened, something like this might work:

wbName = "test.xlsm"
wbFullName = "***Path***\" & wbName

Set xl = GetObject(, "Excel.Application")
For Each wb In xl.Workbooks
  If LCase(wb.Path & "\" & wb.Name) = wbFullName Then
    wb.Application.Run wbName & "!test"
  End If
Next
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328