Jump to content

ObjGet using PID


Recommended Posts

I know I've been staring at my script for too long, because I can't figure this out. :mad2:

I want to attach to a specific instance of excel, preferable using the PID.

ObjGet( ) even says that it can attach to an object using a given PID, but I can't get it to work.

A point in the right direction would be greatly appreciated 

Link to comment
Share on other sites

  • Moderators

@help_me_please how about showing us this code you're staring at, so we can see just what you're doing? Have you looked at the _Excel_* sections of the help file? You can easily open, create, or edit specific Workbooks with these functions.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I want to run a macro within an excel workbook, and I've seen that I must call the excel object using "$oExcel.Run("Macro_Name_Here")".

This macro is a pain, as it uses the currently selected cell as a base to begin an interpolation.

To complicate things further, there will be at least one other excel instance open when attempting to run said macro.

Therefore, I need to select a specific excel object to attempt to execute the macro in.

 

It would go this this...

$Excel_Proccess_ID = WinGetProcess("Excel_Workbook_Title_Name_Here")

$Excel_Object = ObjGet("",$Excel_Proccess_ID) ;  < -- This is the problem

$Excel_Object.Run("Macro_Name_Here")

Link to comment
Share on other sites

Not sure if this helps - i've probvably oversimplified your problem. I've  3 excels open on my laptop, one of them called "ThisExcel.xlsx"  and in that sheet is a macro that just moves the cursor and puts some text in a cell, called Macro1.  This runs it OK:

#include <Excel.au3>
$oExcel=_excel_open()
$oExcel.run("thisexcel.xlsx!Macro1")

Do you not know the name of the spreadsheet at the time it runs ?

Link to comment
Share on other sites

 

3 minutes ago, Jimwright said:

Not sure if this helps - i've probvably oversimplified your problem. I've  3 excels open on my laptop, one of them called "ThisExcel.xlsx"  and in that sheet is a macro that just moves the cursor and puts some text in a cell, called Macro1.  This runs it OK:

#include <Excel.au3>
$oExcel=_excel_open()
$oExcel.run("thisexcel.xlsx!Macro1")

Do you not know the name of the spreadsheet at the time it runs ?

I do know the name of the excel workbook at the time of run.

when you say that you have 3 excels open, do you have three workbooks within the same instance? or three instances of excel?

check the task manager to verify this.

Link to comment
Share on other sites

1 minute ago, help_me_please said:

 

I do know the name of the excel workbook at the time of run.

when you say that you have 3 excels open, do you have three workbooks within the same instance? or three instances of excel?

check the task manager to verify this.

I have 3 Excels open in taskmanager

Link to comment
Share on other sites

The problem is that I have two excel instances open, each with there own workbook, such as (from the task manager)

\/ Microsoft Excel (32bit)

>Workbook2

\/ Microsoft Excel (32bit)

>Workbook1

 

Workbook1 is open initially to read from and write to, and needs to remain open.

Workbook2 is an output that opens in a new instance that the macro needs to run in.

 

When I use

$oExcel = _Excel_Open()

$oExcel.Run("Workbook2.xlsm!macro1")

I get the notification in Workbook1 that Workbook2 is already open, prompting me to open as a read only or to notify.

 

I need to ensure that I target the instance of excel containing Workbook2 as the $oExcel object to call the run function.

I can find the PID based on title name, but am having trouble using the ObjGet( ) function to target a process by PID

Link to comment
Share on other sites

^^^

1 hour ago, Jimwright said:

I have 3 Excels open in taskmanager

 

3 hours ago, JLogan3o13 said:

@help_me_please how about showing us this code you're staring at, so we can see just what you're doing? Have you looked at the _Excel_* sections of the help file? You can easily open, create, or edit specific Workbooks with these functions.

 

Link to comment
Share on other sites

  • Moderators

Don't bump the thread again for another 24 hours or we'll lock it permanently.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I still think you can use the built in excel functions to help you here.   I've got 2 spreadsheets - Workbook1.xlsm just has "hello" in A1.   ThisExcel.xlsm just has the Macro1 I mentioned earlier.   With no Excels currently running I run this:

#include <Excel.au3>
$oExcel=_excel_open()
;;Open the Input Workbook
$StrInputWB="C:\work\useful script\autoit\excelhelp\Workbook1.xlsm"
 $oInputWB=_Excel_BookOpen($oExcel, $StrInputWB)
 ;;Open the Output Workbook
$StrOutputWB="C:\work\useful script\autoit\excelhelp\ThisExcel.xlsm"
 $oOutputWB=_Excel_BookOpen($oExcel, $StrOutputWB)
;EG - activate input and do something
$oInputWB.activate
$ReadSomething=_Excel_RangeRead ($oInputWB,default,"A1")
consolewrite($ReadSomething) ;prove we read from input excel
;;activate output Excel
$oOutputWB.activate
$oExcel.run("Macro1") ;runs macro in the output book

Two Excels launch.   I then get "Hello" in the stdout stream to prove it read from input,  The macro then runs on the output Excel spreadsheet.

Link to comment
Share on other sites

  • 1 year later...

The problem is that I have two excel instances already open, each with a corresponding workbook.

One excel instance is open first, then the second one is automatically opened in another instance (can not change this).

I need to attach to the second instance of excel to run a macro with the workbook.

But when I use the _Excel_Open() function, I get the first excel instance rather than the second instance.

I know the workbook names and can get the PIDs for each instance.

 

Any ideas of how to attach to the second instance specifically?

Link to comment
Share on other sites

Please have a look at the examples for _Excel_BookAttach in the help file.
If your script still doesn't work, then please post your script here.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...