I use some simple AutoHotkey code which contains VBA to change text color, highlight etc. Examples:
#IfWinActive ,ahk_group Word ; bold and red
Ralt & 7::
oWord := ComObj("Word.Application")
oWord.Selection.Font.Bold := 1 ; bold
oWord.Selection.Font.color := 0x0000FF ; red color
Return
#IfWinActive ,ahk_group Word ; highlight green
!1::
oWord := ComObj("Word.Application")
oWord.Selection.Range.HighlightColorIndex := 4 ; green
Return
For a while, I noticed that my hotkeys stopped working after switching to different opened files. After some more testing, I realized that if I open a file from within the first one, it remains in the same instance, but opening another file directly from Explorer will open a different instance of Word.
Using my hotkeys in the second file/instance has no effect, but if I use them in the first file/instance, they work. However, when afterward switching to the second file/instance again, not only do they still not affect the second file/instance, but they change the first file/instance, even if it's not focused or minimized. So, I may try to highlight a word or change it's color in the second file, only to find out that the word the cursor was at in the first file will have changed in the background.
As far as I can tell, Excel doesn't (yet?) use different instances, or at least the pid (as seen from within AutoHotkey Window Spy) stays the same, so I haven't noticed the same problem there.
Is there any way around this, as in using a different syntax or forcing the code to run in the focused window and actually have it work even if it's a different instance of Word? Thanks in advance.
Thinking it might have been an AutoHotkey thing, I also posted about this in that forum (https://www.autohotkey.com/boards/viewtopic.php?f=76&t=106763), where more than one person said that Word isn't supposed to open new files in new processes and one user said it might be some registry setting.
This made me realize that at one point not too long ago, I started searching for a solution to the default behavior of Word, where if a (first) document is opened, even if it's minimized, opening a new document always focuses the other (first) document before showing the new one, even if the first document is minimized. Excel, for example, doesn't do this (by default).
What I found entailed making a small registry tweak, which, even though had the desired result, in this case it also has had the unintended (or maybe intended) consequence of every new directly opened file (i.e. from Explorer, attachment etc.) having a different instance/process.
For reference purposes, it's in HKEY_CLASSES_ROOT\Word.Document.12\shell\Open\command, where you add a /q right after the path and before the /n, so instead of (for me):
"C:\Program Files (x86)\Microsoft Office\Root\Office16\WINWORD.EXE" /n "%1" /o "%u"
it's
"C:\Program Files (x86)\Microsoft Office\Root\Office16\WINWORD.EXE" /q /n "%1" /o "%u"
So, reverting this change, as in deleting the /q in the string, while bringing back the previous slightly annoying behavior, also fixed this.
I highly recommend reading the selected answer in that linked forum post, where someone suggested another slightly more complex way of achieving this result, while also possibly accounting for multiple processes. Thanks for the comments.