How to format a paragraph of MS WORD document Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
songdg
Posts: 605
Joined: 04 Oct 2017, 20:04

How to format a paragraph of MS WORD document

16 Mar 2024, 03:09

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
formatfont.png (57.64 KiB) Viewed 305 times
User avatar
kunkel321
Posts: 1122
Joined: 30 Nov 2015, 21:19

Re: How to format a paragraph of MS WORD document

16 Mar 2024, 13:23

Why not just use Word's "Format Painter?"
Image
ste(phen|ve) kunkel
User avatar
flyingDman
Posts: 2828
Joined: 29 Sep 2013, 19:01

Re: How to format a paragraph of MS WORD document

16 Mar 2024, 15:02

Try:

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
	}
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)
14.3 & 1.3.7
songdg
Posts: 605
Joined: 04 Oct 2017, 20:04

Re: How to format a paragraph of MS WORD document

16 Mar 2024, 22:00

kunkel321 wrote:
16 Mar 2024, 13:23
Why not just use Word's "Format Painter?"
Image
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.
songdg
Posts: 605
Joined: 04 Oct 2017, 20:04

Re: How to format a paragraph of MS WORD document

16 Mar 2024, 22:21

flyingDman wrote:
16 Mar 2024, 15:02
Try:

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
	}
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)
Thanks, if there're some blank paragraphs in between it won't work and got this error.
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)
User avatar
kunkel321
Posts: 1122
Joined: 30 Nov 2015, 21:19

Re: How to format a paragraph of MS WORD document

17 Mar 2024, 09:49

songdg wrote:
16 Mar 2024, 22:00
kunkel321 wrote:
16 Mar 2024, 13:23
Why not just use Word's "Format Painter?"
Image
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.
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
User avatar
flyingDman
Posts: 2828
Joined: 29 Sep 2013, 19:01

Re: How to format a paragraph of MS WORD document

17 Mar 2024, 14:00

if there're some blank paragraphs in between it won't work and got this error.
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:

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()
or

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
songdg
Posts: 605
Joined: 04 Oct 2017, 20:04

Re: How to format a paragraph of MS WORD document

17 Mar 2024, 21:54

kunkel321 wrote:
17 Mar 2024, 09:49
songdg wrote:
16 Mar 2024, 22:00
kunkel321 wrote:
16 Mar 2024, 13:23
Why not just use Word's "Format Painter?"
Image
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.
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.
Thanks anyway :D
songdg
Posts: 605
Joined: 04 Oct 2017, 20:04

Re: How to format a paragraph of MS WORD document

17 Mar 2024, 21:58

flyingDman wrote:
17 Mar 2024, 14:00
if there're some blank paragraphs in between it won't work and got this error.
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:

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()
or

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
Got it. Thanks for the help and detailed explanation, much appreciated :bravo:
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: How to format a paragraph of MS WORD document  Topic is solved

18 Mar 2024, 20:02

songdg wrote:
16 Mar 2024, 03:09
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.

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
songdg
Posts: 605
Joined: 04 Oct 2017, 20:04

Re: How to format a paragraph of MS WORD document

18 Mar 2024, 20:53

FanaticGuru wrote:
18 Mar 2024, 20:02
songdg wrote:
16 Mar 2024, 03:09
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.

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
Thank you so much, you make my day :thumbup:

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 25 guests