Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-25-2019, 05:09 PM
Dave T Dave T is offline Format multiple paragraphs differently Windows 7 64bit Format multiple paragraphs differently Office 2013
Advanced Beginner
Format multiple paragraphs differently
 
Join Date: Nov 2014
Location: Australia
Posts: 66
Dave T is on a distinguished road
Default Format multiple paragraphs differently

Hello All,

I regularly paste several (three to four) short paragraphs of text from various other programs into a Word report and I am after a macro that will apply different manual formatting to each of the paragraphs.
The text is pasted into the Word document without any source formatting.



I have tried using:
Code:
With Selection.Paragraphs(1)
With Selection.Paragraphs(2)
With Selection.Paragraphs(3)
With Selection.Paragraphs(4)
This works OK, but formats the first paragraph of the document, not my selected paragraphs.

After I have selected the specific paragraphs within my document, I am after a macro that will apply the manual formatting to the selected 3-4 paragraphs only, wherever they may be within the document.

If what I have asked is not clear, I am happy to post my full code of what I have done so far.

Thanking you all in advance.
Dave T
Reply With Quote
  #2  
Old 07-25-2019, 05:41 PM
eduzs eduzs is offline Format multiple paragraphs differently Windows 10 Format multiple paragraphs differently Office 2010 32bit
Expert
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Do you want to format entire selected text/paragraphs?
Use for example "SELECTION.FONT.BOLD = TRUE"
If you need diferent format for each paragraph within the selected range:
"selection.Paragraphs(1).range.italic=true"
I think that the problem is that this will not work with multiple areas selection (select non continuous paragraphs ranges).
__________________
Backup your original file before doing any modification.
Reply With Quote
  #3  
Old 07-25-2019, 06:11 PM
Dave T Dave T is offline Format multiple paragraphs differently Windows 7 64bit Format multiple paragraphs differently Office 2013
Advanced Beginner
Format multiple paragraphs differently
 
Join Date: Nov 2014
Location: Australia
Posts: 66
Dave T is on a distinguished road
Default

Hello eduzs,

Thank you for you reply.
I have attached an edited version of what I am trying to achieve that shows the formatting I am after along with the full macro I am using.

I am not sure if my problem is where I use:
Code:
With Selection.Paragraphs(1)
or should I use something along the lines of:
Code:
Selection.Paragraphs(1).Range.Select
I am hoping someone can show me if there is a better way of achieving this result for any selected paragraphs, anywhere within the document.

Regards,
Dave T
Attached Files
File Type: docm Format selected paragraphs.docm (25.5 KB, 8 views)
Reply With Quote
  #4  
Old 07-25-2019, 06:24 PM
eduzs eduzs is offline Format multiple paragraphs differently Windows 10 Format multiple paragraphs differently Office 2010 32bit
Expert
 
Join Date: May 2017
Posts: 262
eduzs is on a distinguished road
Default

Sorry, can't open files here.
You maybe better explain what you need.
If paragraphs are already selected (continuous selection), and you want to format them all at once, then use:
Code:
With Selection
    .Font.Bold = True
End With
If you want to format only the 1st paragraph of the selection (not need to be the 1st paragraph of the document):
Code:
With Selection.Paragraphs(1).Range
    .Font.Bold = True
 End With
If you want to format only the 1st paragraph of the activedocument (not the selection)
Code:
With Activedocument.Paragraphs(1).Range
    .Font.Size= 20
End With
__________________
Backup your original file before doing any modification.
Reply With Quote
  #5  
Old 07-25-2019, 08:16 PM
gmayor's Avatar
gmayor gmayor is offline Format multiple paragraphs differently Windows 10 Format multiple paragraphs differently Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

For your code to work you need to select the four paragraphs first.

If the whole document is formatted like your example with several blocks of four separated by three empty paragraphs, you can format the lot at one go e.g.
Code:
Sub Format_Route()
'https://social.msdn.microsoft.com/Forums/en-US/d616f43f-a9c2-4e70-937b-c778ed229bb3/need-help-regarding-formatting-multiple-paragraphs-individually-in-word-using-vba?forum=isvvba

