it技術:vb2clr
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| it技術:vb2clr [2026/01/30 17:51] – [テキストファイルの書込み] yajuadmin | it技術:vb2clr [2026/02/02 09:50] (現在) – [テキストファイルの読込み] yajuadmin | ||
|---|---|---|---|
| 行 202: | 行 202: | ||
| ==== テキストファイルの読込み ==== | ==== テキストファイルの読込み ==== | ||
| + | === StreamReader === | ||
| <code vb> | <code vb> | ||
| Sub filetextReader() | Sub filetextReader() | ||
| 行 239: | 行 240: | ||
| </ | </ | ||
| + | === StreamReader(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 & " | ||
| + | |||
| + | ' System.IO.StreamReader(path, | ||
| + | Dim reader As mscorlib.Object | ||
| + | Set reader = host.CLRCreateObjectWithParams(" | ||
| + | | ||
| + | ' 全文 | ||
| + | Dim text As String | ||
| + | text = host.CLRInvokeMethod(reader, | ||
| + | host.CLRInvokeMethod reader, " | ||
| + | | ||
| + | ReadFile = text | ||
| + | |||
| + | 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 | ||
| + | </ | ||
| ==== emfファイルをpngファイルに変換 ==== | ==== emfファイルをpngファイルに変換 ==== | ||
| <code vb> | <code vb> | ||
it技術/vb2clr.1769763112.txt.gz · 最終更新: 2026/01/30 17:51 by yajuadmin
