Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-10-2023, 02:11 AM
RobiNew RobiNew is offline Apply style to next paragraph Windows 10 Apply style to next paragraph Office 2016
Competent Performer
Apply style to next paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default Apply style to next paragraph

I'm trying to apply a style to the next paragraph after changing the preceding one, but I get an error. Can someone help? Thanks!


Code:
Dim oPar As Paragraph
 For Each oPar In ActiveDocument.Paragraphs
    If oPar.Range.ParagraphFormat.Alignment = wdAlignParagraphRight Then
      oPar.Range.Style = "MyStyle1"
'      oPar.Next.Style = "MyStyle2" '---> I get an error
    End If
Next oPar
Reply With Quote
  #2  
Old 11-10-2023, 03:51 AM
vivka vivka is offline Apply style to next paragraph Windows 7 64bit Apply style to next paragraph Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! Although I didn't test it, but try:
oPar.range.Next.Style = "MyStyle2"
Reply With Quote
  #3  
Old 11-10-2023, 04:32 AM
gmaxey gmaxey is offline Apply style to next paragraph Windows 10 Apply style to next paragraph 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

RobiNew,


I doubt that would make a difference. The most likely problem is that you find yourself at the end of the document and there is no .Next paragraph to apply the style to.


When you post, instead of "I get an error" How about I get the following error Err.Number and Err.Description.


Try:


Code:
Sub A()
  Dim oPar As Paragraph
  For Each oPar In ActiveDocument.Paragraphs
    If oPar.Range.ParagraphFormat.Alignment = wdAlignParagraphRight Then
      oPar.Style = "MyStyle1"
      If Not oPar.Range.End = ActiveDocument.Range.End Then
        oPar.Next.Style = "MyStyle2" '---> I get an error
      End If
    'End If
  Next oPar
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 11-10-2023, 04:46 AM
gmaxey gmaxey is offline Apply style to next paragraph Windows 10 Apply style to next paragraph 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

There are better ways than the simple fix above.



For one, if your document has a thousand or say a million paragraphs, it would be inefficient to evaluate each one to see if it was the last one. So, since you now know that there is no next paragraph following the last paragraph of a document you could use an error handler:


Code:
Sub A()
  Dim oPar As Paragraph
  For Each oPar In ActiveDocument.Paragraphs
    'If oPar.Range.ParagraphFormat.Alignment = wdAlignParagraphRight Then
      oPar.Style = "Body Text"
      On Error GoTo Err_Handler
      oPar.Next.Style = "Body Text"
    'End If
  Next oPar
lbl_Exit:
  Exit Sub
Err_Handler:
  If Err.Number = 91 Then
    If oPar.Range.End = ActiveDocument.Range.End Then
      'You know this error will occur so it is not an issue
      
    Else
      MsgBox Err.Number & " " & Err.Description
    End If
  Else
     MsgBox Err.Number & " " & Err.Description
  End If
  Resume lbl_Exit
End Sub

or you could adjust your processing range so you never reach the last paragraph:


Code:
Sub B()
Dim oRng As Range
  Dim oPar As Paragraph
  Set oRng = ActiveDocument.Range
  oRng.End = ActiveDocument.Range.Paragraphs.Last.Previous.Range.End
  For Each oPar In oRng.Paragraphs
    If oPar.Range.ParagraphFormat.Alignment = wdAlignParagraphRight Then
      oPar.Style = "Body Text"
      oPar.Next.Style = "Body Text"
    End If
  Next oPar
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 11-10-2023, 07:37 AM
RobiNew RobiNew is offline Apply style to next paragraph Windows 10 Apply style to next paragraph Office 2016
Competent Performer
Apply style to next paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thanks, Vivka! Gmaxey is quite right: I had tried the line you suggest before posting, then I discovered that the error was due to the structure of the larger macro in which the code was inserted. So Gmaxey is again quite right: "How about: I get the following error Err.Number and Err.Description". Thanks, Gmaxey for the very instructive alternatives! Now everything seems to be OK.
Reply With Quote
  #6  
Old 11-10-2023, 08:38 AM
vivka vivka is offline Apply style to next paragraph Windows 7 64bit Apply style to next paragraph Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Yes, I completely agree with Greg!
Reply With Quote
  #7  
Old 11-10-2023, 10:42 AM
RobiNew RobiNew is offline Apply style to next paragraph Windows 10 Apply style to next paragraph Office 2016
Competent Performer
Apply style to next paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Suppose the condition in Sub B() was: If oPar.Range Like Chr(2). Why is it that the code doesn't recongnize the presence of a footnote reference mark? Thanks!
Reply With Quote
  #8  
Old 11-10-2023, 11:42 AM
gmaxey gmaxey is offline Apply style to next paragraph Windows 10 Apply style to next paragraph 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

What does oPar.Range Like Chr(2) have to do with the presence or absence of a footnote?


How about: If oPar.Range.Footnotes.Count > 0 Then
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 11-11-2023, 12:45 AM
RobiNew RobiNew is offline Apply style to next paragraph Windows 10 Apply style to next paragraph Office 2016
Competent Performer
Apply style to next paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

It's perfect. Thank you very much indeed, Gmaxey, for helping me in the transition from Selection to Range!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to apply a style to a group of words inside a paragraph? saint—112 Word 3 09-07-2022 09:55 PM
Apply table style shahid.majeed Word Tables 0 10-02-2019 01:25 AM
My style keeps changing, can't get saved style to apply Meenie50 Word 7 07-20-2017 03:47 PM
Paragraph space before - can styles apply it intelligently? timpani Word 7 10-23-2012 04:08 PM
Character style stripped while applying paragraph style sams_gates Word 0 08-29-2009 02:03 AM

Other Forums: Access Forums

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