Dim i As Long
Dim oRng As Range
    For i = 1 To ActiveDocument.Paragraphs.Count Step 7
        Set oRng = ActiveDocument.Paragraphs(i).Range
        oRng.End = ActiveDocument.Paragraphs(i + 4).Range.End
        oRng.Select

        With Selection.Paragraphs(1)  'ROAD NAME
            .LeftIndent = CentimetersToPoints(0)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 4
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
            .Alignment = wdAlignParagraphLeft
            .KeepWithNext = True
            With .Range.Font
                .Name = "Times New Roman"
                .Size = 12
                .Bold = True
            End With
        End With

        With Selection.Paragraphs(2)  'RESTRICTION
            .LeftIndent = CentimetersToPoints(0.2)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 0
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
            .Alignment = wdAlignParagraphLeft
            .KeepWithNext = True
            With .Range.Font
                .Name = "Times New Roman"
                .Size = 12
                .Bold = True
            End With
        End With

        With Selection.Paragraphs(3)  'STRUCTURE TYPE AND STRUCTURE NUMBER
            .LeftIndent = CentimetersToPoints(1.27)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 0
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
            .Alignment = wdAlignParagraphLeft
            .KeepWithNext = True
            With .Range.Font
                .Name = "Times New Roman"
                .Size = 12
                .Bold = False
            End With
        End With

        With Selection.Paragraphs(4)  'STRUCTURE LOCATION
            .LeftIndent = CentimetersToPoints(1.27)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 0
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
            .Alignment = wdAlignParagraphLeft
            .KeepWithNext = False
            With .Range.Font
                .Name = "Times New Roman"
                .Size = 12
                .Bold = False
            End With
        End With
    Next i
End Sub
or simply program the ranges without selecting them e.g.
Code:
Sub Format_Route()
'https://social.msdn.microsoft.com/Forums/en-US/d616f43f-a9c2-4e70-937b-c778ed229bb3/need-help-regarding-formatting-multiple-paragraphs-individually-in-word-using-vba?forum=isvvba

Dim i As Long
Dim oRng As Range
    For i = 1 To ActiveDocument.Paragraphs.Count Step 7
        Set oRng = ActiveDocument.Paragraphs(i).Range
        oRng.End = ActiveDocument.Paragraphs(i + 4).Range.End
 
        With oRng.Paragraphs(1)  'ROAD NAME
            .LeftIndent = CentimetersToPoints(0)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 4
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
            .Alignment = wdAlignParagraphLeft
            .KeepWithNext = True
            With .Range.Font
                .Name = "Times New Roman"
                .Size = 12
                .Bold = True
            End With
        End With

        With oRng.Paragraphs(2)  'RESTRICTION
            .LeftIndent = CentimetersToPoints(0.2)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 0
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
            .Alignment = wdAlignParagraphLeft
            .KeepWithNext = True
            With .Range.Font
                .Name = "Times New Roman"
                .Size = 12
                .Bold = True
            End With
        End With

        With oRng.Paragraphs(3)  'STRUCTURE TYPE AND STRUCTURE NUMBER
            .LeftIndent = CentimetersToPoints(1.27)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 0
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
            .Alignment = wdAlignParagraphLeft
            .KeepWithNext = True
            With .Range.Font
                .Name = "Times New Roman"
                .Size = 12
                .Bold = False
            End With
        End With

        With oRng.Paragraphs(4)  'STRUCTURE LOCATION
            .LeftIndent = CentimetersToPoints(1.27)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 0
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
            .Alignment = wdAlignParagraphLeft
            .KeepWithNext = False
            With .Range.Font
                .Name = "Times New Roman"
                .Size = 12
                .Bold = False
            End With
        End With
    Next i
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
  #6  
Old 07-28-2019, 04:21 PM
Dave T Dave T is offline Format multiple paragraphs differently Windows 7 64bit Format multiple paragraphs differently Office 2013
Advanced Beginner
Format multiple paragraphs differently
 
Join Date: Nov 2014
Location: Australia
Posts: 66
Dave T is on a distinguished road
Default

Hello Graham,

I really appreciate your reply, and I have regularly looked at your excellent website.
When I looked at your reply I realised that the example I posted was probably a poor choice.

The paragraph produced by the other program is inserted at random locations throughout the document. I wanted to be able to select just those four inserted paragraphs and apply the manual formatting just to them and nothing else.

For some reason the original macro I was using applied the manual formatting to the first four paragraphs in the document. I also wasn't sure if my approach was the most efficient method.

How can I modify your macro to just edit the four selected paragraphs ?

Regards,
Dave T
Reply With Quote
  #7  
Old 07-28-2019, 11:29 PM
gmayor's Avatar
gmayor gmayor is offline Format multiple paragraphs differently Windows 10 Format multiple paragraphs differently Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Remove the loop e.g.
Code:
Sub Format_Route()
'https://social.msdn.microsoft.com/Forums/en-US/d616f43f-a9c2-4e70-937b-c778ed229bb3/need-help-regarding-formatting-multiple-paragraphs-individually-in-word-using-vba?forum=isvvba

    If Not Selection.Paragraphs.Count = 4 Then
        MsgBox "Select the four paragraphs to be formatted"
        Exit Sub
    End If

    With Selection.Paragraphs(1)  'ROAD NAME
        .LeftIndent = CentimetersToPoints(0)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 4
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceSingle
        .Alignment = wdAlignParagraphLeft
        .KeepWithNext = True
        With .Range.Font
            .Name = "Times New Roman"
            .Size = 12
            .Bold = True
        End With
    End With

    With Selection.Paragraphs(2)  'RESTRICTION
        .LeftIndent = CentimetersToPoints(0.2)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceSingle
        .Alignment = wdAlignParagraphLeft
        .KeepWithNext = True
        With .Range.Font
            .Name = "Times New Roman"
            .Size = 12
            .Bold = True
        End With
    End With

    With Selection.Paragraphs(3)  'STRUCTURE TYPE AND STRUCTURE NUMBER
        .LeftIndent = CentimetersToPoints(1.27)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceSingle
        .Alignment = wdAlignParagraphLeft
        .KeepWithNext = True
        With .Range.Font
            .Name = "Times New Roman"
            .Size = 12
            .Bold = False
        End With
    End With

    With Selection.Paragraphs(4)  'STRUCTURE LOCATION
        .LeftIndent = CentimetersToPoints(1.27)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceSingle
        .Alignment = wdAlignParagraphLeft
        .KeepWithNext = False
        With .Range.Font
            .Name = "Times New Roman"
            .Size = 12
            .Bold = False
        End With
    End With
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
  #8  
Old 07-31-2019, 08:12 PM
Dave T Dave T is offline Format multiple paragraphs differently Windows 7 64bit Format multiple paragraphs differently Office 2013
Advanced Beginner
Format multiple paragraphs differently
 
Join Date: Nov 2014
Location: Australia
Posts: 66
Dave T is on a distinguished road
Default

Hello Graham,

Once again I really appreciate your reply.

Looking at what your last post contained, I can see I was very close.
The addition of the requirement to have selected four paragraphs before the macro is run is a very nice touch.

Thanks very much, regards,
Dave T
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing the format/style for numbered paragraphs AND MAKING THEM STICK! FearlessStamper Word 2 10-30-2017 02:17 PM
Copying paragraphs from multiple documents to the current one. paik1002 Word VBA 3 02-22-2016 04:03 AM
Format multiple paragraphs differently VBA to set format for paragraphs that meet with specific requirements AustinBrister Word VBA 3 06-01-2015 07:00 AM
Word 2003 form fields and pasting multiple paragraphs. mwmoron Word 1 12-07-2012 11:06 PM
Format multiple paragraphs differently Find and Replace multiple lines/paragraphs jcw Word 1 11-18-2011 11:47 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:06 AM.


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