PDA

View Full Version : Detecting & Modifying Phonetic Text in a Selection



dmekonnen
11-25-2018, 05:34 AM
Greetings,

I am working on a Word VBA sub to automate tasks that I can readily do in the UI or by working with Open XML directly, but finding it unexpectedly difficult in VBA. For text that has phonetic guide text applied (aka Ruby Annotation, Furigana), I would like to access just the phonetic text. My goal is to then check spelling, and change its font color to red.

How can you get to just this phonetic text from a Selection, by drilling through properties and functions? The Open XML equivalent that I'm after would be the w:rt node under the selection.

Searching the document for a keyword in the phonetic guide text, Word will find it, then its color can be changed separate from the base text. It seems that there should be a way to do the same with VBA.

thanks,

-Daniel

macropod
11-29-2018, 08:49 PM
If you know what the pnetic works are, you could use code like:

Sub CheckWords()
Application.ScreenUpdating = False
Dim strWords As String, i As Long
strWords = strWords & "a,am,an,are,been,began,brought,can,check,come,"
strWords = strWords & "do,find,found,get,give,go,have,hear,let,lot,"
strWords = strWords & "make,may,me,might,number,numeral,oh,plain,plane,"
strWords = strWords & "pose,pound,query,quiet,quite,ran,run,say,see,"
strWords = strWords & "should,song,spell,state,stood,those,take,would"
With ActiveDocumentRange.Find
.ClearFormatting
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Wrap = wdFindContinue
With .Replacement
.ClearFormatting
.Text = "^&"
.Font.Color = wdColorRed
End With
For i = 0 To UBound(Split(strWords, ","))
.Text = Split(strWords, ",")(i)
.Execute Replace:=wdReplaceAll
Next i
End With
Application.ScreenUpdating = True
End Sub

and replace the various words in the strWords variable with the phonetic words you're concerned with. You can add/delete
strWords definition lines as desired - just ensure all except the last terminate with a comma followed by a double quote; the final line should have
double quote but not the preceding comma.