Consulting

Results 1 to 9 of 9

Thread: Automating Japanese furigana

  1. #1

    Automating Japanese furigana

    Hey guys, total VBA rookie here. (though I have ample experience in Ruby) What I want to do is very simple:
    1. take a selection
    2. lookup the phonetic guide text for words in the selection
    3. apply them above
    The PhoneticGuide method has the following arguments:

    Range.PhoneticGuide(text, alignment, raise, font, etc)

    However, the text is mandatory, and I can't find a way to lookup the appropriate text. One would have hoped that leaving text blank would automatically use the default phonetic guide, but alas no. Here is what I have:

    [vba]Sub furigana()
    '
    ' Furigana Macro
    '
    '
    Dim text As String
    Dim furi As String

    With Selection
    text = StrConv(Selection, vbHiragana)
    furi = Application.GetPhonetic(text)

    .Range.PhoneticGuide text:=furi, Alignment:= _
    wdPhoneticGuideAlignmentOneTwoOne, Raise:=15, FontSize:=8, FontName _
    :="YOzFont04"
    End With
    End Sub[/vba]
    How can I parse the selection to find the appropriate text argument? For some reason it poops out with 'method or data member not found." Thanks!
    Last edited by Dr_Funkenste; 11-29-2007 at 12:04 AM.

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Two points here.

    'method or data member not found' is because the Word Application, as far as I know, does not have a GetPhonetic method (although Excel has).

    To get the text of the selection, use Selection.Text or, inside your With block, just .Text (preceded by a period) instead of just Text.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    Thanks for your help. I can grab the text and display it as a phonteic guide, but still can't use GetPhonetic to nab the correct guides. Here's what I have so far:

    [VBA]Sub furigana()
    '
    ' Furigana Macro
    '
    With Selection

    Dim furi As String
    Dim xlApp As Excel.Application

    furi = Selection.text
    'furi = xlApp.GetPhonetic(furi)

    .Range.PhoneticGuide text:=furi, Alignment:= _
    wdPhoneticGuideAlignmentOneTwoOne, Raise:=15, FontSize:=8, FontName _
    :="YOzFont04"
    End With
    End Sub[/VBA]

    If I uncomment the xlApp line, the macro fails with "Object variable or block variable not set" even though I have enabled the Excel Object libraries. Why would EXCEL have GetPhonetic and WORD wouldn't? Shouldn't it be the other way around? Anyways, how can I access this method correctly?

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    To use Excel, you must instantiate it, not just declare it, so


    [vba]
    Sub furigana()
    '
    ' Furigana Macro
    '
    With Selection

    Dim furi As String
    Dim xlApp As Excel.Application

    Set xlApp = CreateObject(" Excel.Application")

    furi = xlApp.GetPhonetic(.Text)

    .Range.PhoneticGuide text:=furi, Alignment:= _
    wdPhoneticGuideAlignmentOneTwoOne, Raise:=15, FontSize:=8, FontName _
    :="YOzFont04"

    Set xlApp = Nothing

    End With
    End Sub
    [/vba]

    As for WHY Excel and not Word - I haven't a clue
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    That works but returns an empty string for furi. My attempts to futz with the encoding properties of furi or the selected text (vbUnicode, etc.) have yielded nothing. Even when I specifiy a string, such as:

    [vba]furi = xlApp.GetPhonetic("???") ' ??? because the VB editor doesn't support Unicode![/vba]
    It returns nothing. The documentation for this method is terrible.

  6. #6
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by Dr_Funkenste
    The documentation for this method is terrible.
    You can omit the middle three words of that sentence.

    Silly question, but you do have Japanese language support installed, don't you?

    I know nothing of Japanese so can't easily test this but when I try, I also get an empty string.

    I will post back if I find anything out, otherwise you may need more specialist help - assuming you can speak Japanese you might be able to find a Japanese forum where people have experience of it - I'm not sure what else to suggest.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  7. #7
    I do have Japanese support installed - I sniffed around that theory as well. Thanks for all your help. Makes you envious of php.net's commented documentation, doesn't it? (ruby is getting better...but not here yet)

  8. #8
    VBAX Newbie
    Joined
    Apr 2009
    Posts
    1
    Location

    You could try ChrW()

    VB Editor does not support Unicode, for sure !!
    But you can generate ant needed Unicode using ChrW(long)

    for instance ChrW(12353) will insert a nice Hiragana.

    Hope it helps, I am a little bit late !

  9. #9
    I found this thread very helpful and was the only decent result that google threw out to me, so sorry for dredging up an old thread.
    I found TonyJollans code to be almost correct.

    Note I am using English MS Word, Excel 2010 with Japanese Language pack. If you don't have the Language pack it wouldn't work! You need to set the default editing Language to Japanese within the Office 2010 Language Preferences. The main fix I did was GetPhonetic returns Katakana, I couldn't figure out why so I used strConv to convert back to hiragana with the Japan LocaleID.

    [vba]
    Option Explicit
    Option Base 0
    Private Const m_sMODULE_NAME As String = "basFixPhonetic"

    Public Function g_sFixPhonetic() As String
    '########################################################################## ####
    ' g_sFixPhonetic (PROCEDURE)
    ' PARAMETERS
    '
    ' RETURN
    '
    ' DESCRIPTION
    ' Fixes the phonetic Japanese Text to Arial Unicode MS font size 6
    ' For some reason you must clear any existing furigana otherwise you get
    ' crazy characters. I think its to do with MSWord saving furgana in this format:
    ' Kanji(Hiragana)
    ' So there is a message box asking you if you want to clear existing furigana
    ' IMPORTANT:
    ' I use a loop iC1 as I need to modify 450 kanji's so please modify as desired
    '########################################################################## ####
    ' Declare Constants
    Const sPROCEDURE_NAME As String = "g_sFixPhonetic"
    On Error GoTo ERROR_TRAP

    ' Declare Variables
    Dim iC1 As Integer ' General Counter
    Dim xlApp As Excel.Application ' Excel
    Dim iClear As Integer ' Clear Furigana

    ' Initialise
    Set xlApp = CreateObject("Excel.Application")

    iClear = MsgBox("Click Yes to Clear, No to add Furigana", vbYesNo, "Add Furigana")
    ' Run for Words
    For iC1 = 0 To 450
    With Selection

    If iClear = vbYes Then
    ' Clear existing Furigana
    .Range.PhoneticGuide Text:=""
    .MoveRight Unit:=wdCharacter
    Else
    Select Case Selection.Range.Kana
    Case wdKanaHiragana, wdKanaKatakana
    ' Do Nothing for now
    Case wdUndefined
    ' GetPhonetic of Kanji via Excel returns Katakana,
    ' Convert Katakana to Hiragana, use LocaleID 1041 (Japan)
    .Range.PhoneticGuide Text:=StrConv(xlApp.GetPhonetic(.Text), vbHiragana, 1041), _
    Alignment:=wdPhoneticGuideAlignmentOneTwoOne, _
    Raise:=13, FontSize:=5, _
    FontName:="Arial Unicode MS"
    End Select

    .MoveRight Unit:=wdCharacter
    End If
    End With
    Next iC1

    CLEAN_UP_EXIT:
    On Error GoTo 0
    Set xlApp = Nothing
    DoEvents
    Exit Function

    ERROR_TRAP:
    If Err.Number <> 0 Then
    Call MsgBox(m_sMODULE_NAME & "." & sPROCEDURE_NAME, True, _
    Err.Number, Err.Description)

    ' Call g_ErrorControl(m_sMODULE_NAME & "." & sPROCEDURE_NAME, True, _
    Err.Number, Err.Description)
    End If
    Err.Clear
    Resume CLEAN_UP_EXIT
    End Function

    [/vba]
    Last edited by Deadeye; 03-14-2014 at 04:17 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •