View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
S. I. Becker S. I. Becker is offline
external usenet poster
 
Posts: 15
Default If workbook is open, save workbook?


"ph8" wrote in message
...

Greetings folks!

How do you check to see if a workbook is open?


Public Function IsWbOpen(ByVal Name as String, Optional ByRef Wb as
Workbook= Nothing) As Boolean
On Error GoTo Failed
Set Wb = Workbooks(Name)
IsWbOpen = True
Exit Function
Failed:
Err.Clear
Set Wb = Nothing
IsWbOpen = False
End Function

And furthermore, if you do find one that is open, is there a way to
force that open workbook to save? Maybe by initializing a local
function or sub on the open workbook which just saves the sheet?


Public Sub SaveWorkbookIfOpen(ByVal Name As String)
Dim Wb as Workboook
If IsWbOpen(Name, Wb) Then
Wb.Save
End If
End Sub

Also, what is the VBA code to open a workbook minimized?


Public Function OpenWorkbookMinimised(ByVal Name As String) As Workbook
' This will cause an error if the workbook Name cannot be opened (e.g.
it doesn't exist, isn't a valid workbook etc.)
Set OpenWorkbookMinimised = Workbooks.Open(Name)
' Opening a workbook causes it to become the active window:
' There is probably a better way of doing this
ActiveWindow.WindowState = xlMinimized
End Function

HTH,

Stewart