COM help

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

COM help

30 Nov 2023, 13:21

Hi, noob here.
Could someone help me write a COM script
to delete all single struckthrough characters?
I'm using Office Word 2007 if that makes a difference.
Oh, also, it needs to only be of the characters selected,
because for some reason Word doesn't have the “only in selection” feature
(at least as far as I can see).
Thanks!
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: COM help

30 Nov 2023, 16:37

robinson wrote:
30 Nov 2023, 13:21
Hi, noob here.
Could someone help me write a COM script
to delete all single struckthrough characters?
I'm using Office Word 2007 if that makes a difference.
Oh, also, it needs to only be of the characters selected,
because for some reason Word doesn't have the “only in selection” feature
(at least as far as I can see).
Thanks!

Try this:

Code: Select all

wdApp := ComObjActive("Word.Application")

wdRng := wdApp.Selection.Range
wdRng.Find.ClearFormatting
wdRng.Find.Font.StrikeThrough := True
wdRng.Find.Replacement.ClearFormatting

wdRng.Find.Execute(,,,,,,0,0,,,2)	; Selection Only (Forward false, wdFindStop, wdReplaceAll)
; wdRng.Find.Execute(,,,,,,,1,,,2)	; Whole Document (wdFindContinue, wdReplaceAll)

If you have nothing selected then it will do the whole document. Could put a check in there to do nothing with no selection if that is preferred.

Commented out how to just do the whole document for others that might prefer that.

This Find/Replace technique based on format can be used for lots of purposes like change one font to another, red text to strikeout, one text size to bold, etc. Pretty much look for any formatting and change it to any other formatting. There is a lot that can be done with Find/Replace.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
robinson
Posts: 219
Joined: 12 Sep 2019, 20:28

Re: COM help

30 Nov 2023, 17:41

@FanaticGuru
That's brilliant, thanks.
I just realised now that it leaves a lot of double spaces,
that were previously to the left and right of the now-deleted segments.
Could you help me out one more time and add an extra line
to replace all double spaces with single ones?
(Actually first replace all triple spaces with double ones.)
Also, where do you recommend a beginner learn about COM?
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: COM help

01 Dec 2023, 14:12

robinson wrote:
30 Nov 2023, 17:41
I just realised now that it leaves a lot of double spaces,
that were previously to the left and right of the now-deleted segments.
Could you help me out one more time and add an extra line
to replace all double spaces with single ones?
(Actually first replace all triple spaces with double ones.)

You just need to do another find/replace for multiple spaces afterwards:

Code: Select all

wdApp := ComObjActive("Word.Application")

wdRng := wdApp.Selection.Range
wdRng.Find.ClearFormatting
wdRng.Find.Font.StrikeThrough := True
wdRng.Find.Replacement.ClearFormatting
wdRng.Find.Execute(,,,,,,0,0,,,2)	; Selection Only (Forward false, wdFindStop, wdReplaceAll)

wdRng := wdApp.Selection.Range
wdRng.Find.ClearFormatting
wdRng.Find.Text := '([^s ])@[^s ]'	; 1 or more hard or soft space before a hard or soft space
wdRng.Find.Replacement.ClearFormatting
wdRng.Find.Replacement.Text := '\1'	; replace with capture of first hard or soft space
wdRng.Find.Execute(,,,1,,,,0,,,2)	; Selection Only (Wildcards, wdFindStop, wdReplaceAll)

As for COM for beginners:
robinson wrote:
30 Nov 2023, 17:41
Also, where do you recommend a beginner learn about COM?

There are many tutorials on the forum. Most are for v1 but v2 is very similar in regards to COM.

@Joe Glines also has a good collection of information and videos on his own site at the-Automator.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
robinson
Posts: 219
Joined: 12 Sep 2019, 20:28

Re: COM help

02 Dec 2023, 22:23

@FanaticGuru
One more thing if you don't mind.
You see that line:

Code: Select all

wdRng.Find.Text := '([^s ])@[^s ]'
What kind of RegEx system is that exactly?
Thanks.
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: COM help

04 Dec 2023, 17:06

robinson wrote:
02 Dec 2023, 22:23
You see that line:

Code: Select all

wdRng.Find.Text := '([^s ])@[^s ]'
What kind of RegEx system is that exactly?

That is an example of the wildcard system used by Word. Not just for VBA or COM, if you manually do a 'Find' in Word, that is the wildcard system used.

A similar wildcard system is used in most MS Office products.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 99 guests