Closing an Open Workbook

jdlc

Registered User.
Local time
Today, 15:19
Joined
Mar 26, 2013
Messages
56
I have a workbook which is already opened and I want to close it. the code that i have is (see below) is not working. Anyone can share some inputs?

Code:
Function CloseExcel(sExcelFile As String) As Integer
Dim XLapp As New Excel.Application
Dim ObjXL As Excel.Workbook
    
On Error GoTo ErrorTrap
    Set ObjXL = XLapp.Workbooks.Open(sExcelFile)
    ObjXL.Application.Visible = True
    ObjXL.Windows(1).Visible = True
    ObjXL.Close
    Set ObjXL = Nothing
    Exit Function
ErrorTrap:
    Debug.Print "(" & Err.Number & ") " & Err.Description
End Function
 
what the code is doing it opening again that excel file (which is already opened) and close it. is this something about the new excel.application that i declared?
 
Look into GetObject()
 
Look into GetObject()
as usual thanks for the tip boss: here is the code.

Code:
Function CloseExcel(fileName As String) As Integer
    
Dim excelObj As Object
On Error GoTo ErrorTrap
    Set excelObj = GetObject(fileName)
    excelObj.Close
    Set excelObj = Nothing
ExitTrap:
    CloseExcel = Err.Number
    Exit Function
    
ErrorTrap:
    Debug.Print "(" & Err.Number & ") " & Err.Description
    Resume ExitTrap
   
End Function
 

Users who are viewing this thread

Back
Top Bottom