Excel Com Object Causing script to hang on exit?

Get help with using AutoHotkey and its commands and hotkeys
JackPJ
Posts: 18
Joined: 19 Oct 2016, 05:52

Excel Com Object Causing script to hang on exit?

01 Dec 2016, 12:25

Hi all,

I have a really strange issue with my program right now that I've never heard nor come across before.

I have a gui, containing some buttons and lists.

The buttons take the selected item from the list, open an excel doc (comobjcreate), run a macro with the list item as the variable then save as a pdf and close the excel doc without changes.

The issue is that after I press one of those buttons to run off a pdf, when I eventually go to close the program it hangs for up to 30 seconds before closing. (doesn't matter if I run off 1 or 100 pdfs).

I've narrowed it down to:
- The script decides to re-launch an excel process in the background,
- going all out on my cpu (max usage on that core) for 20-30 seconds (For the excel.exe process)
- It then closes the excel process and the script.

I can only guess it is caused by me not handling the com object correctly when closing it and ahk decides to do something itself to terminate it before closing?


(To confirm though, If i just open the gui, do not press any buttons then close it, it closes fine)

As the button is what causes the issue, Heres the code from the button (The whole script is 1,200 lines so not worth posting everything)
If anyone could take a look , it would be much appreciated :)!!

Code: Select all

StockPricingActive:
gui, submit, nohide

if (NominalCode = "")
{
msgbox,,,No Account selected
return
}

StockQuery := "SELECT     SL.CUCODE FROM         dbo.SL_ACCOUNTS AS SL LEFT OUTER JOIN                       dbo.SL_ANALYSIS AS SA ON SL.CUCODE = SA.SACODE LEFT OUTER JOIN                       dbo.NL_ACCOUNTS AS NL ON NL.NCODE = SA.SANOMINALCR WHERE (NL.NCODE='" . NominalCode . "')"
StockAccountCodeObj := ADOSQL(ConnectionString,StockQuery)

StockAccountCode := StockAccountCodeObj[2,1]
;~ msgbox,,,%StockAccountCode%

;~ StockListQuery := "SELECT STKCODE, STKNAME, "
LRLocation := A_ScriptDir . "\" . "Stock_WithCostActive" . ".xlsm"
xl := ComObjCreate("Excel.Application")
xl.Visible := True
path = %LRLocation%
xl_workbook := xl.WorkBooks.Open(Path)
xl.Range("B1").value := StockAccountCode
xl.Run("GetDataFromADO")
xl.DisplayAlerts := False
xl.sheets("Printout").select
xl.Range("A1").Copy
xl.Range("A1").PasteSpecial(-4163)
xl.sheets("Connectionstring").delete

SaveLoc := A_MyDocuments . "\" . StockAccountCode . "- Price List.xlsx"	
Xl.ActiveWorkBook.SaveAs(SaveLoc,51)

xl.ActiveSheet.ExportAsFixedFormat(0,StockAccountCode . "- Price List.pdf",0,True,False,1,100,True)
xl_workbook.Save()
xl_workbook.Close(1)

xl.quit
xl.DisplayAlerts := True
return

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], casperharkin, Google [Bot], Odlanir, SOTE, teadrinker, translatorgreek and 154 guests