Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #9  
Old 03-15-2024, 03:28 PM
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, I've added the new lines of code to my macro you have provided. If I leave the Err Handler bit in it makes all my auto cross refs highlighted when they shouldn't be, but when I comment them out its throwing up an Error 5825 Object has been deleted. I have marked the line of code red in the below code. The last bit of the code is supposed to leave any auto cross refs/fields unchanged and only highlight the manual ones. Any idea why this might be happening?

Code:
'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
          Select Case True
            Case .Characters.Last.Previous.Fields.count = 1
              If Not .Characters.Last.Previous.Fields(1).Result = "]" Then
                .Words.Last.Previous.Words(1).HighlightColorIndex = wdBrightGreen
              End If
            Case Else
              If Not .Characters.Last.Previous Like "]" Then
                .Words.Last.Previous.Words(1).HighlightColorIndex = wdBrightGreen
              End If
          End Select
        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
          Options.DefaultHighlightColorIndex = wdNoHighlight 'Don't highlight anything already in fields eg cross refs and square brackets
          ActiveWindow.View.ShowFieldCodes = True
          .ClearFormatting
          .MatchWildcards = False
          .text = "^d"
          .Execute Replace:=wdReplaceAll
          ActiveWindow.View.ShowFieldCodes = False
        End With
      Case Else
    End Select
  End With
Next Rng
Reply With Quote
 



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 11:38 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