COM change Language ID in Word

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

COM change Language ID in Word

18 May 2024, 14:42

Hi, noob here.
I was trying to change text from one font to the other, and this worked:

Code: Select all

oWord := ComObjActive("Word.Application")
oWord.Selection.Font.Name := "Arial"
But it wasn't useful for what I wanted,
because I wanted to change Chinese punctuation into Western-style punctuation,
which does work if you just choose a font from the drop-down menu in Word,
but which doesn't work if you use the above code.
So I went hunting on the Visual Basic forums for clues, and I found this:
Image
I think this may be why the code doesn't work,
because I think that as well as a font name, every font may have a “Language ID”.
Does anyone know how to change language ID in COM?
Thanks!
User avatar
Datapoint
Posts: 303
Joined: 18 Mar 2018, 17:06

Re: COM change Language ID in Word

18 May 2024, 16:34

This is the code shown in your image and ported to AHK. The language ID's are listed at the link in the comments.

Code: Select all

oWord := ComObjActive("Word.Application")
wdRussian := 1049 ; https://learn.microsoft.com/en-us/office/vba/api/Word.wdlanguageid
oWord.Selection.WholeStory
oWord.Selection.LanguageID := wdRussian
oWord.Selection.Font.Size := 12
oWord.Selection.Font.Name := "Georgia"
*Edited - removed the line that saved the selection (objsel) because Selection.WholeStory could change the selection (I didn't test anything).
robinson
Posts: 217
Joined: 12 Sep 2019, 20:28

Re: COM change Language ID in Word

18 May 2024, 18:28

@Datapoint
Thanks for the help
but it's still not changing the Chinese punctuation
the way simply selecting “Arial” from the menu does.
Is there anything else language-related Word could be doing here?
User avatar
Seven0528
Posts: 399
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: COM change Language ID in Word

18 May 2024, 23:57

 I don’t know much about this issue, but I wonder if changing fonts and replacing the period character are related. In Chinese or Japanese, the ideographic full stop is used, corresponding to U+3002. In contrast, other languages (e.g., English, Korean) use the period ., corresponding to U+002E. Although these two characters can be entered using the same key on the keyboard, they are completely different characters, aren’t they?
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
mcl
Posts: 361
Joined: 04 May 2018, 16:35

Re: COM change Language ID in Word

19 May 2024, 02:11

Code: Select all

wdWidthHalfWidth := 6
oWord.Selection.Range.CharacterWidth := wdWidthHalfWidth
github://oGDIp - GDI+ wrapper for AHK v1.1
robinson
Posts: 217
Joined: 12 Sep 2019, 20:28

Re: COM change Language ID in Word

19 May 2024, 05:46

@mcl
That's an improvement!!!
But, still, it won't work if there is a single Chinese character in the selection,
unlike simply selecting “Arial” from Word's drop-down menu.
Looks like you're just one step away now.
Don't give up!

-EDIT-
Actually no, the problem is,
it won't affect any punctuation that's both selected, and touching any Chinese character.
robinson
Posts: 217
Joined: 12 Sep 2019, 20:28

Re: COM change Language ID in Word

19 May 2024, 05:51

@Seven0528
From what I can tell,
all Chinese punctuation are unique characters with their own codes,
except for quotation marks and forward/backward slashes,
which are just font choices.
Well, font choices plus some other mysterious thing we're trying to figure out,
which we've now discovered includes a character width value.
mcl
Posts: 361
Joined: 04 May 2018, 16:35

Re: COM change Language ID in Word

19 May 2024, 18:13

robinson wrote:
19 May 2024, 05:46
it won't affect any punctuation that's both selected, and touching any Chinese character.
I'm using the following code and see no issues. Or more likely, misunderstand what issues to look for — if so, can you provide sample text?

Code: Select all

F11::{
	oWord := ComObjActive("Word.Application")
	oWord.Selection.Font.Name := "Arial"
	oWord.Selection.Range.CharacterWidth := 6  ; wdWidthHalfWidth
	Return
}
chinese_quotes.png
chinese_quotes.png (10.5 KiB) Viewed 225 times
P.S. You can also try Selection.Text = Selection.Text if you don't mind to clear all formatting.
github://oGDIp - GDI+ wrapper for AHK v1.1
robinson
Posts: 217
Joined: 12 Sep 2019, 20:28

Re: COM change Language ID in Word

19 May 2024, 20:18

@mcl I'm assuming from what you can see in this .gif
that it's not changing those two quotation marks because they're beside a Chinese character.
(same code as yours, and again, changes all punctuation using drop-down menu)
Image

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Google [Bot], mikeyww and 40 guests