|
定义为函数 日后调用更方便
- Rem 当VBA7和Win64都是True时(只有64的Excel才是这种情况),使用第一条Declare语句。在其他版本中,使用第二条Declare语句
- Rem =====================================================================================================================
- #If Vba7 And Win64 Then
- Private Declare PtrSafe 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 PtrSafe Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA"(ByVal lpApplicationName As String,ByVal lpKeyName AsAny,ByVal lpString As Any,ByVal lpFileName As String) As Long
- #Else
- Rem 32位的: 注意64位系统,安装的Excel可能是32位的Excel VBA
- Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVallpDefault 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 Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByRef lpReturnedString() As Byte, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
-
- #End If
- Sub Main()
- Dim StrIniFile, StrSection, StrKey, StrValue As String
-
- StrIniFile = ThisWorkbook.Path & "\canshu.ini"
- StrSection = "信息设置"
- StrKey = "用户名"
- StrValue = "特朗普"
-
- Dim L As Long
- L = WriteIni(StrIniFile, StrSection, StrKey, StrValue)
-
- StrValue = ReadIni(StrIniFile, StrSection, StrKey, "")
- MsgBox "用户名= " & StrValue, vbInformation
- End Sub
- Rem ********************************************************************
- Rem ReadIni 读INI文件,段落中某关键字的值
- Rem 参数:IniFile Ini文件路径
- Rem 参数:Section ini文件中的段落 [信息设置]
- Rem 参数:Key ini文件的关键字 用户名=特朗普
- Rem 参数:Value ini文件中的值,读时取空值
- Rem StrValue = ReadIni(StrIniFile, StrSection, StrKey, "")
- Rem ********************************************************************
- Public Function ReadIni(ByVal IniFile As String, ByVal Section As String, ByVal Key As String, ByVal Value As String) As String
- Dim strRtn As String '//Dim strRtn As String * 5000
- strRtn = Space(5000)
- Dim lngRtn As Long
- lngRtn = GetPrivateProfileString(Section, Key, Value, strRtn, 4999, IniFile)
- If lngRtn > 0 Then
- strRtn = Trim(strRtn)
- ReadIni = Mid(strRtn, 1, Len(strRtn) - 1)
- Else
- ReadIni = DefaultValue
- End If
- End Function
- Rem ********************************************************************
- Rem WriteIni 写INI文件,段落中某关键字的值
- Rem 参数:IniFile Ini文件路径
- Rem 参数:Section ini文件中的段落 [信息设置]
- Rem 参数:Key ini文件的关键字 用户名=特朗普
- Rem 参数:Value ini文件中的值,输入时取空值
- Rem Dim L As Long
- Rem L = WriteIni(StrIniFile, StrSection, StrKey, StrValue)
- Rem ********************************************************************
- Public Function WriteIni(ByVal IniFile As String, ByVal Section As String, ByVal Key As String, ByVal Value As String) As Long
- Dim lngRtn As Long
- lngRtn = WritePrivateProfileString(Section, Key, Value, IniFile)
- If lngRtn > 0 Then
- Else
- Call Err.Raise(-1, "IniFileUtil.WriteIntoIni", "Failed to write")
- End If
- WriteIni = lngRtn
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|