Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-18-2020, 05:55 PM
Hockey Hockey is offline macro to insert paragraph Windows 7 64bit macro to insert paragraph Office 2007
Novice
macro to insert paragraph
 
Join Date: Jan 2020
Posts: 4
Hockey is on a distinguished road
Default macro to insert paragraph

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.
Reply With Quote
  #2  
Old 01-18-2020, 10:07 PM
gmayor's Avatar
gmayor gmayor is offline macro to insert paragraph Windows 10 macro to insert paragraph Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,154
gmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of light
Default

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
Reply With Quote
  #3  
Old 01-19-2020, 06:18 PM
Hockey Hockey is offline macro to insert paragraph Windows 7 64bit macro to insert paragraph Office 2007
Novice
macro to insert paragraph
 
Join Date: Jan 2020
Posts: 4
Hockey is on a distinguished road
Default

@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.
Reply With Quote
  #4  
Old 01-20-2020, 12:12 AM
gmayor's Avatar
gmayor gmayor is offline macro to insert paragraph Windows 10 macro to insert paragraph Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,154
gmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of lightgmayor is a glorious beacon of light
Default

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
The main issue with inserting paragraphs at regular intervals is that of keeping track of the number of paragraphs. However you could do it another way without the need for changing the styles
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
By omitting "+" & from the line
Code:
oRng.InsertAfter "+" & vbCr
this method could be used to insert empty paragraphs, though the styles are a better choice for that.
__________________
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.
Reply With Quote
  #5  
Old 01-20-2020, 03:20 AM
Hockey Hockey is offline macro to insert paragraph Windows 7 64bit macro to insert paragraph Office 2007
Novice
macro to insert paragraph
 
Join Date: Jan 2020
Posts: 4
Hockey is on a distinguished road
Default

@Graham Mayor. Again many thanks for ur great help.
Reply With Quote
  #6  
Old 01-28-2020, 06:21 AM
Hockey Hockey is offline macro to insert paragraph Windows 7 64bit macro to insert paragraph Office 2007
Novice
macro to insert paragraph
 
Join Date: Jan 2020
Posts: 4
Hockey is on a distinguished road
Default

thank you very much. my prblem is solves. many thanks.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to insert paragraph Word macro to insert text at the beginning of paragraph but skip tables ashalles135 Word VBA 5 09-26-2018 09:49 AM
macro to insert paragraph 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 paragraph 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
macro to insert paragraph Insert paragraph break before images jsoule Word VBA 3 02-25-2015 07:53 AM
macro to insert paragraph How to insert paragraph character after every 500 characters? aditya_bokade Word VBA 21 05-09-2014 03:56 AM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 09:31 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft