COM change font of highlighted text

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
robinson
Posts: 214
Joined: 12 Sep 2019, 20:28

COM change font of highlighted text

16 May 2024, 20:45

Hi, noob here.
Could someone write me a bit of COM
to automatically change the selected text to the Arial font?
Thanks!
User avatar
mikeyww
Posts: 27243
Joined: 09 Sep 2014, 18:38

Re: COM change font of highlighted text

16 May 2024, 21:18

I can do even better than that: just search the forum to find the code that is already written for you.

viewtopic.php?p=379452#p379452

I've never encountered a noob with 206 posts! ;)
robinson
Posts: 214
Joined: 12 Sep 2019, 20:28

Re: COM change font of highlighted text

17 May 2024, 05:49

@mikeyww
Hi, thanks for the reply.
I cut this part out of the post (and changed it to Arial):

Code: Select all

^F1::
	oWord := ComObjActive("Word.Application")
	oWord.Selection.Font.Name := "Arial"
return
But it doesn't work for selected text; only when nothing is selected.
User avatar
boiler
Posts: 17281
Joined: 21 Dec 2014, 02:44

Re: COM change font of highlighted text

17 May 2024, 06:00

It works for me as-is on selected text. By the way, you posted it as v1 code, but you posted in the v2 section of the forum. The main lines also work in v2 with the other syntax changed:

Code: Select all

#Requires AutoHotkey v2.0
^F1:: {
	oWord := ComObjActive("Word.Application")
	oWord.Selection.Font.Name := "Arial"
}
robinson
Posts: 214
Joined: 12 Sep 2019, 20:28

Re: COM change font of highlighted text

17 May 2024, 07:51

@mikeyww @boiler
I tried changing from selected Times New Roman to Arial and it worked fine.
But I know what the problem is.

See I don't like Chinese-style punctuation, even on Chinese text.
And I found that literally all Chinese punctuation except for double quotation marks, are their own unique symbols.
But the double quotation marks aren't, for some reason.
The Chinese double quotation marks are just a font choice.
And if you select the Chinese text with Chinese quotation marks,
and you manually change it to Arial from Word's drop-down menu, it changes them to regular Arial quotation marks.
But for some reason doing so with this code has no effect.
Any ideas?
Here I made a .gif showing what I'm talking about. (z is the hotkey trigger.)
Keep your eyes on the double quotation mark. See how the menu affects it but the AHK code doesn't?
Image
User avatar
boiler
Posts: 17281
Joined: 21 Dec 2014, 02:44

Re: COM change font of highlighted text

17 May 2024, 08:03

robinson wrote: But for some reason doing so with this code has no effect.
Any ideas?
There must be something more that Word is doing when selecting a different font from the dropdown than just applying a new font to the entire selection like happens via COM, so the only thing I can suggest is trying to automate the user interface to change the font via the dropdown.
robinson
Posts: 214
Joined: 12 Sep 2019, 20:28

Re: COM change font of highlighted text

18 May 2024, 11:29

@boiler
I tried looking up font-changing code on VBA forums for clues,
and I found this.
Does this mean anything to you?
Image
User avatar
boiler
Posts: 17281
Joined: 21 Dec 2014, 02:44

Re: COM change font of highlighted text

18 May 2024, 12:05

I suppose that could help address the issue, but I don’t have experience with changing the language.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Chunjee, kunkel321 and 40 guests