Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether Excel workbook is already open [closed]

Tags:

excel

vba

In VBA, I opened an MS Excel file named "myWork.XL" programmatically.

Now I would like a code that can tell me about its status - whether it is open or not. I.e. something like IsWorkBookOpened("myWork.XL) ?

like image 797
user1222679 Avatar asked Feb 21 '12 06:02

user1222679


People also ask

Is there a way to tell if someone has an Excel file open?

In the Share Workbook message box, you can see who has opened the shared file in the 'Who has this workbook open now' box.

How can you see a list of what workbooks are currently open in Excel?

If there are more than nine workbooks open and you click the Switch Windows tool, Excel displays an option that says "More Windows." Click the option and you can see a display of all open workbooks. Selecting a workbook from this list ends up in that workbook being displayed.

Is workbook open VBA?

To open a workbook using VBA, you need to use the “Workbook. Open” method and specify the path of the file (make sure to specify the full path to the workbook with name and extension file type).


2 Answers

For my applications, I generally want to work with a workbook rather than just determine if it's open. For that case, I prefer to skip the Boolean function and just return the workbook.

Sub test()      Dim wb As Workbook      Set wb = GetWorkbook("C:\Users\dick\Dropbox\Excel\Hoops.xls")      If Not wb Is Nothing Then         Debug.Print wb.Name     End If  End Sub  Public Function GetWorkbook(ByVal sFullName As String) As Workbook      Dim sFile As String     Dim wbReturn As Workbook      sFile = Dir(sFullName)      On Error Resume Next         Set wbReturn = Workbooks(sFile)          If wbReturn Is Nothing Then             Set wbReturn = Workbooks.Open(sFullName)         End If     On Error GoTo 0      Set GetWorkbook = wbReturn  End Function 
like image 29
Dick Kusleika Avatar answered Sep 23 '22 02:09

Dick Kusleika


Try this:

Option Explicit  Sub Sample()     Dim Ret      Ret = IsWorkBookOpen("C:\myWork.xlsx")      If Ret = True Then         MsgBox "File is open"     Else         MsgBox "File is Closed"     End If End Sub  Function 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 Select End Function 
like image 80
Siddharth Rout Avatar answered Sep 23 '22 02:09

Siddharth Rout