Select a word in MSWord Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
afshindavoudy
Posts: 44
Joined: 10 Jan 2024, 13:25

Select a word in MSWord

Post by afshindavoudy » 25 Jan 2024, 11:15

How can I select the word under the cursor in MS Word?
The mouse pointer may be at another position, so I can't use a double click.
The cursor also may be at the beginning, middle, or end of the word.

Appreciate any help
Last edited by afshindavoudy on 25 Jan 2024, 13:16, edited 1 time in total.

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Select a work in MSWord

Post by flyingDman » 25 Jan 2024, 11:30

Try:

Code: Select all

f12::
{
oWord := ComObjActive("word.application")
oWord.selection.expand(2)
}
but the cursor cannot be at the end of the word if that word is the last word before a line break. To catch that you can use:

Code: Select all

f12::
{
oWord := ComObjActive("word.application")
oWord.selection.expand(2)
if (oWord.selection.text ~= "\R")
	{
	send '{left 2}'
	oWord.selection.expand(2)
	}
}
14.3 & 1.3.7

afshindavoudy
Posts: 44
Joined: 10 Jan 2024, 13:25

Re: Select a work in MSWord

Post by afshindavoudy » 25 Jan 2024, 13:30

flyingDman wrote:
25 Jan 2024, 11:30
Try:

Code: Select all

f12::
{
oWord := ComObjActive("word.application")
oWord.selection.expand(2)
}
but the cursor cannot be at the end of the word if that word is the last word before a line break. To catch that you can use:

Code: Select all

f12::
{
oWord := ComObjActive("word.application")
oWord.selection.expand(2)
if (oWord.selection.text ~= "\R")
	{
	send '{left 2}'
	oWord.selection.expand(2)
	}
}
Thank you for the script
How can I exclude the white space from selection? For example in this sentence:
"This is a word"
If the cursor is in the middle of "This" the selection would be like "This " (with an extra space)

btw: It works when the cursor is at the end of a word, except for the end of the sentence!

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Select a word in MSWord

Post by flyingDman » 25 Jan 2024, 13:56

Perhaps:

Code: Select all

f12::
	{
	oWord := ComObjActive("word.application")
	oWord.selection.expand(2)
	if (oWord.selection.text ~= "\R")
		{
		send '{left 2}'
		oWord.selection.expand(2)
		}
	while (oWord.selection.text ~= "\s")
		send '+{left}'
	}
14.3 & 1.3.7

afshindavoudy
Posts: 44
Joined: 10 Jan 2024, 13:25

Re: Select a word in MSWord

Post by afshindavoudy » 25 Jan 2024, 23:41

flyingDman wrote:
25 Jan 2024, 13:56
Perhaps:

Code: Select all

f12::
	{
	oWord := ComObjActive("word.application")
	oWord.selection.expand(2)
	if (oWord.selection.text ~= "\R")
		{
		send '{left 2}'
		oWord.selection.expand(2)
		}
	while (oWord.selection.text ~= "\s")
		send '+{left}'
	}
Amazing work. Thanks!

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Select a word in MSWord

Post by flyingDman » 26 Jan 2024, 00:25

Bit faster, bit smoother:

Code: Select all

f12::
	{
	oWord := ComObjActive("word.application")
	oWord.ScreenUpdating := False                                 ; <<<<<
	oWord.selection.expand(2)
	if (oWord.selection.text ~= "\R")
		{
		send '{left 2}'
		oWord.selection.expand(2)
		}
	while (oWord.selection.text ~= "\s")
		send '+{left}'
	oWord.ScreenUpdating := True                                  ; <<<<<
	}

14.3 & 1.3.7

afshindavoudy
Posts: 44
Joined: 10 Jan 2024, 13:25

Re: Select a word in MSWord

Post by afshindavoudy » 27 Jan 2024, 02:23

flyingDman wrote:
26 Jan 2024, 00:25
Bit faster, bit smoother:

Code: Select all

f12::
	{
	oWord := ComObjActive("word.application")
	oWord.ScreenUpdating := False                                 ; <<<<<
	oWord.selection.expand(2)
	if (oWord.selection.text ~= "\R")
		{
		send '{left 2}'
		oWord.selection.expand(2)
		}
	while (oWord.selection.text ~= "\s")
		send '+{left}'
	oWord.ScreenUpdating := True                                  ; <<<<<
	}

Great! Thanks!
How can I make it work in other applications aswell?

Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Select a word in MSWord

Post by Descolada » 27 Jan 2024, 13:28

Perhaps something like this for other applications (requires UIA.ahk in the same folder as the script):

Code: Select all

#Requires AutoHotkey v2
#include UIA.ahk

