Select a word in MSWord Topic is solved
-
- Posts: 44
- Joined: 10 Jan 2024, 13:25
Select a word in MSWord
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
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.
- flyingDman
- Posts: 2848
- Joined: 29 Sep 2013, 19:01
Re: Select a work in MSWord
Try:
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)
}
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
-
- Posts: 44
- Joined: 10 Jan 2024, 13:25
Re: Select a work in MSWord
Thank you for the scriptflyingDman wrote: ↑25 Jan 2024, 11:30Try: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) }
Code: Select all
f12:: { oWord := ComObjActive("word.application") oWord.selection.expand(2) if (oWord.selection.text ~= "\R") { send '{left 2}' oWord.selection.expand(2) } }
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!
- flyingDman
- Posts: 2848
- Joined: 29 Sep 2013, 19:01
Re: Select a word in MSWord
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
-
- Posts: 44
- Joined: 10 Jan 2024, 13:25
Re: Select a word in MSWord
Amazing work. Thanks!flyingDman wrote: ↑25 Jan 2024, 13:56Perhaps: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}' }
- flyingDman
- Posts: 2848
- Joined: 29 Sep 2013, 19:01
Re: Select a word in MSWord
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
-
- Posts: 44
- Joined: 10 Jan 2024, 13:25
Re: Select a word in MSWord
Great! Thanks!flyingDman wrote: ↑26 Jan 2024, 00:25Bit 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 ; <<<<< }
How can I make it work in other applications aswell?
Re: Select a word in MSWord
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
}
-
- Posts: 44
- Joined: 10 Jan 2024, 13:25
Re: Select a word in MSWord
This one is really amazing!Descolada wrote: ↑27 Jan 2024, 13:28Perhaps 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 }
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
-
- Posts: 44
- Joined: 10 Jan 2024, 13:25
Re: Select a word in MSWord
@Descolada
Do you have any idea how to make it work in VSCode?
Do you have any idea how to make it work in VSCode?
Re: Select a word in MSWord Topic is solved
@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
}
-
- Posts: 44
- Joined: 10 Jan 2024, 13:25
Re: Select a word in MSWord
@Descolada
Works perfectly
Thanks again
Works perfectly
Thanks again