it技術:vb2clr
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| it技術:vb2clr [2026/01/30 17:53] – [テキストファイルの読込み] yajuadmin | it技術:vb2clr [2026/02/02 09:50] (現在) – [テキストファイルの読込み] yajuadmin | ||
|---|---|---|---|
| 行 240: | 行 240: | ||
| </ | </ | ||
| - | === ReadAllText | + | === StreamReader(Shift_JIS) |
| + | Shift_JISはエンコードが必要、UTF-8をエンコードで指定してしまうとBOM有無などの自動判別機能がなくなってしまうため、enc を引数として指定しないように修正が必要である。 | ||
| <code vb> | <code vb> | ||
| Public Function ReadFile() As Variant | Public Function ReadFile() As Variant | ||
| 行 260: | 行 262: | ||
| path = ThisWorkbook.path & " | path = ThisWorkbook.path & " | ||
| - | ' | + | ' |
| - | Dim text As Variant | + | Dim reader |
| - | | + | |
| | | ||
| - | | + | |
| + | Dim text As String | ||
| + | text = host.CLRInvokeMethod(reader, " | ||
| + | host.CLRInvokeMethod reader, " | ||
| + | |||
| + | ReadFile = text | ||
| Set host = Nothing | Set host = Nothing | ||
| + | Set filetype = Nothing | ||
| + | Set encType = Nothing | ||
| + | Set reader = Nothing | ||
| + | End Function | ||
| + | </ | ||
| + | === ReadAllText(Shift_JIS) === | ||
| + | Shift_JISはエンコードが必要、UTF-8をエンコードで指定してしまうとBOM有無などの自動判別機能がなくなってしまうため、enc を引数として指定しないように修正が必要である。 | ||
| + | <code vb> | ||
| + | Public Function ReadFile() As Variant | ||
| + | |||
| + | Dim host As New CLRHost | ||
| + | host.Initialize False | ||
| + | |||
| + | host.CLRLoadAssembly (" | ||
| + | |||
| + | Dim filetype As mscorlib.Type | ||
| + | Set filetype = host.CLRResolveType(" | ||
| + | Dim encType As mscorlib.Type | ||
| + | Set encType = host.CLRResolveType(" | ||
| + | Dim enc As mscorlib.Object | ||
| + | Set enc = host.CLRInvokeStaticMethod(encType, | ||
| + | | ||
| + | ' パス | ||
| + | Dim path As String | ||
| + | path = ThisWorkbook.path & " | ||
| + | |||
| + | ' File.ReadAllText(path, | ||
| + | Dim text As String | ||
| + | text = host.CLRInvokeStaticMethod(filetype, | ||
| + | | ||
| + | ReadFile = text | ||
| + | |||
| + | Set host = Nothing | ||
| + | Set filetype = Nothing | ||
| + | Set encType = Nothing | ||
| + | Set reader = Nothing | ||
| End Function | End Function | ||
| </ | </ | ||
it技術/vb2clr.1769763199.txt.gz · 最終更新: 2026/01/30 17:53 by yajuadmin
