在VBA中,我以编程方式打开了一个名为“myWork.XL”的MSExcel文件。现在,我想要一个可以告诉我它的状态的代码-不管它是否是开放的。比如说IsWorkBookOpened("myWork.XL) ?检测Excel工作簿是否已打开
3 回答
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
Option ExplicitSub Sample() Dim Ret Ret = IsWorkBookOpen("C:\myWork.xlsx") If Ret = True Then MsgBox "File is open" Else MsgBox "File is Closed" End IfEnd SubFunction IsWorkBookOpen(FileName As String) Dim ff As Long, ErrNo As Long On Error Resume Next ff = FreeFile() Open FileName For Input Lock Read As #ff Close ff ErrNo = Err On Error GoTo 0 Select Case ErrNo Case 0: IsWorkBookOpen = False Case 70: IsWorkBookOpen = True Case Else: Error ErrNo End SelectEnd Function
郎朗坤
TA贡献1921条经验 获得超9个赞
Function BookOpen(strBookName As String) As Boolean Dim oBk As Workbook On Error Resume Next Set oBk = Workbooks(strBookName) On Error GoTo 0 If oBk Is Nothing Then BookOpen = False Else BookOpen = True End IfEnd FunctionSub testbook() Dim strBookName As String strBookName = "myWork.xls" If BookOpen(strBookName) Then MsgBox strBookName & " is open", vbOKOnly + vbInformation Else MsgBox strBookName & " is NOT open", vbOKOnly + vbExclamation End IfEnd Sub
添加回答
举报
0/150
提交
取消