楼主 G_avin |
Q:如何利用EXCEL读取文本文件中的指定信息? A:利用API函数读取及修改配置文件信息
- Public 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
- Public 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 Sub WriteToIni(ByVal Filename As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)
- Dim buff As String * 128
- buff = Value + Chr(0)
- WritePrivateProfileString Section, Key, buff, Filename
- End Sub
- Public Function ReadFromIni(ByVal Filename As String, ByVal Section As String, ByVal Key As String) As String
- Dim i As Long
- Dim buff As String * 128
- GetPrivateProfileString Section, Key, "", buff, 128, Filename
- i = InStr(buff, Chr(0))
- ReadFromIni = Trim(Left(buff, i - 1))
- End Function
- Private Sub Workbook_Open()
- If MsgBox("当前版本号为" & ReadFromIni(ThisWorkbook.Path & "\conf.ini", "version", "id") & Chr(13) _
- & "是否修改版本号为当前版本号加 1 ?", vbYesNo, "修改版本提示") = vbNo Then Exit Sub
- Call WriteToIni(ThisWorkbook.Path & "\conf.ini", "version", "id", ReadFromIni(ThisWorkbook.Path & "\conf.ini", "version", "id") + 1)
- MsgBox "修改成功,当前版本号为" & ReadFromIni(ThisWorkbook.Path & "\conf.ini", "version", "id")
- End Sub
配置信息.rar |