Close Excel Workbook that is Open, and not the first instance.

Close Excel Workbook that is Open, and not the first instance.

C_Haines_ENG
Collaborator Collaborator
1,546 Views
21 Replies
Message 1 of 22

Close Excel Workbook that is Open, and not the first instance.

C_Haines_ENG
Collaborator
Collaborator

Hello all, I'm going to unbury this monster because its driving me completely insane!

 

So I've figured out how to get the first instance of an excel application, but this code will not work if its not the first excel instance open. GetObject(,"Excel.Application") will only get the first instance of an excel application and it would be very common to have another engineer have another excel document open before running my ilogic script.

 

Is there a way to find if an excel workbook is open no matter if its the first or 30th instance of excel, and close the application attached to it, delete it, and then create a new one to save to that location.

 

Attached below is my code:

 

BOMPath = "C:\BOM Calculator\PART_BOM.xlsx" 'FINAL DESTINATION OF BOM RESULTS

'CREATE CALCULATOR FOLDER IF IT DOESNT EXIST
If Not System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(BOMPath)) Then
    System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(BOMPath))
	
End If

If System.IO.File.Exists(BOMPath)

	ExcelApp = GetObject(BOMPath).Application
	
	ExcelApp.Range("B2") = "PART #    "
	
	ExcelWB = ExcelApp.Workbooks("PART_BOM.xlsx")
	ExcelWB.Close
	ExcelApp.Quit
	
	ExcelApp = Nothing
	
	My.Computer.FileSystem.DeleteFile(BOMPath)
	
End If
	


'[ CREATE EXCEL APPLICATION, INSERT HEADER, AND CLOSE PREVIOUS INSTANCE IF STILL OPEN
	
If System.IO.Directory.Exists(BOMPath)
	My.Computer.FileSystem.DeleteFile(BOMPath)
End If
	
ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = True
ExcelWB = ExcelApp.Workbooks.Add
ExcelWB.SaveAs(BOMPath)

AppActivate("PART_BOM")

ExcelApp = Nothing

Despite what the documentation says:

ExcelApp = GetObject(BOMPath).Application

Will CREATE a hidden background process if the file isnt open, driving me completely insane.

 

I cant figure this out, and when I get close it just closes every single excel application thats open. Please help. 

0 Likes
Accepted solutions (1)
1,547 Views
21 Replies
Replies (21)
Message 21 of 22

mat_hijs
Collaborator
Collaborator

I see the difference now. I think GoExcel still opens and closes the workbook in the background (although like I said I feel like that handles the background processes), but that doesn't matter anyway since I'm not using iLogic so I don't have access to GoExcel. There's probably other ways to do this but it's not worth it in my case to search for that. I don't have any issues with opening and closing and performance also isn't an issue in my case since it'll only take a couple of seconds. I'm going by the saying "If it ain't broke, don't fix it", especially because it already took me long enough to get it working in the first place.

0 Likes
Message 22 of 22

C_Haines_ENG
Collaborator
Collaborator

Amen. 

0 Likes