VBA操作INI配置文件源代码
(2018-05-24 18:50:43)
标签:
excelvba代码word |
分类: VBA专区 |
一般程序都有一些配置或者设置选项,而这些参数一般是保存为INI配置文件,如何使用VBA操作这些INI格式的配置文件是比较常用的,这里提供一个操作INI文件的源代码:
01.Option
Explicit02.'================================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
Long16.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
Long23. 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
String29. 30. Dim
strRtn As
String31. strRtn
= Space(256)32. 33. Dim
lngRtn As
Long34. lngRtn
= GetPrivateProfileString(Section, Key, DefaultValue, strRtn, 255,
IniFile)35. 36. If
lngRtn > 0 Then37. strRtn
= Trim(strRtn)38. ReadFromIni
= Mid(strRtn, 1, Len(strRtn) - 1)39. Else40. ReadFromIni
= DefaultValue41. End
If42. 43.End
Function44. 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
Long52. lngRtn
= WritePrivateProfileString(Section, Key, Value,
IniFile) 53. 54. If
lngRtn > 0 Then55. Else56. Call
Err.Raise(-1,
"IniFileUtil.WriteIntoIni", "Failed
to write")57. End
If58. 59.End
Sub前一篇:Excel中高亮显示重复值

加载中…