#1
|
|||
|
|||
Formatting specific paragraphs with styles without using Selection
I need to format certain paragraphs with styles, but apart from the fact that I do not want to use Selection, my code here below produces unsatisfactory results. Can someone help? Thanks!
Code:
'Formatting first three paragraphs Dim parRng As Range Set parRng = ActiveDocument.Paragraphs(1).Range parRng.Select parRng.Style = ActiveDocument.Styles("AuthorPar") parRng.Move Unit:=wdParagraph, Count:=1 parRng.Select parRng.Style = ActiveDocument.Styles("LocPar") parRng.Move Unit:=wdParagraph, Count:=1 parRng.Select parRng.Style = ActiveDocument.Styles("Title") 'this should be in italics (but fails) --------------- 'Formatting unnumbered subtitles For Each Paragraph In ActiveDocument.Paragraphs NrPar = NrPar + 1 If Paragraph.Range.ComputeStatistics(wdStatisticLines) = 1 _ And Paragraph.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft _ And Not Paragraph.Range Like "*[0-9]*" Then ActiveDocument.Paragraphs(NrPar).Range.Select Selection.ClearFormatting 'apparently necessary Selection.Style = ActiveDocument.Styles("NotNrPar") 'this should be in italics (OK) End If Next Paragraph NrPar = 0 |
#2
|
|||
|
|||
You certainly don't (and rarely do) need to use selection. You will have to substitute your own style names:
Code:
Sub ScratchMacro() 'A basic Word Macro coded by Gregory K. Maxey Dim oPar As Paragraph Dim oRng As Range With ActiveDocument Set oRng = .Range .Paragraphs(1).Range.Style = "Heading 1" .Paragraphs(2).Range.Style = "Heading 2" .Paragraphs(3).Range.Style = "Heading 3" oRng.Start = .Paragraphs(4).Range.Start End With 'Format unnumbered subtitles For Each oPar In oRng.Paragraphs If oPar.Range.ComputeStatistics(wdStatisticLines) = 1 _ And oPar.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft _ And Not oPar.Range Like "*[0-9]*" Then oPar.Range.Style = "Body Text" End If Next oPar lbl_Exit: Set oPar = Nothing Exit Sub End Sub |
#3
|
|||
|
|||
Thank you very much indeed, Gmaxey! It is all very instructive. The code works perfectly, but the style in .Paragraphs(3) - which includes Character: Italics - fails to convert to italics. Is there a simple way to force the change? Many thanks!
UPDATE: I managed to amend using: .Paragraphs(3).Range.Style = wdStyleDefaultParagraphFont .Paragraphs(3).Range.Style = "Mystyle" |
#4
|
|||
|
|||
Hi, Gmaxey! I tried this code of yours on its own and it produces an error: Object variable or With block variable not set. Please help. Thanks!
Code:
Sub Unnumbered() Dim oPar As Paragraph Dim oRng As Range For Each oPar In oRng.Paragraphs If oPar.Range.ComputeStatistics(wdStatisticLines) = 1 _ And oPar.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft _ And Not oPar.Range Like "*[0-9]*" Then oPar.Range.Style = "Mystyle" End If Next oPar End Sub UPDATE: I managed to amend with: For Each oPar In ActiveDocument.Paragraphs |
#5
|
|||
|
|||
Yes, that is how you would do it. You got the error because you never set the oRng object to a document range.
|
#6
|
|||
|
|||
What kind of code should I insert if I wanted to avoid the first two paragraph of the doc? Thanks!
|
#7
|
|||
|
|||
You kind of have your answer already in this thread
Code:
Sub ScratchMacro() 'A basic Word Macro coded by Gregory K. Maxey Dim oPar As Paragraph Dim oRng As Range With ActiveDocument Set oRng = .Range oRng.Start = .Paragraphs(3).Range.Start End With 'Format unnumbered subtitles For Each oPar In oRng.Paragraphs If oPar.Range.ComputeStatistics(wdStatisticLines) = 1 _ And oPar.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft _ And Not oPar.Range Like "*[0-9]*" Then oPar.Range.Style = "Body Text" End If Next oPar lbl_Exit: Set oPar = Nothing Exit Sub End Sub |
#8
|
|||
|
|||
Of course! Thanks a lot, anyway.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Assign different heading styles for different paragraphs | Thorin23 | Word VBA | 3 | 05-02-2021 06:22 AM |
Formatting all paragraphs based on pre defined styles | afif | Word VBA | 0 | 07-26-2019 06:18 AM |
Make paragraphs appear according to specific combination | razdul | Word VBA | 2 | 04-17-2019 06:17 AM |
Trouble Formatting Specific Paragraphs - Bullets, Spacing, Centering, etc | adzialo | Word | 2 | 05-01-2017 12:53 PM |
Selection of all Text for a specific page in word is spanning selection across pages | ramsgarla | Word VBA | 9 | 12-05-2012 03:23 AM |