F12::{
    try if (el := UIA.GetFocusedElement()) && el.IsTextPatternAvailable {
        selectionRange := el.GetSelection()[1]
        selectionRange.ExpandToEnclosingUnit(UIA.TextUnit.Word)
        text := selectionRange.GetText()
        if text ~= "\s$"
            selectionRange.MoveEndpointByUnit(UIA.TextPatternRangeEndpoint.End, UIA.TextUnit.Character, -1)
        if text ~= "^\s?$"
            selectionRange.MoveEndpointByUnit(UIA.TextPatternRangeEndpoint.Start, UIA.TextUnit.Word, -1)
        selectionRange.Select()
        return
    }
    ClipSave := ClipboardAll()
    A_Clipboard := ""
    Send "{Ctrl down}{Right}{Left}{Shift down}{Right}{Shift up}c{Ctrl up}"
    ClipWait(0.5)
    If SubStr(A_Clipboard, -1, 1) = " "
        Send "{Shift down}{Left}{Shift up}"
    else if A_Clipboard = ""
        Send "{Ctrl down}{Shift down}{Left}{Shift up}{Ctrl up}"
    A_Clipboard := ClipSave
}

afshindavoudy
Posts: 44
Joined: 10 Jan 2024, 13:25

Re: Select a word in MSWord

Post by afshindavoudy » 28 Jan 2024, 02:41

Descolada wrote:
27 Jan 2024, 13:28
Perhaps something like this for other applications (requires UIA.ahk in the same folder as the script):

Code: Select all

#Requires AutoHotkey v2
#include UIA.ahk

F12::{
    try if (el := UIA.GetFocusedElement()) && el.IsTextPatternAvailable {
        selectionRange := el.GetSelection()[1]
        selectionRange.ExpandToEnclosingUnit(UIA.TextUnit.Word)
        text := selectionRange.GetText()
        if text ~= "\s$"
            selectionRange.MoveEndpointByUnit(UIA.TextPatternRangeEndpoint.End, UIA.TextUnit.Character, -1)
        if text ~= "^\s?$"
            selectionRange.MoveEndpointByUnit(UIA.TextPatternRangeEndpoint.Start, UIA.TextUnit.Word, -1)
        selectionRange.Select()
        return
    }
    ClipSave := ClipboardAll()
    A_Clipboard := ""
    Send "{Ctrl down}{Right}{Left}{Shift down}{Right}{Shift up}c{Ctrl up}"
    ClipWait(0.5)
    If SubStr(A_Clipboard, -1, 1) = " "
        Send "{Shift down}{Left}{Shift up}"
    else if A_Clipboard = ""
        Send "{Ctrl down}{Shift down}{Left}{Shift up}{Ctrl up}"
    A_Clipboard := ClipSave
}
This one is really amazing!
It works in almost all apps and text-fields, even at the end of the line which selects the last word correctly.
In minor cases like VSCode, for some reason its not working!
Still great work!
Thank you so much

afshindavoudy
Posts: 44
Joined: 10 Jan 2024, 13:25

Re: Select a word in MSWord

Post by afshindavoudy » 28 Jan 2024, 03:04

@Descolada
Do you have any idea how to make it work in VSCode?

Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Select a word in MSWord  Topic is solved

Post by Descolada » 28 Jan 2024, 05:20

@afshindavoudy yeah for some reason the UIA method is causing weird behavior in VSCode, so I added an exception for it:

Code: Select all

#Requires AutoHotkey v2
#include UIA.ahk

F12::{
    static Exceptions := ["code.exe"]
    try if !HasValue(Exceptions, WinGetProcessName("A")) && (el := UIA.GetFocusedElement()) && el.IsTextPatternAvailable {
        selectionRange := el.GetSelection()[1]
        selectionRange.ExpandToEnclosingUnit(UIA.TextUnit.Word)
        text := selectionRange.GetText()
        if text ~= "\s$"
            selectionRange.MoveEndpointByUnit(UIA.TextPatternRangeEndpoint.End, UIA.TextUnit.Character, -1)
        if text ~= "^\s?$"
            selectionRange.MoveEndpointByUnit(UIA.TextPatternRangeEndpoint.Start, UIA.TextUnit.Word, -1)
        selectionRange.Select()
        return
    }
    ClipSave := ClipboardAll()
    A_Clipboard := ""
    Send "{Ctrl down}{Right}{Left}{Shift down}{Right}{Shift up}c{Ctrl up}"
    ClipWait(0.5)
    If SubStr(A_Clipboard, -1, 1) = " "
        Send "{Shift down}{Left}{Shift up}"
    else if A_Clipboard = ""
        Send "{Ctrl down}{Shift down}{Left}{Shift up}{Ctrl up}"
    A_Clipboard := ClipSave
}

HasValue(arr, target) {
	for index, value in arr
		if (value = target)
			return index
	return 0
}

afshindavoudy
Posts: 44
Joined: 10 Jan 2024, 13:25

Re: Select a word in MSWord

Post by afshindavoudy » 28 Jan 2024, 07:53

@Descolada
Works perfectly
Thanks again

Post Reply

Return to “Ask for Help (v2)”