Select and Highlight a Sentence in Word

I created the below macro to first select a sentence in Word (for Windows running in Parallels) and second to highlight that sentence (in yellow).

I note that the corresponding keystrokes are CTRL + LEFT MOUSE CLICK and CTRL + ALT + H respectively.

In testing the macro:

i) CTRL + LEFT MOUSE CLICK does not work in that it visually highlights the sentence then "release" the highlight. No amount of pause helps.

ii) CTRL + ALT + H works perfectly.

I would appreciate help in getting the CTRL + LEFT MOUSE CLICK to work as this would be a huge time saver!

Thank you!!

Higlight Text.kmmacros (4.8 KB)

You don't want to use VBA in the Windows MS Word to assign a keystroke to something (I think) like this:

Sub MarkCurrentSentence()
    
    Selection.Expand Unit:=wdSentence
    
    Selection.Range.HighlightColorIndex = wdYellow
    
End Sub

?

(Might be snappier, if you can get it to work)

Appreciate the response.

I never thought of that but will use that as my fall back.

Would like to get this working in Keyboard Maestro and see no reason why it cannot / should not.

Thanks.

It can be difficult to reliably send simulated modifier keys to a VM, for one. You'll even have trouble sending uppercase letters via an "Insert text by typing" Action.

Agreed, I have noticed that.

In point of fact CNTRL + ALT do not work reliably as hot keys in Windows through Parallels.

The weird thing is that the CNTRL + ALT + H works flawlessly. Perhaps the muse click is the problem?

Will work on it over the next few days to see whether I can get this working in KM knowing that there is always the VBA fallback.

There may be an argument for seeing the native VBA approach as the ideal (reliable, performant), and GUI scripting over a Virtual Machine interface as a fall-back (tricky, slow).

If you want a version which toggles highlighting of the sentence, you could use something like this:

Sub ToggleHighlightCurrentSentence()

    With Selection.Range
        .Expand Unit:=wdSentence
        
        If .HighlightColorIndex = wdYellow Then
            .HighlightColorIndex = wdNoHighlight
        Else
            .HighlightColorIndex = wdYellow
        End If
    End With
    
End Sub

Appreciated and I hear you.

I like the idea of consolidating everything in one place (i.e., Keyboard Maestro) as it simplifies keep tracking of automations, moving to different / new computers, etc. but I do understand i) constant and reliable is critical ii) native solutions are preferred over VM GUI solutions and iii) something is better than nothing.

One follow up: I understand that I should add the VBA code to the normal.dot template (and other templates that I use) so that it is available in all new documents but will it be picked up by existing documents that use that template?

Thanks.

:+1: macros in normal.dot are globally available.

VBA / MS Office forums should be good for more specifics.

Awesome and thank you.

Will give it a go!

1 Like

Make sure that Parallels custom shortcuts are not interfering. IIRC, there are some customizable settings that change CTRL ←→ CMD and also make CTRL + LEFT-Click = RIGHT-Click. It is particularly frustrating in Office, so I also disabled every Parallel shortcut and implemented my own "fixes" in KM.

I wish it were that but it isn't because the CTR:L + LEFT CLICK works as it should (i.e., selecting the entire sentence) other than when trying to get keyboard Maestro to do so. It is very annoying!!

PS if you prefer expressions, where possible, to statements,
then you can also write it like this:

Sub ToggleHighlightCurrentSentence()
    With Selection.Range
        .Expand Unit:=wdSentence

        .HighlightColorIndex = _
            IIf(.HighlightColorIndex = wdYellow, wdNoHighlight, wdYellow)
    End With
End Sub

Given the problems with passing meta keys to Parallels, have you tried the simplest solution to selecting a sentence?

Appreciated, it worked great.

I made some changes so that i) it uses whatever highlight colour is selected and ii) the cursor move to the next line.

Sub ToggleHighlightAndMoveDownFixed()
    With Selection
        ' 1. Expand to the current sentence
        .Expand Unit:=wdSentence
        
        ' 2. Toggle the highlight
        If .Range.HighlightColorIndex <> wdNoHighlight Then
            .Range.HighlightColorIndex = wdNoHighlight
        Else
            .Range.HighlightColorIndex = Options.DefaultHighlightColorIndex
        End If
        
        ' 3. Move cursor to the very BEGINNING of the sentence we just highlighted
        .Collapse Direction:=wdCollapseStart
        
        ' 4. Now move down exactly 1 line from that starting point
        .MoveDown Unit:=wdLine, Count:=1
        
        ' 5. Ensure we are at the start of that new line
        .HomeKey Unit:=wdLine
    End With
End Sub

Great idea, thank you.

PS. Still wish I could get this to work in KM!! :frowning: :frowning:

1 Like

I successfully got to "select sentence" (Ctrl+Click) and "highlight" it in Parallels 19 using Keyboard Maestro:
highlight

I made sure to disable the "Mouse Shortcut" → "Secondary Click"

And finally, because Ctr+Alt+H wasn't working, I changed the Microsoft Word "Highlight" shortcut to Ctrl+Shift+H:

image

I appreciate the tenacity, truly!

Agreed but the solution you describe above is triggering a Word macro from Keyboard Maestro which is different that having Keyboard Maestro perform / simulate the Word keystrokes.

I don't have any Word macro. My solution simulate the sentence selection and triggers the modified shortcut for the built-in Highlight (no VBA required).

Ahhh, I missed that, sorry.

One follow up, how did you get the CNTRL + CLICK which selects the sentence work as that is the first step in the chain (i.e., highlight follows that noting I was always able to get teh highlighting to work, it was the sentence selection which I could not get to work)?

But you can't, and that's the problem.

KM sends events through macOS public APIs. Proper macOS apps receive, and act on, those events.

Word for Windows doesn't give a flying fig about macOS events...

You're actually sending them to Parallels, with added indirection because you're using Coherence mode, and hoping that Parallels accurately translates them for Windows actions. But some of the tricks Parallels uses appear to be applied at an earlier level in the macOS event hierarchy -- KM's simulated meta keys seem to be later in the hierarchy than Parallels looks for them, so are often missed.

Frankly, it's a miracle it works at all -- and what works for @JuanWayri may not work for you.

Some troubleshooting ideas:

  1. Make sure that when you manually Ctrl+Click it DOES select the sentence in Word.
  2. If you trigger your KM Macro with a hotkey, ADD A PAUSE BEFORE your simulated Ctrl+Click.