如何只有在Excel工作簿是打开使用VBScript运行宏? [英] How to run a macro only if the Excel workbook is open with VBScript?

查看:208
本文介绍了如何只有在Excel工作簿是打开使用VBScript运行宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

世界!

我想在我的工作自动完成报表任务,我有以下情况:

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

我需要通过使用脚本的工作簿执行宏。我试着写一个VBScript来完成这项工作,这是它的显著部分:

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.

有没有一种方法(在VBS或VBA),以确定是否该工作簿是打开的? (顺便说一句,它位于另一台计算机上我的网络上)

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)

推荐答案

由于要运行宏,只有当工作簿已经打开,这样的事情可能工作:

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

这篇关于如何只有在Excel工作簿是打开使用VBScript运行宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