gpt4 book ai didi

vba - 读写 .ini 文件的推荐方式

转载 作者:行者123 更新时间:2023-12-04 05:44:41 26 4
gpt4 key购买 nike

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 中尝试此操作。

最佳答案

这是我们使用的一些代码片段,它应该可以帮助您理解。这些例程使用 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

关于vba - 读写 .ini 文件的推荐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/660138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com