PDA

View Full Version : Highlighting misspelt text, but ignoring certain text



PosIIx
11-26-2016, 03:46 PM
Hi All,

My first post to this forum, so I apologize in advance if I breach convention in any way.

I'm having trouble finding and ignoring all instances of certain text in a macro Im using to highlight misspelled words. I'm currently using the following to highlight misspelled text in a macro (this is only part of what this macro does):

Dim rng As Range
Dim docSource As Document
Set docSource = ActiveDocument

For Each rng In docSource.SpellingErrors

rng.Font.Size = 16
rng.Font.Underline = wdUnderlineDouble
rng.Font.ColorIndex = wdGreen

Next


For context, I define terms in the documents I write. Often defined terms are acronyms that show up as misspelled words. I define terms in one of three ways:

("Ttext") or (the "Text") or ("Text", and together wtih X, Y, and Z, collectively the "Texts").

Simply, I'd like my spellcheck macro to ignore any words between quotes within parentheses. I know how to use characters (^210 for example) and can identify character numbers with the following macro:

Sub WhatCharacterCode()
MsgBox Asc(Selection.Text)
End Sub

I'm very new to VBA and I'm not sure how to proceed. Is there an easy way to do this?

Thanks so much for your time and assistance.

Brent

gmaxey
11-27-2016, 09:34 AM
Brent,

You have to create the conditional statements. I'm not sure how you apply the parenthesis, but this might get you closer:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRngErr As Range
Dim oDoc As Document
Set oDoc = ActiveDocument
For Each oRngErr In oDoc.SpellingErrors
With oRngErr
If .Characters.First.Previous = Chr(34) Or .Characters.First.Previous = ChrW(8220) And _
.Characters.Last.Next = Chr(34) Or .Characters.Last.Next = ChrW(8221) Then
'Skips errors in plain or curly qoutes.
Else

With .Font
.Size = 16
.Underline = wdUnderlineDouble
.ColorIndex = wdGreen
End With
End If

End With
Next
lbl_Exit:
Exit Sub
End Sub

PosIIx
11-27-2016, 04:56 PM
Hi Greg,

Thanks so much for responding. I altered the relevant part of what you provided to read:

If .Characters.First.Previous = Chr(34) Or .Characters.First.Previous = ChrW(8220) And _
.Text = " the " And .Characters.Last.Next = Chr(34) Or .Characters.Last.Next = ChrW(8221)

It now ignores anything in the quotes I use. What I was hoping to do now is probably more complex. Do you know how I might get it to ignore all instances of any word that appeared in quotes, even when they occur elsewhere in the document without them (basically, an ignore all). For instance, is it possible to create a sub-loop that for instance, selects the text within quotes, then changes the font of all instances of that text in the document to, say 10.5, then jumps back to the main loop and looks for the next quotation marks and does the same for the text therein, until all are changed, then runs the spellcheck on all text that is not 10.5 size font (I can figure out from your code how to do that part), then changes all text at 10.5 back to 11 (I can do this part as well).

I feel like I'm over-asking here, please let me know if I am. I'm really new to this.

Thanks for your help.

Brent

gmaxey
11-27-2016, 06:22 PM
You will need to give me an simplified example of what the document looks like before the code is run and what it should look like after the code is run.

PosIIx
11-27-2016, 08:00 PM
Hi Greg,

Sure Greg, thanks again. A simplified version of what I'm after would look a little like this:

Before

Mr. John D Anderues ("Anderues") is involved in the Alpha projecct. Anderues is a senior analysst. Andruus is good at his job.

After

Mr. John D Anderues ("Anderues") is involved in the Alpha projecct. Anderues is a senior analysst. Andruus is good at his job.

Above, the only misspelled words whose fonts are altered are those that were never in quotes. This shows all other misspelled words, including instances where words in quotes were misspelled (the last misspelled word above).


My Thoughts on a Solution

A. Loop 1

1. The loop would identify move from word to word until it finds text within quotes within parentheses that is not 10.5 font.
2. The subloop would then start at the beginning of the document and change all instances of that word (including the one in quotes) to font 10.5. It would do make it distinct from the rest of the text. (the 10.5 font change is arbitrary, any change would work)
3. the loop would then restart at the start of the document until it finds the next word in quotes within parenthesis.
4. Repeat step 2.


... until all words in quotes are made 10.5 font.

B. Spell Check

Run spell check on all text that is not 10.5 font, changing the font and size of misspelled words to 16 green as in my original text.

C. Change all text that is 10.5 font to 11 size font (I can handle this).

The result would be none of the words that ever appeared in quotes were included in the spell check. I'm really having trouble with steps 1 and 2 of Loop 1 above.

Thanks so much for your help. I appreciate you taking the time. If I'm asking too much, please let me know.

Brent

gmaxey
11-27-2016, 08:24 PM
Try this:


Sub ScratchMacro()
'A basic Word macro code by Gregory K. Maxey
Dim oRngErr As Range
Dim oDoc As Document
Dim oCol As New Collection
Set oDoc = ActiveDocument
For Each oRngErr In oDoc.SpellingErrors
With oRngErr
If .Characters.First.Previous = Chr(34) Or .Characters.First.Previous = ChrW(8220) And _
.Characters.Last.Next = Chr(34) Or .Characters.Last.Next = ChrW(8221) Then
On Error Resume Next
oCol.Add oRngErr, oRngErr
On Error GoTo 0
End If
End With
Next oRngErr
For Each oRngErr In oDoc.SpellingErrors
On Error Resume Next
oCol.Add oRngErr, oRngErr
If Err.Number = 0 Then
With oRngErr.Font
.Size = 16
.Underline = wdUnderlineDouble
.ColorIndex = wdGreen
End With
oCol.Remove (oCol.Count)
Else
oRngErr.SpellingChecked = True
End If
Next oRngErr
lbl_Exit:
Exit Sub
End Sub

PosIIx
11-29-2016, 08:17 PM
Wow, Greg that works beautifully. Thank you so much! Much obliged!

Brent

PosIIx
11-29-2016, 09:55 PM
Hi Greg,

I have one more question if you don't mind.


I'm using the following code to replace text in one column of table I've set up with text in another. I would like the process to ignore text that has already been replaced. The way I hoped to do this was to make the replaced text font size 10, then have the script ignore all text that isnt font size 11. This way the edited text is re-edited. Here is the code I'm using:

' from Doug Robbins, Word MVP, Microsoft forums, Feb 2015, based on another macro written by Graham Mayor, Aug 2010
ActiveDocument.TrackRevisions = Not ActiveDocument.TrackRevisions
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim oRng As Range
Dim rFindText As Range, rReplacement As Range
Dim i As Long
Dim sFname As String
'Change the path in the line below to reflect the name and path of the table document
sFname = "C:\Users\bcg\Documents\Macro Table\Table.docx"
Set oDoc = ActiveDocument
Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
Set oTable = oChanges.Tables(1)
For i = 1 To oTable.Rows.Count
Set oRng = oDoc.Range
Set rFindText = oTable.Cell(i, 1).Range
rFindText.End = rFindText.End - 1
Set rReplacement = oTable.Cell(i, 2).Range
rReplacement.End = rReplacement.End - 1
Selection.HomeKey wdStory
With oRng
If .Characters.First.Font.Size = 10 Then
Else

With .Find
.Replacement.Font.Size = 10
.MatchWildcards = True
.Text = rFindText.Text
.Replacement.Text = rReplacement.Text
.Forward = True
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End If
End With
Next i
oChanges.Close wdDoNotSaveChanges
ActiveDocument.TrackRevisions = Not ActiveDocument.TrackRevisions
.So far the code will replace the text with font size 10, but it doesn't seem to ignore size 10 font on the next pass? Can you see an easy way to make it do that?

Thanks so much again.

Brent

PosIIx
11-30-2016, 10:36 AM
Hi Greg,

Please ignore my last message - I tinkered with it and figured that out.

Thanks so much!

Brent