Skip to content

Instantly share code, notes, and snippets.

@gmodeblog
Last active August 29, 2015 14:22
Show Gist options
  • Save gmodeblog/f5af7ccaa64977a643a6 to your computer and use it in GitHub Desktop.
Save gmodeblog/f5af7ccaa64977a643a6 to your computer and use it in GitHub Desktop.
'--------------------------------------------------------------------
' 関数名 : GetJTDText
' 機能 : 指定のpathのjtdファイル(一太郎)からテキストを抽出する
' 引数 : path = ファイル絶対パス
' 戻り値 : ファイル内テキスト<String Array>
'--------------------------------------------------------------------
Function GetJTDText(path As String) As String()
Dim jtd_app As Object
Dim jtd_doc As Object
Dim jtd_lib As Object
Dim jtd_sentences As Object
' 一太郎アプリケーションの起動
Set jtd_app = CreateObject("JXW.application")
' 文書を開く
Set jtd_doc = jtd_app.Documents
jtd_doc.Open (path)
' 文書内に対する操作のためのライブラリ取得
Set jtd_lib = jtd_app.TaroLibrary
Dim res() As String
Dim res_index As Integer
res_index = 0
' シートが複数に分かれている場合を考慮して、全てのシートからテキスト抽出
Dim sheet_num As Integer
sheet_num = jtd_lib.GetSheetCount
Dim i As Integer
For i = 1 To sheet_num
' シートのカレントを順に移していく
jtd_lib.ChangeCurrentSheet (i)
' 文書内のテキストを全て選択状態にする
jtd_lib.SelectAll (1)
On Error GoTo myError
' 選択状態のテキストを取得
Set temp = jtd_lib.GetString ' string配列ではなくCollectionが返ってくる点に注意
' Collectionであるtempを使って全て結果に格納
Dim temp_index As Integer
For temp_index = 1 To temp.Count()
ReDim Preserve res(res_index)
res(res_index) = temp.Item(temp_index)
res_index = res_index + 1
Next
myError:
' シートに何も書かれていない場合、jtd_lib.GetStringでエラーになる模様……
Next
GetJTDText = res
' 解放処理
jtd_doc.Close ' 文書を閉じる
jtd_app.Quit ' アプリケーションを閉じる
Set jtd_app = Nothing
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment