- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
Solved! Go to Solution.