Excel-COM - Issue with ActiveCell Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Excel-COM - Issue with ActiveCell

14 Nov 2018, 19:10

Hello guys,

Sometimes I face the problem that the ActiveCell doesn't get recognized.
A simple example:

Code: Select all

NumPad1::
xl := ComObjActive("Excel.Application") ; Connection to currently active Excel instance
ce := xl.ActiveCell ; Active cell
ce.font.color := 0x00FFFF ; Set font color to yellow
return

NumPad2::
xl := ComObjActive("Excel.Application") ; Connection to currently active Excel instance
ce := xl.ActiveCell ; Active cell
ce.font.color := 0xF0B000 ; Set font color to blue
return

NumPad3::
xl := ComObjActive("Excel.Application") ; Connection to currently active Excel instance
ce := xl.ActiveCell ; Active cell
ce.font.color := 0x50B000 ; Set font color to green
return

NumPad4::
xl := ComObjActive("Excel.Application") ; Connection to currently active Excel instance
ce := xl.ActiveCell ; Active cell
ce.font.color := 0xFFFFFF ; Set font color to white
return
The script generally works, but after switching to another cell manually,
this cell does not get colored but the currently active one.

Here you can see that not always the font color of the active cell gets changed,
but the font color of the previously selected one.
Image

The color of text2 never got changed in this example.
What is the issue for that?

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Excel-COM - Issue with ActiveCell  Topic is solved

14 Nov 2018, 20:04

You only need to connect to Excel once

Code: Select all

xl := ComObjActive("Excel.Application") ; Connection to currently active Excel instance
return

NumPad1::
ce := xl.ActiveCell ; Active cell
ce.font.color := 0x00FFFF ; Set font color to yellow
return

NumPad2::
ce := xl.ActiveCell ; Active cell
ce.font.color := 0xF0B000 ; Set font color to blue
return

NumPad3::
ce := xl.ActiveCell ; Active cell
ce.font.color := 0x50B000 ; Set font color to green
return

NumPad4::
ce := xl.ActiveCell ; Active cell
ce.font.color := 0xFFFFFF ; Set font color to white
return
but after switching to another cell manually,
this cell does not get colored but the currently active one.
how do you switch to another cell and it is not the active one?
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: Excel-COM - Issue with ActiveCell

14 Nov 2018, 21:50

I tried your script and it worked fine. Could not reproduce the problem.
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Excel-COM - Issue with ActiveCell

15 Nov 2018, 14:36

Hi,

Okay, after removing the
xl := ComObjActive("Excel.Application") ; Connection to currently active Excel instance
from the scripts, it works well.

Even with or without the return after
xl := ComObjActive("Excel.Application") ; Connection to currently active Excel instance
it works.

Code: Select all

;Excel-FontText
#SingleInstance force
xl := ComObjActive("Excel.Application") ; Anbindung an die aktive Excel-Instanz

NumPad1::
ce := xl.ActiveCell ; Anbindung an die aktive Zelle
ce.font.color := 0x00FFFF ; Zelle gelb einfärben
return

NumPad2::
ce := xl.ActiveCell ; Anbindung an die aktive Zelle
ce.font.color := 0xF0B000 ; Zelle blau einfärben
return

NumPad3::
ce := xl.ActiveCell ; Anbindung an die aktive Zelle
ce.font.color := 0x50B000 ; Zelle grün einfärben
return

NumPad4::
ce := xl.ActiveCell ; Anbindung an die aktive Zelle
ce.font.color := 0xFFFFFF ; Zelle weiß einfärben
return
Thanks, guys!

Edit
To answer the question:
I switch cells mostly by using the arrow keys.
In rare situations I click with the mouse on cells.
Anyway, both things work perfectly now (regarding the color change.)
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 242 guests