Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-15-2024, 08:54 AM
gmaxey gmaxey is offline VBA IF Statement Help Windows 10 VBA IF Statement Help Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Shelley,




If you pull that loop out and run it on your test document, you will see where and why the error occurs:

Code:
Sub A()
Dim oPara As Paragraph
Dim Rng As Range
  On Error GoTo Err_Handler:
For Each oPara In ActiveDocument.Paragraphs 'Highlights missing punctuation at end of paragraphs
    With oPara.Range
    If .Information(wdWithInTable) Or .Font.AllCaps Or .Characters.First.Font.Bold Or Len(.Text) < 3 Then
        GoTo NextFor
      Else
      If Not .Characters.Last.Previous Like "*[.!?:;,]" Then
      If Not .Characters.Last.Previous.Fields(1).Result = "]" Then
      If Not .Characters.Last.Previous Like "]" Then
      .Words.Last.Previous.Words(1).HighlightColorIndex = wdPink
       End If
       End If
       End If
       
      Select Case .Words.Last.Previous.Words(1)
      Case "and", "but", "or", "then", "plus", "minus", "less", "nor"
      Set Rng = .Words.Last    '.Previous.Words(1)
      Rng.MoveStartUntil cSet:=" " & Chr(160), Count:=-10
      Set Rng = Rng.Characters.First.Previous.Previous
      If Rng.Text = ";" Then
      'if oPara ends with these words and have semi-colon before them do nothing no highlight else
      .Words.Last.Previous.Words(1).HighlightColorIndex = wdNoHighlight
      If Rng.Text = "," Then
      'if oPara ends with these words and have semi-colon before them do nothing no highlight else
      .Words.Last.Previous.Words(1).HighlightColorIndex = wdPink
      End If
      End If
      Case Else
      End Select
      End If
      End With
NextFor:
  Next
lbl_Exit:
  Exit Sub
Err_Handler:
  MsgBox Err.Number & " - " & Err.Description
  oPara.Range.Select
  Resume
End Sub

There is no field(1) in the range.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #2  
Old 03-15-2024, 09:58 AM
Shelley Lou Shelley Lou is offline VBA IF Statement Help Windows 10 VBA IF Statement Help Office 2016
Expert
VBA IF Statement Help
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA IF Statement Help

Hi Greg, oh crikey I'm confused now as I thought it was working ok before. I thought I'd got the code to not highlight paragraphs that end with a closing square bracket which may be in the form of a text form field or just plain text square brackets when looking for missing punctuation at the end of paragraphs. Is that not how the code is written? I want the code to miss those paragraphs - I'm not sure how else to write the code.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA IF Statement Help IF Statement (again) teza2k06 Excel 8 02-11-2022 08:41 AM
Converting a Select statement in Excel to an update statement shabbaranks Excel Programming 5 10-31-2018 11:47 PM
VBA IF Statement Help Need a little help with an if statement cangelis Excel 2 04-08-2015 05:55 PM
VBA IF Statement Help If statement, may be? Tony Singh Excel 6 03-04-2015 12:52 PM
VBA IF Statement Help Need help with If, Then Statement Please cangelis Excel 4 01-03-2014 09:10 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:11 PM.


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