―――――――――――――――――
Option Explicit
Sub AozoraRubyToWordRuby()
' 青空文庫形式:|親文字《ルビ》 を、Wordのルビ(PhoneticGuide)に変換する
' ルビ書式:游明朝 10pt を指定(必要なら下の定数を変更)
Const RUBY_FONT_NAME As String = "游明朝"
Const RUBY_FONT_SIZE As Single = 10
Const RUBY_RAISE As Long = 0 ' 上下位置。必要なら 1~2 程度で微調整
Dim rngSearch As Range
Set rngSearch = ActiveDocument.Content
With rngSearch.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.Text = "|(*)《(*)》"
.Replacement.Text = ""
.Wrap = wdFindStop
.Forward = True
.Format = False
End With
Do While rngSearch.Find.Execute = True
Dim matchString As String
matchString = rngSearch.Text
Dim posOpen As Long, posClose As Long
posOpen = InStr(matchString, "《")
posClose = InStr(matchString, "》")
If posOpen <= 2 Or posClose <= posOpen Then
rngSearch.Collapse Direction:=wdCollapseEnd
GoTo ContinueLoop
End If
Dim parentText As String, rubyText As String
parentText = Mid$(matchString, 2, posOpen - 2)
rubyText = Mid$(matchString, posOpen + 1, posClose - (posOpen + 1))
' 念のため空を弾く
If Len(parentText) = 0 Or Len(rubyText) = 0 Then
rngSearch.Collapse Direction:=wdCollapseEnd
GoTo ContinueLoop
End If
' 親文字に置換
rngSearch.Text = parentText
' ルビを付与(ルビ側のフォント/サイズを指定)
rngSearch.PhoneticGuide rubyText, _
wdPhoneticGuideAlignmentCenter, _
RUBY_RAISE, _
RUBY_FONT_SIZE, _
RUBY_FONT_NAME
' 次の検索へ(いま置換した箇所の末尾に移動)
rngSearch.Collapse Direction:=wdCollapseEnd
ContinueLoop:
Loop
End Sub
―――――――――――――――――