![]() |
|
|
|
#1
|
|||
|
|||
|
Hi, i am a novice in macro / word vba.
I need help for the following task. I have a document consisting of many paragraphs. I need a macro to do the following: 1. Insert an additional paragraph mark/break after every three existing paragraphs until end of document. Grateful if somebody can help. Thank you very much. |
|
#2
|
||||
|
||||
|
It is bad practice to insert empty paragraphs. Word is not a typewriter, which is the origin of inserting space using empty paragraphs, so spacing should be applied with styles. You probably have existing styles with space that would so the job, but the following will create styles and add them as appropriate to give the required spacing. If you have suitable styles, remove the style creation and apply them instead.
Code:
Sub Macro1()
Dim i As Long
Dim oPara As Paragraph
'Create styles or use existing styles
ActiveDocument.Styles.Add Name:="Body_Spaced", Type:=wdStyleTypeParagraph
ActiveDocument.Styles("Body_Spaced").AutomaticallyUpdate = False
With ActiveDocument.Styles("Body_Spaced").Font
.Name = "Calibri"
.Size = 12
End With
With ActiveDocument.Styles("Body_Spaced").ParagraphFormat
.SpaceAfter = 12
End With
ActiveDocument.Styles.Add Name:="Body_Unspaced", Type:= _
wdStyleTypeParagraph
ActiveDocument.Styles("Body_Unspaced").AutomaticallyUpdate = False
With ActiveDocument.Styles("Body_Unspaced").Font
.Name = "Calibri"
.Size = 12
End With
With ActiveDocument.Styles("Body_Unspaced").ParagraphFormat
.SpaceAfter = 0
End With
'end of style creation
'apply styles
For i = 1 To ActiveDocument.Paragraphs.Count
Set oPara = ActiveDocument.Paragraphs(i)
If i Mod 3 = 0 Then
oPara.Range.Style = "Body_Spaced"
Else
oPara.Range.Style = "Body_Unspaced"
End If
Next i
Set oPara = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#3
|
|||
|
|||
|
@Graham Mayor
Thank you .. thank you... thank you very much ... it works very well in one document.. . In another document, I need to insert the sigh "+" after every three paragraphs. I try to modify the vba but don't know how. Grateful if u can help. Many thanks. |
|
#4
|
||||
|
||||
|
I assume that you mean you want a '+' sign in the space? That being the case you can adapt the previous macro e.g. as follows.
Code:
Sub Macro2()
'Graham Mayor - https://www.gmayor.com - Last updated - 20 Jan 2020
Dim i As Long
Dim oPara As Paragraph
Dim oRng As Range
'Create styles or use existing styles
On Error Resume Next
ActiveDocument.Styles.Add Name:="Body_Spaced", Type:=wdStyleTypeParagraph
ActiveDocument.Styles("Body_Spaced").AutomaticallyUpdate = False
With ActiveDocument.Styles("Body_Spaced").Font
.Name = "Calibri"
.Size = 12
End With
With ActiveDocument.Styles("Body_Spaced").ParagraphFormat
.SpaceAfter = 12
End With
ActiveDocument.Styles.Add Name:="Body_Unspaced", Type:= _
wdStyleTypeParagraph
ActiveDocument.Styles("Body_Unspaced").AutomaticallyUpdate = False
With ActiveDocument.Styles("Body_Unspaced").Font
.Name = "Calibri"
.Size = 12
End With
With ActiveDocument.Styles("Body_Unspaced").ParagraphFormat
.SpaceAfter = 0
End With
On Error GoTo 0
'end of style creation
'apply styles
For i = 1 To ActiveDocument.Paragraphs.Count
Set oPara = ActiveDocument.Paragraphs(i)
If i Mod 3 = 0 Then
oPara.Range.Style = "Body_Spaced"
Else
oPara.Range.Style = "Body_Unspaced"
End If
Next i
Set oRng = ActiveDocument.Range
With oRng.Find
.Style = "Body_Spaced"
Do While .Execute
If Len(oRng.Next.Paragraphs(1).Range) > 1 Then
oRng.InsertAfter "+" & vbCr
End If
oRng.Style = "Body_Unspaced"
oRng.Collapse 0
Loop
End With
Set oRng = Nothing
Set oPara = Nothing
End Sub
Code:
Sub Macro3()
'Graham Mayor - https://www.gmayor.com - Last updated - 20 Jan 2020
Dim oRng As Range
Dim lngPara As Long
Dim lngCount As Long
Const lngNum As Long = 3 'the number of paragraphs to group
lngCount = ActiveDocument.Paragraphs.Count
For lngPara = lngCount To 1 Step -1
'eliminate empty paragraphs at the end from the count
If Len(ActiveDocument.Paragraphs(lngPara).Range) = 1 Then
lngCount = lngCount - 1
Else
Exit For
End If
Next lngPara
'Find the end of the last of the grouped paragraphs
Do Until lngCount Mod lngNum = 0
lngCount = lngCount - 1
Loop
'Insert paragraphs containing a '+' character
For lngPara = lngCount To lngNum Step -lngNum
Set oRng = ActiveDocument.Paragraphs(lngPara).Range
oRng.InsertAfter "+" & vbCr
Next lngPara
Set oRng = Nothing
End Sub
Code:
oRng.InsertAfter "+" & vbCr
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com Last edited by gmayor; 01-20-2020 at 05:45 AM. |
|
#5
|
|||
|
|||
|
@Graham Mayor. Again many thanks for ur great help.
|
|
#6
|
|||
|
|||
|
thank you very much. my prblem is solves. many thanks.
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
How to insert paragraph character after every 500 characters?
|
aditya_bokade | Word VBA | 28 | 11-13-2021 10:48 PM |
Word macro to insert text at the beginning of paragraph but skip tables
|
ashalles135 | Word VBA | 5 | 09-26-2018 09:49 AM |
word macro To insert text at the beginning and at end of paragraph
|
ArieH | Word VBA | 20 | 09-10-2017 04:23 PM |
Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank
|
caboy | Word VBA | 2 | 04-01-2015 07:00 AM |
Insert paragraph break before images
|
jsoule | Word VBA | 3 | 02-25-2015 07:53 AM |