代码最开头加入这个:
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
Private Const mc_strIniFileName As String = “MyIni.ini”
然后添加这个:
Public Function GetIni(appName As String, keyName As String) As String
Dim strDefault As String
Dim lngBuffLen As Long
Dim strResu As String
Dim x As Long
Dim strIniFile As String
If Right(App.Path, 1) = "\" Then
strIniFile = App.Path & mc_strIniFileName
Else
strIniFile = App.Path & "\" & mc_strIniFileName
End If
strResu = String(1025, vbNullChar): lngBuffLen = 1025
strDefault = ""
x = GetPrivateProfileString(appName, keyName, strDefault, strResu, lngBuffLen, strIniFile)
Debug.Print x
Debug.Print strResu
GetIni = Left(strResu, x)
End Function
Public Sub WriteIni(appName As String, keyName As String, valueNew As String)
Dim x As Long
Dim strIniFile As String
If Right(App.Path, 1) = "\" Then
strIniFile = App.Path & mc_strIniFileName
Else
strIniFile = App.Path & "\" & mc_strIniFileName
End If
x = WritePrivateProfileString(appName, keyName, valueNew, strIniFile)
Debug.Print x
End Sub
然后一个button测试下:
Private Sub Command3_Click()
Dim strRet As String
strRet = GetIni(“AAA”, “BBB”)
MsgBox "读出为:" & strRet
End Sub