How to format a paragraph of MS WORD document Topic is solved
How to format a paragraph of MS WORD document
How to format a paragraph of MS WORD document, I want to format the last paragraph use the previous paragraph's font type and size.
- Attachments
-
- font.rar
- (12.23 KiB) Downloaded 27 times
-
- formatfont.png (57.64 KiB) Viewed 324 times
Re: How to format a paragraph of MS WORD document
Why not just use Word's "Format Painter?"
ste(phen|ve) kunkel
- flyingDman
- Posts: 2840
- Joined: 29 Sep 2013, 19:01
Re: How to format a paragraph of MS WORD document
Try:
Applies the font size and name of the previous paragraph to the current paragraph. (Note the sample text you provided included fonts that are not present on my system. If that's the case the script won't work)
Code: Select all
oWord := ComObjActive("Word.Application")
f12::
{
fname := oWord.Selection.Previous(4, 1).font.Name ; see https://learn.microsoft.com/en-us/office/vba/api/word.selection.previous
fsize := oWord.Selection.Previous(4, 1).font.size ; and https://learn.microsoft.com/en-us/office/vba/api/word.wdunits
oWord.selection.expand(4)
oword.selection.font.name := fname
oword.selection.font.size := fsize
}
14.3 & 1.3.7
Re: How to format a paragraph of MS WORD document
Can you elaborate more on that, or provide some links, I searched the forum and found nothing about "Format Painter" PS I can't the image you post.
Last edited by songdg on 16 Mar 2024, 22:23, edited 1 time in total.
Re: How to format a paragraph of MS WORD document
Thanks, if there're some blank paragraphs in between it won't work and got this error.flyingDman wrote: ↑16 Mar 2024, 15:02Try:Applies the font size and name of the previous paragraph to the current paragraph. (Note the sample text you provided included fonts that are not present on my system. If that's the case the script won't work)Code: Select all
oWord := ComObjActive("Word.Application") f12:: { fname := oWord.Selection.Previous(4, 1).font.Name ; see https://learn.microsoft.com/en-us/office/vba/api/word.selection.previous fsize := oWord.Selection.Previous(4, 1).font.size ; and https://learn.microsoft.com/en-us/office/vba/api/word.wdunits oWord.selection.expand(4) oword.selection.font.name := fname oword.selection.font.size := fsize }
Error: This value of type "String" has no property named "font".
003: oWord := ComObjActive("Word.Application")
006: {
▶ 007: fname := oWord.Selection.Previous(4, 1).font.Name
008: fsize := oWord.Selection.Previous(4, 1).font.size
009: oWord.selection.expand(4)
Re: How to format a paragraph of MS WORD document
My reply wasn't ahk-related or forum-related. The screenshot is an image of the MS Word editor. On the Home Ribbon, in the Clipboard Group, is a Paint Brush icon. It is for the "Format Painter." Word's built-in Format Painter is designed to do the thing that you want. It copies the formatting from one paragraph to another.
ste(phen|ve) kunkel
- flyingDman
- Posts: 2840
- Joined: 29 Sep 2013, 19:01
Re: How to format a paragraph of MS WORD document
These rogue blank paragraphs should probably be deleted for the script to work. "Previous" refers to the prior paragraph whether blank or not and there is no easy way to have it refer to "prior if not blank". This is a script to delete "blank" paragraphs in a document:if there're some blank paragraphs in between it won't work and got this error.
Code: Select all
oWord := ComObjActive("Word.Application")
oWord.selection.wholestory
loop oWord.ActiveDocument.Paragraphs.count
(strlen(oWord.selection.paragraphs.item(a_index).range.text) = 1) && lst .= a_index ","
if lst ?? ""
for x,y in strsplit(sort(trim(lst,","),"NRD,"),",")
oWord.selection.paragraphs.item(y).range.delete()
Code: Select all
oWord := ComObjActive("Word.Application")
oWord.selection.wholestory
loop
rslt := oWord.Selection.Find.Execute("^p^p",,,,,,1,1,1,"^p",2)
until rslt = 0
return
14.3 & 1.3.7
Re: How to format a paragraph of MS WORD document
Thanks anywaykunkel321 wrote: ↑17 Mar 2024, 09:49My reply wasn't ahk-related or forum-related. The screenshot is an image of the MS Word editor. On the Home Ribbon, in the Clipboard Group, is a Paint Brush icon. It is for the "Format Painter." Word's built-in Format Painter is designed to do the thing that you want. It copies the formatting from one paragraph to another.
Re: How to format a paragraph of MS WORD document
Got it. Thanks for the help and detailed explanation, much appreciatedflyingDman wrote: ↑17 Mar 2024, 14:00These rogue blank paragraphs should probably be deleted for the script to work. "Previous" refers to the prior paragraph whether blank or not and there is no easy way to have it refer to "prior if not blank". This is a script to delete "blank" paragraphs in a document:if there're some blank paragraphs in between it won't work and got this error.orCode: Select all
oWord := ComObjActive("Word.Application") oWord.selection.wholestory loop oWord.ActiveDocument.Paragraphs.count (strlen(oWord.selection.paragraphs.item(a_index).range.text) = 1) && lst .= a_index "," if lst ?? "" for x,y in strsplit(sort(trim(lst,","),"NRD,"),",") oWord.selection.paragraphs.item(y).range.delete()
Code: Select all
oWord := ComObjActive("Word.Application") oWord.selection.wholestory loop rslt := oWord.Selection.Find.Execute("^p^p",,,,,,1,1,1,"^p",2) until rslt = 0 return
- FanaticGuru
- Posts: 1908
- Joined: 30 Sep 2013, 22:25
Re: How to format a paragraph of MS WORD document Topic is solved
If you want to copy all the formatting from the paragraph above instead of just the font and size, you can do this:
Code: Select all
oWord := ComObjActive("Word.Application")
f11::
{
oWord.Selection.Previous(4, 1).Select()
oWord.Selection.CopyFormat
oWord.Selection.Next(4, 1).Select()
oWord.Selection.PasteFormat
}
You can copy even the formatting from a blank paragraph with this method.
I just noticed you said "last" paragraph and not "current". But either way the technique is similar.
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: How to format a paragraph of MS WORD document
Thank you so much, you make my dayFanaticGuru wrote: ↑18 Mar 2024, 20:02
If you want to copy all the formatting from the paragraph above instead of just the font and size, you can do this:Code: Select all
oWord := ComObjActive("Word.Application") f11:: { oWord.Selection.Previous(4, 1).Select() oWord.Selection.CopyFormat oWord.Selection.Next(4, 1).Select() oWord.Selection.PasteFormat }
You can copy even the formatting from a blank paragraph with this method.
I just noticed you said "last" paragraph and not "current". But either way the technique is similar.
FG
Who is online
Users browsing this forum: Descolada and 82 guests