PDA

View Full Version : Automating Japanese furigana



Dr_Funkenste
11-28-2007, 11:28 PM
Hey guys, total VBA rookie here. (though I have ample experience in Ruby) What I want to do is very simple:
take a selection
lookup the phonetic guide text for words in the selection
apply them aboveThe 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:

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
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!

TonyJollans
11-29-2007, 05:27 AM
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.

Dr_Funkenste
11-29-2007, 08:38 AM
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:

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

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?

TonyJollans
11-29-2007, 10:39 AM
To use Excel, you must instantiate it, not just declare it, so



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


As for WHY Excel and not Word - I haven't a clue :(

Dr_Funkenste
11-29-2007, 11:49 AM
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:

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

TonyJollans
11-29-2007, 12:25 PM
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.

Dr_Funkenste
11-29-2007, 12:49 PM
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)

vbsnail
04-26-2009, 03:03 PM
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 !

Deadeye
03-14-2014, 03:45 PM
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.


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