
INIファイルを扱うにはWin32APIを使います。
まずは、下記のコードをどこかに記述しておいてください。
1 2 | Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( ByVal lpApplicationName As String , ByVal lpKeyName As Any, ByVal lpDefault As String , ByVal lpReturnedString As String , ByVal nSize As Long , ByVal lpFileName As String ) As Long Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( ByVal lpApplicationName As String , ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String ) As Long |
読み込み

たとえば、このようなINIファイルのデータを読み込むには次のように記述します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | Sub GetValue() Dim Value As String * 255 Dim section As String Dim fileName As String Dim r As Integer Dim g As Integer Dim b As Integer section = "CellColor" fileName = "C:\work\Excel\conf.ini" 'R Call GetPrivateProfileString(section, "R" , "ERROR" , Value, Len(Value), fileName) r = Left(Value, InStr(1, Value, vbNullChar) - 1) 'G Call GetPrivateProfileString(section, "G" , "ERROR" , Value, Len(Value), fileName) g = Left(Value, InStr(1, Value, vbNullChar) - 1) 'B Call GetPrivateProfileString(section, "B" , "ERROR" , Value, Len(Value), fileName) b = Left(Value, InStr(1, Value, vbNullChar) - 1) Range( "B2" ).Interior.Color = RGB(r, g, b) End Sub |
また、取得した値には後ろにNull文字が含まれていますので、Left関数で取り除いています。
実行結果

書き込み
INIファイルに書き込む例です。1 2 3 4 5 6 7 8 9 10 11 12 13 | Sub SetValue() Dim section As String Dim fileName As String section = "CellColor" fileName = "C:\work\Excel\conf.ini" Call WritePrivateProfileString(section, "R" , "255" , fileName) Call WritePrivateProfileString(section, "G" , "20" , fileName) Call WritePrivateProfileString(section, "B" , "147" , fileName) End Sub |
実行結果

スポンサーリンク