VBA中是否有可用于读写INI文件的方法?我知道我可以使用;
Open "C:\test.ini" For Input As #1
...并解析数据。相反,我试图看看哪些工具已经可用。
我知道在C#你可以做......
using INI;
INIFile ini = new INIFile("C:\test.ini");
VBA是否有等价物?
我在MS Access 2003 VBA中尝试此操作。
答案 0 :(得分:9)
以下是我们使用的一些代码段,它应该可以帮助您获得想法。这些例程使用API调用。包含两个函数来读取/写入ini文件中特定部分的字符串设置。
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
Public Function IniFileName() As String
IniFileName = "c:\[yourpath here]\settings.ini"
End Function
Private Function ReadIniFileString(ByVal Sect As String, ByVal Keyname As String) As String
Dim Worked As Long
Dim RetStr As String * 128
Dim StrSize As Long
iNoOfCharInIni = 0
sIniString = ""
If Sect = "" Or Keyname = "" Then
MsgBox "Section Or Key To Read Not Specified !!!", vbExclamation, "INI"
Else
sProfileString = ""
RetStr = Space(128)
StrSize = Len(RetStr)
Worked = GetPrivateProfileString(Sect, Keyname, "", RetStr, StrSize, IniFileName)
If Worked Then
iNoOfCharInIni = Worked
sIniString = Left$(RetStr, Worked)
End If
End If
ReadIniFileString = sIniString
End Function
Private Function WriteIniFileString(ByVal Sect As String, ByVal Keyname As String, ByVal Wstr As String) As String
Dim Worked As Long
iNoOfCharInIni = 0
sIniString = ""
If Sect = "" Or Keyname = "" Then
MsgBox "Section Or Key To Write Not Specified !!!", vbExclamation, "INI"
Else
Worked = WritePrivateProfileString(Sect, Keyname, Wstr, IniFileName)
If Worked Then
iNoOfCharInIni = Worked
sIniString = Wstr
End If
WriteIniFileString = sIniString
End If
End Function
答案 1 :(得分:4)
这并不令人愉快,但您可以使用Windows API。 Here's a link和another from MS。
答案 2 :(得分:1)
答案 3 :(得分:1)
Karl Peterson's kpini几乎包含了您可能需要的所有内容:API声明,Cinifile类,类似的东西。我会从那开始,然后把它变成味道,这不应该花很长时间。