Question as per title. I realise/accept that this could be dangerous in certain situations (e.g. if workbook file currently in use)
Question as per title. I realise/accept that this could be dangerous in certain situations (e.g. if workbook file currently in use)
*******************************************************
HELP WANTED! (Links to Forum threads)
Trying to create reusable code for Custom Events at Workbook (not Application) level
*******************************************************
Try this code
![]()
Sub Sample() Dim Ret Dim wbname As String wbname = "C:\Users\wbname.xlsx" Ret = IsWorkBookOpen(wbname) wbnameonly = Right(wbname, Len(wbname) - InStrRev(wbname, "\")) MsgBox wbnameonly If Ret = True Then MsgBox "OPen" Workbooks(wbnameonly).Close SaveChanges:=False MsgBox "File " & wbnameonly & " closed by code" 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
If you think someone helped you, click on the "* Add Reputation" as a way to say thank you.
If your problem is solved, go to Thread Tools and select Mark This Thread Solved
TY saravnepali
I tried the code.
- It closes the workbook if it is open in the same instance of Excel that the VBA is running in.
- It doesn't close the workbook if the workbook is open in a different instance of Excel.
Hi,
See the file associated with post #2 in the following thread (API references coded for 32 bit Excel only): http://www.excelforum.com/excel-prog...instances.html
The file contains the Macro DisplayDataForAllOpenExcelInstances() below that displays data about each open Excel File in all instances of Excel which should help you get started. There is also a Macro in the file that closes all open Excel Files in All Instances of Excel, except the file that is running the code. Please NOTE that Macro DisplayDataForAllOpenExcelInstances() overwrites the contents of Sheet 'Main' in the 'Active Workbook'. A runtime error will occur if Sheet 'Main' DOES NOT EXIST.
Lewis![]()
Option Explicit 'Reference: cytop (Thank you) http://www.ozgrid.com/forum/showthread.php?t=182853 'The following declarations are for 32 bit Excel only Private Declare Function FindWindowEx Lib "User32" Alias "FindWindowExA" _ (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _ ByVal lpsz2 As String) As Long Sub ClearA21ToA9999() Sheets("Main").Range("A21:A9999").ClearContents End Sub Sub DisplayDataForAllOpenExcelInstances() 'This displays information about all open instances of Excel 'Reference: cytop (Thank you) http://www.ozgrid.com/forum/showthread.php?t=182853 Dim xlApp As Object Dim wb As Object Dim ws As Object Dim iCountInstances As Long Dim iCountWorkbooks As Long Dim iRow As Long Dim hWndXL As Long 'Clear the output area Call ClearA21ToA9999 'Set the row number to the row before the first output row iRow = 20 'Output a Start message iRow = iRow + 1 Sheets("Main").Cells(iRow, 1) = "DisplayDataForAllOpenExcelInstances() started on " & Now() & "." 'Find the first Excel Window hWndXL = FindWindowEx(0&, 0&, "XLMAIN", vbNullString) 'Loop until there are no more Open Instances of Excel While hWndXL > 0 'Increment counter iCountInstances = iCountInstances + 1 'Print Instance & Handle to Debug window iRow = iRow + 1 iRow = iRow + 1 iRow = iRow + 1 Sheets("Main").Cells(iRow, 1) = "Instance #" & iCountInstances & ": Handle: " & hWndXL 'Get a reference to it If GetReferenceToXLApp(hWndXL, xlApp) Then 'Clear the Workbook count iCountWorkbooks = 0 'Iterate through the workbook For Each wb In xlApp.Workbooks 'Increment the count of Workbooks in this instance iCountWorkbooks = iCountWorkbooks + 1 'Output Workbook name iRow = iRow + 1 iRow = iRow + 1 Sheets("Main").Cells(iRow, 1) = "Workbook " & iCountWorkbooks & ": " & wb.Name 'List worksheets in the Workbook For Each ws In wb.Worksheets iRow = iRow + 1 Sheets("Main").Cells(iRow, 1) = "Workbook " & iCountWorkbooks & ": " & wb.Name & _ " Sheet: " & ws.Name Next ws Next wb End If 'Find the next Excel Window hWndXL = FindWindowEx(0, hWndXL, "XLMAIN", vbNullString) Wend End Sub
Thanks Lewis. I will see if I can get time to test this later this month. Will let you know how it goes.
Thanks for the suggestion Lewis. The 4th goal is now resolved by using GetObject on the open workbook followed by closing the object.
1. In total, the topic is not entirely clear, because it is not known whether it is an open file on the local computer, or whether it is an open file from e.g. a company network. Depending on where you open the file, different ways to handle these situations will apply
2. "Open ...For Input ..."
This method is not credible (reliable ?), see 'Open' statement description in Microsoft context help:
"Important - In Binary, Input, and Random modes, you can open a file using a different file number without first closing the file. In Append and Output modes, you must close a file before opening it with a different file number",
so using 'Input' for 'Open' statement you can open an already open file and declare (state, tell) that it has not been opened before.
3. A brief "web query" shows interesting solutions that can be customized to this topic.
A. Solution of Peter Thornton - lists all Excel instances nicely:
https://social.msdn.microsoft.com/Fo...forum=exceldev
B. "Check Whether an Excel File Is Open on a Network"
http://www.java2s.com/Code/VBA-Excel...onaNetwork.htm
and in many, many places ...
... on the forum something would also be found, but I admit that I was not looking here ...![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks