Jump to content


Photo

Try Catch Issue


  • Please log in to reply
1 reply to this topic

#1 Mickers

Mickers
  • Members
  • 1229 posts

Posted 24 January 2012 - 03:32 PM

Either this is a bug or I'm missing something. Here's my code:
ControlGet, hwnd, hwnd, , Excel71, ahk_class XLMAIN
if !(Xl := Acc_ObjectFromWindow(hwnd, -16).Application) {
	Msgbox, 262144, @(>_<)@, No active Excel sheet.
	ExitApp
}
loop
	row := A_Index
until (xl.range("a" . A_Index).value = "")

f2::
Try xl.range("a" . row).value := "testing"
Catch
	Xl := Xl_Conn() ; returns zilch
return

Xl_Conn() {
	ControlGet, hwnd, hwnd, , Excel71, ahk_class XLMAIN
	if !(Xl := Acc_ObjectFromWindow(hwnd, -16).Application)
		Msgbox, 262144, @(>_<)@, No active Excel sheet.`n`nOpen a spreadsheet and try again.
	else return Xl
}

Acc_ObjectFromWindow(hWnd, IdObject = -4) {
	static h
	if not h
		h := DllCall("LoadLibrary","Str","oleacc","Ptr")
	if DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", IdObject&=0xFFFFFFFF, "Ptr", -VarSetCapacity(IID, 16)+NumPut(IdObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81, NumPut(IdObject==0xFFFFFFF0?0x0000000000020400:0x11CF3C3D618736E0,IID, "Int64"), "Int64"), "Ptr*", pacc)=0
	return ComObjEnwrap(9, pacc)
}
It should attempt the first empty cell to "testing" otherwise call the function that relinks the workbook. I tested this by using the hotkey on an open workbook, closing the workbook, hitting hotkey again which causes the error message, then I open the workbook again, and use the hotkey. The msgbox does not trigger but the workbooks cell isn't set. In the error log I just see the Try trigger but it skips the Catch. What's up?

Hope I'm not wasting anyones time. :oops:

#2 sinkfaze

sinkfaze
  • Moderators
  • 6087 posts

Posted 24 January 2012 - 05:24 PM

I think what you're trying is silently failing. Try testing the validity of the pointer instead.