自分用です。
参考にさせていただいたサイトさんです

 

 

Sub ルビをカッコ付きに変換_ExcelVBAからWord操作()

    Dim wdApp As Object
    Dim wdDoc As Object
    Dim myField As Object
    Dim myCode As String

    ' Wordアプリケーションを取得または起動
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If wdApp Is Nothing Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    wdApp.Visible = True

    ' Word文書を開く(ファイルパスは適宜変更)
    Dim filepath As String
    filepath = ThisWorkbook.Path & "\ルビテスト.docx" ' 同じフォルダにある想定
    Set wdDoc = wdApp.Documents.Open(filepath)

    ' フィールドをルビ表記に変換
    For Each myField In wdDoc.Fields
        myCode = myField.Code.Text
        myField.Select
        wdApp.Selection.Range.Text = ConvertRuby(myCode)
    Next

    MsgBox "変換完了"

End Sub

Function ConvertRuby(codetext As String) As String
    ' 「EQ \* jc2 \* ruby(るび,漢字)」 → 「漢字(るび)」形式に変換
    Dim i As Long, pos As Long
    Dim rubybase As String, rubytext As String, t As String
    pos = 0
    rubybase = ""
    rubytext = ""

    For i = 1 To Len(codetext)
        t = Mid(codetext, i, 1)
        If t = "(" Or t = ")" Or t = "," Then
            pos = pos + 1
        ElseIf pos = 2 Then
            rubytext = rubytext & t
        ElseIf pos = 4 Then
            rubybase = rubybase & t
        End If
    Next i

    ConvertRuby = rubybase & "(" & rubytext & ")"
End Function

 

おすすめの記事