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!
COM help
- FanaticGuru
- Posts: 1908
- Joined: 30 Sep 2013, 22:25
Re: COM help
robinson wrote: ↑30 Nov 2023, 13:21Hi, 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
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
Re: COM help
@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?
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?
- FanaticGuru
- Posts: 1908
- Joined: 30 Sep 2013, 22:25
Re: COM help
robinson wrote: ↑30 Nov 2023, 17:41I 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:
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
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
Re: COM help
@FanaticGuru
One more thing if you don't mind.
You see that line:
What kind of RegEx system is that exactly?
Thanks.
One more thing if you don't mind.
You see that line:
Code: Select all
wdRng.Find.Text := '([^s ])@[^s ]'
Thanks.
- FanaticGuru
- Posts: 1908
- Joined: 30 Sep 2013, 22:25
Re: COM help
robinson wrote: ↑02 Dec 2023, 22:23You see that line:What kind of RegEx system is that exactly?Code: Select all
wdRng.Find.Text := '([^s ])@[^s ]'
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
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
Who is online
Users browsing this forum: No registered users and 102 guests