Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to pass COM object pointer to a script


  • Please log in to reply
11 replies to this topic
xaxidk
  • Members
  • 2 posts
  • Last active: Mar 01 2013 04:51 AM
  • Joined: 22 Jul 2007
I'm using AHK_L. I have a pointer to an Excel object and I'd like to pass the pointer to another script so that the second script can control the same Excel object. I've tried the following method, but it doesn't work:

Script1.ahk: This creates the Excel object
oExcel := ComObjCreate("Excel.Application")
run Script2.ahk %oExcel%

Script2.ahk: This should make the Excel object visible, but it doesn't
oExcel = %1%
oExcel.Visible := 1

Any help would be greatly appreciated.

  • Guests
  • Last active:
  • Joined: --
I suggest you to merge your two scripts in one.

#
  • Guests
  • Last active:
  • Joined: --

I have a pointer to an Excel object and I'd like to pass the pointer to another script

You cannot. The pointer is not valid outside of the current process.

run Script2.ahk %oExcel%

Command lines are purely strings. You cannot pass objects.


Perhaps you can pass the HWND of the Excel window to the other script, and it can use the HWND to identify which Excel to work with. oExcel.Hwnd gives you the HWND. I think Acc_ObjectFromWindow will give you the object, but I don't have Excel.

krapot
  • Guests
  • Last active:
  • Joined: --
Hi everyone

May be is it possible to use NumGet to have the base adress of the object and to pass it to the other script ?

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
You can retrieve the "base address" or pointer (using CombjUnwrap()), you can pass it, the 2nd script can read it - but it's worthless. As already mentioned, the pointer is not valid in another process. So any attempt to get an object from it or to call a method will fail.

The suggestion with the HWND might be more helpful. However, I'm not sure Acc_* will help you...

Also the second script might try to simply use ComObjActive("Excel.Application"). However, I'm not sure when an object is registered as active (e.g. does then window have to be active, too?).
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

xaxidk
  • Members
  • 2 posts
  • Last active: Mar 01 2013 04:51 AM
  • Joined: 22 Jul 2007
ComObjActive("Excel.Application") won't work properly if there's more than one instance of Excel open, which happens to be the case here.

I think I'm just going to use a third script to coordinate the other two scripts. Thanks for the help everyone.

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

ComObjActive("Excel.Application") won't work properly if there's more than one instance of Excel open

It's recommended to use oExcel := Excel_Get() instead of oExcel := ComObjActive("Excel.Application") to access the Excel object for a few reasons - when you use oExcel := ComObjActive("Excel.Application");
• if there is more than 1 running Excel process, it will get the first registered on the ROT (Running Object Table).
• if the Excel process hasn't been registered on the ROT (if it was just opened), you will get a Com Error.
• you will get an error if the worksheet is being edited
oExcel := Excel_Get()		; access the Excel object(that's better way than oExcel := ComObjActive("Excel.Application"))

#Include Acc.ahk			; by Sean and Jethrow	http://www.autohotkey.com/forum/viewtopic.php?t=82767

Excel_Get(WinTitle="ahk_class XLMAIN") {	; by Sean and Jethrow, minor modification by Learning one
	ControlGet, hwnd, hwnd, , Excel71, %WinTitle%
	if !hwnd
		return
	Window := Acc_ObjectFromWindow(hwnd, -16)
	Loop
		try
			Application := Window.Application
		catch
			ControlSend, Excel71, {esc}, %WinTitle%
	Until !!Application
	return Application
}	; http://www.autohotkey.com/forum/viewtopic.php?p=492448#492448


tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Apr 19 2019 05:49 PM
  • Joined: 21 Dec 2007
I prefer to use ComObjGet(path)
Never lose.
WIN or LEARN.

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
ComObjGet(path) is nice, but if I'm right, you can't use it if you want to access new workbook that isn't saved yet.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Apr 19 2019 05:49 PM
  • Joined: 21 Dec 2007
your correct but why work with an unsaved file?
Never lose.
WIN or LEARN.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
ComObjGet can get the Book if it's registered on the ROT:
Run Excel, , , pid

MsgBox, 262144, , Wait for Excel ...

ControlGetText, WorkBookName, Excel71, ahk_pid %pid%

Book := ComObjGet(WorkBookName)

Book.ActiveSheet.Range("A1").Value := "Cell A1"


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
@tank: Why not? :) But jethrow proved we were wrong about ComObjGet().
@jethrow: Thanks for the info! :)