Search code examples
vbaexcelime

Programming a Japanese keyboard "Henkan" 「変換」 button


I am programming VBA(7.0) in Excel 2010 and am trying to make a macro that will change a string variable (containing Kanji) into it's Hiragana constituents. As far as I could tell there are no VBA-specific methods to do this. Hence, I assume the way to go about it, is to attempt to emulate the [henkan] 「変換」 button on a Japanese Keyboard.

For those not accustomed to Japanese Keyboards, the 「変換」 button is used to change constituent Hiragana as it is written, into a Kanji compound (likewise highlighting existing text and pressing it will offer options to change it to other Kanji as well as it's constituent Hiragana or Katakana). Pressing the button will bring up a list from the IME which lists possible selections for your entry.

I gathered from here and here that the scancode of the button in question is 79.

Putting 1 + 1 together (getting 3) and trying the code below didn't yield any results.

Private Sub test_Click()
  Sheets("Main").Range("A1").Select   '<--- A1 contains a Kanji Compound
  Application.SendKeys (79)   '<--- Both (79) and ("79") were tried
End Sub

I noticed there are the following VBA functions (which may end up helping with an end result) but they don't seem to help the situation.

  • StrConv (Can convert Hiragana <> Katakana, but not Kanji)
  • Phonetics
    • .Add (Can add the reading of the Kanji (as Furigana) however this requires user input and is not automatic).
    • .CharacterType (Returns or sets the phonetics type; Hiragana, Katakana etc.)
  • IME (Largely used to set input rules)

A co-worker suggested that I may need to look at the IME API to see firstly, if I can access it (permissions with the API), and secondly if it will let me know the way of accessing the key. However my experience with API'S (especially IME) is nil to none.

Is there a VBA-specific way of emulating the Kanji -> Hiragana Process (only a one way Kanji to Hiragana conversion is required)?

Failing that, is there a process which could sendkey the「変換」 button and select the Hiragana option?


Solution

  • Although I don't like to answer my own questions, I have found a solution.

    In order to get the Excel [PHONETIC] function to work properly, [Japanese Editing Tools] must be installed. These are found in either a Language Pack (which for Excel 2010 is USD$25 each) or by installing from a Japanese Language Microsoft Office [2010] installation disk - Just installing the tools to an English Office edition is possible (thus mitigating the need for a full Japanese edition installation).

    Failure to install the [Japanese Editing Tools] will mean that the [PHONETIC] function will ALWAYS contain a blank string (unless syntactically incorrect) for both the in-cell and VBA functions.

    The full steps are as follows.

    1. Install Microsoft Office Japanese Editing Tools for your edition of Microsoft Office (MO2010 > Install JET MO2010 edition)

    2. Use the [PHONETIC] Function.

      • This can be used as an in-cell function with the format

      =Phonetic(reference)

      Where [reference] is the name of a cell ie.[A2] - You can NOT directly input a string into this in-cell function).

      • In VBA the function is Application.GetPhonetic(text)

      Where [text] is either directly inputted, a string variable or a reference to a cell with a string.

    3. Assuming automated Hiragana is required as Kanji is entered into a Userform Textbox.

    Kanji Input TextBox => [KanjiBox], Hiragana Reading TextBox => [YomiBox]

    Private Sub KanjiBox_Change()
        'Finds last entry in Range (for this example, range is in the [B] column)
        lastEntry = Sheets("Entries").Range("B1048576").End(xlUp).Row
        'Finds if entry already exists in range, if it does;_
         launches MsgBox Warning and clears all fields. Range starts from Row 7.
        For entryRow = 7 To lastEntry
            If KanjiBox = Sheets("Entries").Range("B" & entryRow) Then
                MsgBox "There is already an entry for " & KanjiBox
                KanjiBox = ""
                YomiBox = ""
                Exit For
            End If
        Next entryRow
        'This sets the [YomiBox] as a Hiragana reading of the Kanji_
         (Uses StrConv to change the Phonetic result from Katakana to Hiragana)
        YomiBox = StrConv(Application.GetPhonetic(KanjiBox), vbHiragana)
        'The following fixes leftover reading text when backspacing
        If KanjiBox = "" Then
            YomiBox = ""
        End If
    End Sub
    

    Using this method, 「変換」 key emulation was not required.

    Any way of Emulating the 「変換」 key as a key press event, without the use of third party applications is still, as yet, unknown by me.