Vba能够通过读取ini文件控制控件的属性(大小、位置、字体、颜色、样式……)吗?代码怎么写?

Vba能够通过读取ini文件控制控件的属性(大小、位置、字体、颜色、样式……)吗?代码怎么写?,第1张

VBA必须通过调用WN32 API来实现INI文件的读写,把控件属性及州拆其值在程序退出时写入INI文弊迹睁件,在程序加载时读取INI文件并设置租岁控件属性。

Private 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

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

'INI文件写

'参数:

'strSection:节名称

'strItem:项名称

'strValue:项的值

'strIniFile:INI文件

Private Function WriteIniFile(strSection,strItem,strValue,strIniFile) as Long

Dim lngWriteOk As Long

lngWriteOk = WritePrivateProfileString(strSection,strItem,strValue,strIniFile)

End Sub

'INI文件读

'参数:

'strSection:节名称

'strItem:项名称

'strDefValue:项的默认值

'strIniFile:INI文件

Private Function strReadIniFile(strSection,strItem,strDefValue,strIniFile) as String

Dim lngReadOk As Long

Dim strValue As String

Dim strReadValue As String

strValue=strDefValue

tmpReadValue = String(255, 0)

lngReadOk = GetPrivateProfileString(strSection, strItem, strDefValue, strReadValue, 256, strIniFile)

If lngReadOk then

strValue=trim(strReadValue)

end if

strReadIniFile=strValue

End Sub

功能实现函数如下

Private 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

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

'********************************************************************

'* Sub : ReadFromIni and WriteIntoIni

'*

'* Purpose:  读写INI文件

'*

'********************************************************************

Public Function ReadFromIni(ByVal IniFile As String, ByVal Section As String, ByVal Key As String, ByVal DefaultValue As String) As String

Dim strRtn As String

strRtn = Space(256)

Dim lngRtn As Long

lngRtn = GetPrivateProfileString(Section, Key, DefaultValue, strRtn, 255, IniFile)

If lngRtn >0 Then

strRtn = Trim(strRtn)

ReadFromIni = Mid(strRtn, 1, Len(strRtn) - 1)

Else

ReadFromIni = DefaultValue

End If

End Function

Public Sub WriteIntoIni(ByVal IniFile As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)

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

End Sub

函数使用测试如下

Sub Main()

Dim strIniFile As String

strIniFile = ActiveWorkbook.Path &"\example.ini"

Dim strSection As String

strSection = "Application"

Dim strKey As String

strKey = "Version"

Dim strValue As String

strValue = "1.0.30"

Call WriteIntoIni(strIniFile, strSection, strKey, strValue)

strValue = ReadFromIni(strIniFile, strSection, strKey, "")

MsgBox "Version = " &strValue, vbInformation

End Sub

运行上面的函数,程尺迹神序会在当前州孝目录生成一个example.ini文件,并向里面写入如下信息

[Application]

Version=1.0.30

然后再读取显示出来,如下

vbadiff用法一、题目:

要求编写一段代码,运用Dir函数返回一个文件夹的文件列表。

二、代码:

Sub 示例_1_12()

Dim wjm

wjm = Dir("C:\WINDOWS\弊伍WIN.ini")

MsgBox wjm

wjm = Dir("C:\WINDOWS\*.ini")

wjm = Dir

End Sub

三、代码详解

1、Sub 示例_1_12():宏程序的开始语句。宏名为示例_1_12。

2、Dim wjm :变量wjm声明为可变型数据类型。

3、wjm = Dir("C:\WINDOWS\WIN.ini") :

如果该文件存在则返回“WIN.INI”(在C:\Windows 文件夹中) ,把返回的文件名赋给变量wjm 。如果该文件不存在则wjm=””。

4、wjm = Dir("C:\WINDOWS\*.ini") :

返回带指定扩展名的文件名。如果超过一个 *.ini 文件存在,函数将返回按条件第租链或一个找到的文件名。

5、wjm = Dir :

若第二次调用 Dir 函数,但不带任何参数,则唤核函数将返回同一目录下的下一个 *.ini 文件


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12227460.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存