#1
|
|||
|
|||
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) 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 |
#2
|
|||
|
|||
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. |
#3
|
|||
|
|||
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) Code:
Selection.Paragraphs(1).Range.Select Regards, Dave T |
#4
|
|||
|
|||
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 Code:
With Selection.Paragraphs(1).Range .Font.Bold = True End With Code:
With Activedocument.Paragraphs(1).Range .Font.Size= 20 End With
__________________
Backup your original file before doing any modification. |
#5
|
||||
|
||||
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 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 |
#6
|
|||
|
|||
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 |
#7
|
||||
|
||||
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 |
#8
|
|||
|
|||
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 |
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 |
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 |
Find and Replace multiple lines/paragraphs | jcw | Word | 1 | 11-18-2011 11:47 AM |