spawning com objects naturally

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

spawning com objects naturally

14 Apr 2021, 13:02

I am trying to control my 3d modeling program with com and ran into some issues. If I spawn the application with comObjCreate("SldWorks.application") it creates creates an instance of the application, but it does not behave normally. When I double click files that should open in this application they fail to launch. I can simulate the problem with excel com. Create a test excel spreadsheet and save it to disk. Then spawn excel using

Code: Select all

xl := comObjCreate("excel.application")
xl.visible := true
then double click the test spreadsheet. It will spawn new instance of excel instead of opening in the existing instance. How can I make the com application behave like a naturally launched program?
User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: spawning com objects naturally

14 Apr 2021, 13:19

After what you have shown, you can use xl.Workbooks.Open("C:\MyPath\MyFile.xlsx") to open a particular file, or you can use xl.Workbooks.Add to open a new workbook.

Or you can connect to an already open instance using xl := ComObjActive("Excel.Application") instead of using ComObjCreate.
colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: spawning com objects naturally

14 Apr 2021, 14:08

Thanks for the suggestion. My current solution is to connect to existing instance. I always have to run the following code to guarantee the program will interact normally with the rest of the system once my script is done. I was wondering if my catch statement could be transferred to a com call.

Code: Select all

getSolidworksHandle(file := "")
{	
	try
	{
		swApp := ComObjActive("SldWorks.Application")		
	}
	catch
	{
		run % getSolidworksPath() ;launch SLDWORKS.exe
		DHW := A_DetectHiddenWindows
		detecthiddenwindows off
		loop ;wait for stable state
		{
			Process, Exist, % "SLDWORKS.exe"
			if(ErrorLevel > 0)
			{
				WinGetTitle,procTitle,ahk_pid %ErrorLevel%
				;tooltip % procTitle
				if(instr(procTitle,"SOLIDWORKS "))
				{
					break
				}
			}
			sleep 100
		}
		DetectHiddenWindows, %DHW%
		swApp:=ComObjActive("SldWorks.Application")		
	}
	if(file)
	{
		type := 1
		swApp.openDoc(file,type)
	}
	return swApp
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Google [Bot], Holarctic, Rohwedder and 204 guests