加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

VBA操作INI配置文件源代码

(2018-05-24 18:50:43)
标签:

excel

vba

代码

word

分类: VBA专区

一般程序都有一些配置或者设置选项,而这些参数一般是保存为INI配置文件,如何使用VBA操作这些INI格式的配置文件是比较常用的,这里提供一个操作INI文件的源代码:

 
01.Option Explicit
02.'================================
03.' VBA操作INI配置文件
04.'
05.'
06.'================================
07.Private Declare Function _
08.GetPrivateProfileString Lib "kernel32" _
09. Alias "GetPrivateProfileStringA" _
10. (ByVal lpApplicationName As String, _
11. ByVal lpKeyName As Any, _
12. ByVal lpDefault As String, _
13. ByVal lpReturnedString As String, _
14. ByVal nSize As Long, _
15. ByVal lpFileName As String) As Long
16.Private Declare Function _
17. WritePrivateProfileString Lib "kernel32" _
18. Alias "WritePrivateProfileStringA" _
19. (ByVal lpApplicationName As String, _
20. ByVal lpKeyName As Any, _
21. ByVal lpString As Any, _
22. ByVal lpFileName As String) As Long
23. 
24.Public Function ReadFromIni( _
25. ByVal IniFile As String, _
26. ByVal Section As String, _
27. ByVal Key As String, _
28. ByVal DefaultValue As String) As String
29.     
30.    Dim strRtn As String
31.    strRtn = Space(256)
32.     
33.    Dim lngRtn As Long
34.    lngRtn = GetPrivateProfileString(Section, Key, DefaultValue, strRtn, 255, IniFile)
35.     
36.    If lngRtn > 0 Then
37.        strRtn = Trim(strRtn)
38.        ReadFromIni = Mid(strRtn, 1, Len(strRtn) - 1)
39.    Else
40.        ReadFromIni = DefaultValue
41.    End If
42. 
43.End Function
44. 
45.Public Sub WriteIntoIni( _
46. ByVal IniFile As String, _
47. ByVal Section As String, _
48. ByVal Key As String, _
49. ByVal Value As String)
50.     
51.    Dim lngRtn As Long
52.    lngRtn = WritePrivateProfileString(Section, Key, Value, IniFile)
53.     
54.    If lngRtn > 0 Then
55.    Else
56.        Call Err.Raise(-1, "IniFileUtil.WriteIntoIni", "Failed to write")
57.    End If
58. 
59.End Sub

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有