用于检查文件是否打开的VB脚本 - 如果是,则退出 [英] VB Script to check if file is open - exit if so
问题描述
早安好人 -
我的工作网络共享由财务团队利用。 有些时候运行各种自动化脚本,某些文件没有更新,因为有人打开了excel文件。
I have a network share at work that is leveraged by the finance team. There are times when running various automation scripts that certain files are not updated due to the fact someone has an excel file open.
因此,我正在寻找一段VB脚本代码首先检查文件是否打开。如果没有,继续,如果它是打开的,然后退出。
Therefore, I'm looking to write a piece of VB script code that first checks to see if the file is open. If not, proceed, if it is open, then exit.
任何人都可以提供帮助吗?谢谢!
Can anyone assist? Thank you!
推荐答案
我一直在玩这个,似乎工作正常。 这是最佳做法还是有人可以推荐另一种方法?
I've been playing around with this and it seems to be working okay. Is this best practice or can someone recommend another method?
ExcelFileName = "Format.xlsx"
On Error Resume Next
Set xl = GetObject(, "Excel.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
WScript.Echo "Workbook is open"
WScript.Sleep 10000
Set wb = obj
Exit For
End If
Next
If wb Is Nothing Then
WScript.Echo "Workbook not open."
WScript.Quit 1
End If
WScript.Echo "Exiting VB Script"
WScript.Quit 99
这篇关于用于检查文件是否打开的VB脚本 - 如果是,则退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!