Excel VBA側の設定は過去記事をご覧ください。
https://qiita.com/torisan_/items/226c319249e5d4fdc79e
ObjectScript側の準備
クラスNewClass1に、次のコードを追加する。
fileread()
ClassMethod fileread(filename As %String) As %String
{
#dim line as %String
#dim stream
#dim sc
Set stream=##class(%Stream.FileCharacter).%New()
Set sc=stream.LinkToFile(filename)
While 'stream.AtEnd {
Set line=stream.Read()
; Process the chunk here
WRITE line
}
Quit line
}
変数宣言は、先頭を1文字以上の空白にして、#dimと表現する。
%Stream.FileChalacterがCache'に用意されたクラス。
streamに、LinkToFile関数で指定したファイルが結び付けられて開かれる。
stream.Read()で、32000バイトのデータを読み取る。1行ずつ読み取りたいときはstream.ReadLine()でOK。
戻り値はQuitで記述する。
VBAの準備
以下のサブルーチン(TestCacheEntry2)をExcel VBAのModule1に記述する。
Module1.TestCacheEntry2()
Sub TestCacheEntry2()
TestCacheConnection '以前の記事での開き方を参照
Dim Patient As CacheActiveX.ObjInstance
Set Patient = factory.New("User.Main.NewClass1")
Dim s1 As String
s1 = Patient.fileread("C:\data\test.txt")
Patient.s = s1
MsgBox (s1)
Patient.sys_Save
Patient.sys_Close
Set Patient = Nothing
End Sub
画面イメージは次の通り。この例ではVBAでの関数の呼び出し方を示すために、少し回りくどくObjectScriptでファイルを読み込んでCache'にsys_Saveしていますが、ファイル操作だけならObjectScriptの中で完結すればOKです。なおActiveXだと、.NETのように再度コードを読み込む必要がない(!)ので、コード開発はとても快適です。画面を伴うDBのプロトタイプ開発ではExcelVBA-ActiveX-Cache'ObjectScriptは最高の環境かもしれません。