Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-08-2023, 09:58 AM
RobiNew RobiNew is offline Formatting specific paragraphs with styles without using Selection Windows 10 Formatting specific paragraphs with styles without using Selection Office 2016
Competent Performer
Formatting specific paragraphs with styles without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default 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

Reply With Quote
  #2  
Old 11-08-2023, 08:59 PM
gmaxey gmaxey is offline Formatting specific paragraphs with styles without using Selection Windows 10 Formatting specific paragraphs with styles without using Selection Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 11-09-2023, 12:31 AM
RobiNew RobiNew is offline Formatting specific paragraphs with styles without using Selection Windows 10 Formatting specific paragraphs with styles without using Selection Office 2016
Competent Performer
Formatting specific paragraphs with styles without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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"
Reply With Quote
  #4  
Old 11-09-2023, 07:38 AM
RobiNew RobiNew is offline Formatting specific paragraphs with styles without using Selection Windows 10 Formatting specific paragraphs with styles without using Selection Office 2016
Competent Performer
Formatting specific paragraphs with styles without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 11-09-2023, 08:42 AM
gmaxey gmaxey is offline Formatting specific paragraphs with styles without using Selection Windows 10 Formatting specific paragraphs with styles without using Selection Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Yes, that is how you would do it. You got the error because you never set the oRng object to a document range.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 11-09-2023, 09:01 AM
RobiNew RobiNew is offline Formatting specific paragraphs with styles without using Selection Windows 10 Formatting specific paragraphs with styles without using Selection Office 2016
Competent Performer
Formatting specific paragraphs with styles without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

What kind of code should I insert if I wanted to avoid the first two paragraph of the doc? Thanks!
Reply With Quote
  #7  
Old 11-09-2023, 09:10 AM
gmaxey gmaxey is offline Formatting specific paragraphs with styles without using Selection Windows 10 Formatting specific paragraphs with styles without using Selection Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 11-09-2023, 09:56 AM
RobiNew RobiNew is offline Formatting specific paragraphs with styles without using Selection Windows 10 Formatting specific paragraphs with styles without using Selection Office 2016
Competent Performer
Formatting specific paragraphs with styles without using Selection
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Of course! Thanks a lot, anyway.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting specific paragraphs with styles without using Selection 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
Formatting specific paragraphs with styles without using Selection 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
Formatting specific paragraphs with styles without using Selection 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:19 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