Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-09-2022, 05:39 AM
Shelley Lou Shelley Lou is offline VBA help with punctuation code Windows 10 VBA help with punctuation code Office 2016
Competent Performer
VBA help with punctuation code
 
Join Date: Dec 2020
Posts: 163
Shelley Lou is on a distinguished road
Default VBA help with punctuation code

I first posted the issue in VBA Express several months ago but haven't had a reply to that so am posting here in the hope I can resolve some of the issues within the code. I hope I haven't broken any etiquette rules by doing so.

HTML Code:
http://www.vbaexpress.com/forum/showthread.php?69330-Word-VBA-help-with-missing-punctuation-macro&highlight=
Document with comments.DOCX

I need some help to iron out a few issues with the code. The code crashes when there are comments at the end of a paragraph with no punctuation. How can I tell the code to skip/ignore comments altogether.

I have tried adding in Chr(160) to include if the semi-colon is followed by a non-breaking space instead of a space but it didn't work and now sure how to include for the code to look for both a space and/or a non-breaking space after a semi-colon.



Code:
oRng.MoveStartWhile Chr(32), wdBackward
There is a list of words that the code should ignore if the para ends with a semi-colon and any of the words, but the code isn't recognising 'and/or' and is still being highlighted. Is this because it is separated with a forward slash and therefore the code isn't recognising it?

Code:
Case "and", "but", "or", "then", "and/or", "plus", "minus", "less", "nor"
I would also like the code to ignore footnotes if they appear at the end of a paragraph as currently they are all being highlighted and not sure what to add so the code skips/ignore footnotes and also square brackets whether they appear in text form fields or not.

Code:
Sub HighlightMissingPunctuation()
Dim oPara As Paragraph
Dim oRng As Range
    Application.ScreenUpdating = False
    On Error Resume Next
    For Each oPara In ActiveDocument.Paragraphs
        With oPara.Range
            Set oRng = oPara.Range
            oRng.End = oRng.End - 1
            oRng.Collapse 0
            oRng.MoveStartWhile Chr(32), wdBackward
            oRng.text = ""
            If .Characters.Last.Previous.InRange(ActiveDocument.TablesOfContents(1).Range) = False Then
            If oPara.Range.Information(wdWithInTable) = False Then
            If Len(.text) > 2 And Not .Font.Bold And Not .Font.AllCaps 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", "and/or", "plus", "minus", "less", "nor"
                        Set oRng = .Words.Last.Previous.Words(1)
                        oRng.MoveStartWhile Chr(32), wdBackward
                        oRng.Start = oRng.Start - 1
                        If oRng.Characters(1) = ";" 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
                        End If
                        If oRng.Characters(1) = "," Then
                            'if oPara ends with these words and have comma before them highlight pink
                            .Words.Last.Previous.Words(1).HighlightColorIndex = wdPink
                        End If
                    Case Else
                End Select
            End If
        End With
    Next oPara
    Application.ScreenUpdating = True
End Sub
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA help with punctuation code VBA Move Footnote References Before Punctuation Shelley Lou Word VBA 28 09-22-2023 12:17 AM
VBA help with punctuation code VBA code help: replacing punctuation Shelley Lou Word VBA 8 07-01-2021 04:42 AM
Excluding punctuation from tracked changes counts jimmy12 Word VBA 0 06-23-2021 07:40 AM
VBA help with punctuation code punctuation order fariz Word 2 10-31-2016 12:57 AM
Punctuation lexsper Word 0 04-06-2015 07:26 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:42 PM.


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