Auto bold certain words of pasted text in word processor

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
efaustus9
Posts: 11
Joined: 27 Nov 2021, 23:07

Auto bold certain words of pasted text in word processor

Post by efaustus9 » 28 Nov 2021, 00:04

Hi all,

I am new to this and I am not even sure what I am looking to do falls within the capabilities of AHK.I am using a version of Dragon Natural Speaking that no longer supports pasting rich text in precanned dictations commands. Now when I issue a dragon command to paste any of my many dictations it will only do so in plain text. I was wondering if it would be possible to have AHK to automatically bold certain words in a pasted text (e.g. "Part 1.", "Part 2","summary"). I am not using MS word but in the word processor I am using ctrl+B still does bold text. Any input would be greatly appreciated, thanks!

prestonage
Posts: 35
Joined: 17 Nov 2021, 10:53

Re: Auto bold certain words of pasted text in word processor

Post by prestonage » 28 Nov 2021, 01:20

Use hotstrings, like this. Just make more for the other strings you desire to auto-bold.

Code: Select all

::Part 1::

Send, Part 1							; Send text

Sleep, 10								; sleep to make sure commands register

Send, +{Left 6}							; highlight full word (6 characters)

Sleep, 10

Send, ^b								; Bold the text

Sleep, 10

Send, {Right 6}							; return cursor to original spot

Sleep, 10

Send, ^b								; turn off bold

return

efaustus9
Posts: 11
Joined: 27 Nov 2021, 23:07

Re: Auto bold certain words of pasted text in word processor

Post by efaustus9 » 28 Nov 2021, 11:59

prestonage wrote:
28 Nov 2021, 01:20
Use hotstrings, like this. Just make more for the other strings you desire to auto-bold.
Thank you so much prestonage! I tried out your code and it works great if I manually type out "Part 1" however if Part 1 is pasted from a canned dictation the script isn't activated and thus stays in plain text. I am aware AHK can interpret and can be activated by certain keystrokes not sure if AHK with a key combo can scan words in a active word processor window and apply formatting to these designated words.


prestonage
Posts: 35
Joined: 17 Nov 2021, 10:53

Re: Auto bold certain words of pasted text in word processor

Post by prestonage » 30 Nov 2021, 12:35

efaustus9 wrote:
29 Nov 2021, 00:58
This seems close to what I am looking to do but it looks like it will only work on MS word.

https://www.autohotkey.com/board/topic/91284-help-with-script-to-search-for-certain-words-in-a-word-doc-then-center-and-bold/
In that case you are way overthinking this. This is a common feature in most word processors. I use libreoffice, and you can do it there.

https://ask.libreoffice.org/t/search-and-replace-to-apply-a-font-attribute/37106

Lepes
Posts: 141
Joined: 06 May 2021, 07:32
Location: Spain

Re: Auto bold certain words of pasted text in word processor

Post by Lepes » 30 Nov 2021, 14:42

In fact, I think it could be simplified as:

Code: Select all

::Part 1:: Send, ^bPart 1^b  
 
I don't know any word processor that doesn't work these code (changing bold shorcut).

I tested and it didn't work pasting test. It only work when I type manually.

efaustus9
Posts: 11
Joined: 27 Nov 2021, 23:07

Re: Auto bold certain words of pasted text in word processor

Post by efaustus9 » 30 Nov 2021, 18:28

prestonage wrote:
30 Nov 2021, 12:35

In that case you are way overthinking this. This is a common feature in most word processors. I use libreoffice, and you can do it there.

https://ask.libreoffice.org/t/search-and-replace-to-apply-a-font-attribute/37106
Thanks again unfortunately I have to use a proprietary word processor that's part of a specialized suite and is rather limited. I do have MS word on my machine maybe I could use it as an intermediary. For example possibly a designated ahk sensitive keystroke to open ms word, have it wait 10s for me to deploy canned dictation, next it runs the code I referenced above, than it copies finished formatted text, switches active window and finally pastes output.

prestonage
Posts: 35
Joined: 17 Nov 2021, 10:53

Re: Auto bold certain words of pasted text in word processor

Post by prestonage » 01 Dec 2021, 01:54

efaustus9 wrote:
30 Nov 2021, 18:28
prestonage wrote:
30 Nov 2021, 12:35

In that case you are way overthinking this. This is a common feature in most word processors. I use libreoffice, and you can do it there.

https://ask.libreoffice.org/t/search-and-replace-to-apply-a-font-attribute/37106
Thanks again unfortunately I have to use a proprietary word processor that's part of a specialized suite and is rather limited. I do have MS word on my machine maybe I could use it as an intermediary. For example possibly a designated ahk sensitive keystroke to open ms word, have it wait 10s for me to deploy canned dictation, next it runs the code I referenced above, than it copies finished formatted text, switches active window and finally pastes output.
I still think you are overthinking the issue. If you have to use this poor-quality word processor for whatever reason, you should be able to just do the adjustments in ms word, and then copy/paste with formatting.

efaustus9
Posts: 11
Joined: 27 Nov 2021, 23:07

Re: Auto bold certain words of pasted text in word processor

Post by efaustus9 » 01 Dec 2021, 02:09

I was able to frankenstein this together from various code I found and it works :D

Code: Select all

#Persistent
^F:: ; ctrl+F initiates script

WinWait ahk_exe WINWORD.EXE
WinActivate ahk_exe WINWORD.EXE
; WinWaitActive ahk_exe WINWORD.EXE
sleep, 5000 ; waits for canned dictation 

try ; gards against runtime errors if ComObjActive() failed
oWord := ComObjActive("Word.Application")
catch ; if ComObjActive() failed...
return ; ... stop further execution
 
oDoc := oWord.documents[1]
 
oDoc.Select ; makes the Selection object to be the whole document
 
if (oWord.Selection.Find.Execute("Part .1"))
oWord.Selection.Font.Bold := true
 
oDoc.Select
 
if (oWord.Selection.Find.Execute("Part .2"))
oWord.Selection.Font.Bold := true
 
oDoc.Select
 
if (oWord.Selection.Find.Execute("Part .3"))
oWord.Selection.Font.Bold := true

 
oDoc.Select
 
if (oWord.Selection.Find.Execute("Part .4"))
oWord.Selection.Font.Bold := true

oDoc.Range.FormattedText.Cut ;cuts all formatted text 

WinWait ahk_exe notepad.exe
WinActivate ahk_exe notepad.exe
; WinWaitActive ahk_exe notepad.exe
Send ^V ;pastes all formated text
I used notepad.exe as the output for testing purposes as I do not have the proprietary company word processing software on my home computer, but you can paste the clipboard back into the word document to confirm the desired formatting.

Jeremy123
Posts: 1
Joined: 09 Aug 2022, 17:08

Re: Auto bold certain words of pasted text in word processor

Post by Jeremy123 » 20 Sep 2022, 04:48

Does this code actually work? more help

efaustus9
Posts: 11
Joined: 27 Nov 2021, 23:07

Re: Auto bold certain words of pasted text in word processor

Post by efaustus9 » 09 Dec 2022, 02:06

Jeremy123 wrote:
20 Sep 2022, 04:48
Does this code actually work? more help
Yes, does it not for you?

Post Reply

Return to “Ask for Help (v1)”