COM object script works inconsistently

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jp7
Posts: 5
Joined: 16 Oct 2018, 19:55

COM object script works inconsistently

27 Jan 2019, 14:15

Hi I've been using a script (which I believe I found on the forum here) to highlight text in Word. Nothing fancy but saves time.

It worked at first, now it frequently doesn't (but sometimes does) and I am at a loss as to why.

Note that currently the tester line at the top (Numpad1::MsgBox, hello) works, but not the highlighting.

Any ideas what I could fix? Thanks!

Code: Select all

#SingleInstance force

Numpad1::MsgBox, hello

#1::
!Numpad1::
oWord := ComObj("Word.Application")	; MS Word object
oWord.Selection.Range.HighlightColorIndex := 7 ;highlight color
return


!Numpad2::
oWord := ComObj("Word.Application")	; MS Word object
oWord.Selection.Range.HighlightColorIndex := 4 ;highlight color
return

!Numpad3::
oWord := ComObj("Word.Application")	; MS Word object
oWord.Selection.Range.HighlightColorIndex := 3 ;highlight color
return

!Numpad4::
oWord := ComObj("Word.Application")	; MS Word object
oWord.Selection.Range.HighlightColorIndex := 8 ;highlight color
return

!Numpad5::
oWord := ComObj("Word.Application")	; MS Word object
oWord.Selection.Range.HighlightColorIndex := 6 ;highlight color
return

!Numpad6::
oWord := ComObj("Word.Application")	; MS Word object
oWord.Selection.Range.HighlightColorIndex := 16 ;highlight color
return

;oWord.Selection.Range.HighlightColorIndex := 
;1 - black
;2 - blue
;3 - turquoise
;4 - green
;5 - pink
;6 - red
;7 - yellow
;8 - blank - no color
;9 - dark blue
;10 - teal
;11 - dark green
;12 - violet
;13 - dark red
;14 - dark yellow
;15 - gray 50%
;16 - gray 25%
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: COM object script works inconsistently

27 Jan 2019, 14:34

At 1st glance you really shouldn't have to create a new com object for every hotkey ( unless you're opening/closing word during AHk's runtime ).

Try placing one object instance at the top of the script

Code: Select all

#SingleInstance force
oWord := ComObj("Word.Application")	; MS Word object
; ^^^ remove the rest below
; ......
( untested ) htms
User avatar
Datapoint
Posts: 295
Joined: 18 Mar 2018, 17:06

Re: COM object script works inconsistently

27 Jan 2019, 14:40

Code: Select all

#NoEnv
#SingleInstance force

#1::
!Numpad1::
oWord := ComObjActive("Word.Application")	; MS Word object
oWord.Selection.Range.HighlightColorIndex := 7 ;highlight color
; Clear global COM variables when you are done with them. (or don't save them at all, as in the next example)
; If you have a reference to a Word object saved in a variable, then your script will keep Word open in the background, 
; even if you closed the window already. It will stay open until you clear the variable. It will interfere with 
; ComObjActive in the future beause you have two instances of word open. ComObjActive might not get the one you 
; want. You can check the Windows task manager to see if you have more than one Word process open. If you have more than
; one Word process then ComObjActive will have problems.
oWord := "" ; clear com vars. not required for local vars (ie vars in functions)
return

!Numpad2::ComObjActive("Word.Application").Selection.Range.HighlightColorIndex := 4 ;highlight color

!Numpad3::ComObjActive("Word.Application").Selection.Range.HighlightColorIndex := 3 ;highlight color

; Note: You can also use any BGR value. ex: 0x0000FF is red, 0xFF0000 is blue etc.

;oWord.Selection.Range.HighlightColorIndex := 
;1 - black
;2 - blue
;3 - turquoise
;4 - green
;5 - pink
;6 - red
;7 - yellow
;8 - blank - no color
;9 - dark blue
;10 - teal
;11 - dark green
;12 - violet
;13 - dark red
;14 - dark yellow
;15 - gray 50%
;16 - gray 25%
Last edited by Datapoint on 27 Jan 2019, 14:43, edited 1 time in total.
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: COM object script works inconsistently

27 Jan 2019, 14:43

Datapoint wrote:
27 Jan 2019, 14:40

Code: Select all

!Numpad2::ComObjActive("Word.Application").Selection.Range.HighlightColorIndex := 4 ;highlight color

!Numpad3::ComObjActive("Word.Application").Selection.Range.HighlightColorIndex := 3 ;highlight color
these will not work as objects in AHk must be assigned.
User avatar
Datapoint
Posts: 295
Joined: 18 Mar 2018, 17:06

Re: COM object script works inconsistently

27 Jan 2019, 14:44

TLM wrote:
27 Jan 2019, 14:43
Datapoint wrote:
27 Jan 2019, 14:40

Code: Select all

!Numpad2::ComObjActive("Word.Application").Selection.Range.HighlightColorIndex := 4 ;highlight color

!Numpad3::ComObjActive("Word.Application").Selection.Range.HighlightColorIndex := 3 ;highlight color
these will not work as objects in AHk must be assigned.
u sure?
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: COM object script works inconsistently

27 Jan 2019, 15:18

not sure lol blame the snow I'm buried in lol my bad
but why not use 1 object??
User avatar
Datapoint
Posts: 295
Joined: 18 Mar 2018, 17:06

Re: COM object script works inconsistently

27 Jan 2019, 15:24

TLM wrote:
27 Jan 2019, 15:18
but why not use 1 object??
In case you close Word and open another instance before you reload the script?
jp7
Posts: 5
Joined: 16 Oct 2018, 19:55

Re: COM object script works inconsistently

30 Jan 2019, 10:41

Thanks very much for you help @TLM and @Datapoint. As you can see I am *very* new to this.

I haven't had time to try solutions yet, *but if I understand correctly multiple instances of Word running is what is causing problems in my current script?* And if I load the script after opening Word (as I just did) it should work find (as it currently is).

I will look more closely at your more robust solutions. I'm a translator so am constantly opening new Word docs all day...

Again, THANKS!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: aitaixy, dipahk and 216 guests