ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > VBA读取、修改INI配置文件信息

VBA读取、修改INI配置文件信息

作者:绿色风 分类: 时间:2022-08-18 浏览:107
楼主
G_avin
Q:如何利用EXCEL读取文本文件中的指定信息?
A:利用API函数读取及修改配置文件信息
  1. 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

  2. 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


  3. Public Sub WriteToIni(ByVal Filename As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)
  4. Dim buff As String * 128
  5. buff = Value + Chr(0)
  6. WritePrivateProfileString Section, Key, buff, Filename
  7. End Sub

  8. Public Function ReadFromIni(ByVal Filename As String, ByVal Section As String, ByVal Key As String) As String
  9. Dim i As Long
  10. Dim buff As String * 128
  11. GetPrivateProfileString Section, Key, "", buff, 128, Filename
  12. i = InStr(buff, Chr(0))
  13. ReadFromIni = Trim(Left(buff, i - 1))
  14. End Function



  15. Private Sub Workbook_Open()
  16. If MsgBox("当前版本号为" & ReadFromIni(ThisWorkbook.Path & "\conf.ini", "version", "id") & Chr(13) _
  17. & "是否修改版本号为当前版本号加 1 ?", vbYesNo, "修改版本提示") = vbNo Then Exit Sub
  18. Call WriteToIni(ThisWorkbook.Path & "\conf.ini", "version", "id", ReadFromIni(ThisWorkbook.Path & "\conf.ini", "version", "id") + 1)
  19. MsgBox "修改成功,当前版本号为" & ReadFromIni(ThisWorkbook.Path & "\conf.ini", "version", "id")
  20. End Sub

配置信息.rar
2楼
xtanuihazfh
G妹好东西不少嘛

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap