Excel中VBA编程学习笔记(十三)--注册表操作

使用函数GetAllSettings(aooName,section)可以获取程序或者工程appName下的section区域的所有注册表项及值;

使用函数SaveSetting appName,section,key,value为注册表项设置值;

GetSetting(appName,section,key [,default])返回指定注册表项的值;

DeleteSetting appName,section [,key]删除注册表项,如果不带key参数则删除整个区域的注册表项。

【例1】

Sub test()

    Const appName = "ExcelBookA"

    Const section = "UsedInformation"

    Const key1 = "第一次打开时间"

    Const key2 = "上次打开时间"

    Const key3 = "打开总次数"

    Dim i%

    Dim arr As Variant

    Dim strInfo$

    arr = GetAllSettings(appName, section)

    If Not IsArray(arr) Then       ‘如果注册表项存在则返回一个数组

        SaveSetting appName, section, key1, Now()

        SaveSetting appName, section, key2, "没有记录"

        SaveSetting appName, section, key3, 0

       

    End If

   

    i = GetSetting(appName, section, key3)

    SaveSetting appName, section, key3, i + 1

    arr = GetAllSettings(appName, section)

    strInfo = "以下是当前工作薄的使用情况:" & vbCr

    For i = LBound(arr) To UBound(arr)

        strInfo = strInfo & vbCr & arr(i, 0) & ":" & arr(i, 1)

    Next

    strInfo = strInfo & vbCr & vbCr & "要清除记录信息吗?"

    If MsgBox(strInfo, vbDefaultButton1 + vbYesNo) = vbYes Then

        DeleteSetting appName

    Else

        SaveSetting appName, section, key2, Now

    End If

End Sub

打开注册表可以看到写入的注册表信息

猜你喜欢

转载自blog.csdn.net/huzhizhewudi/article/details/84556546
今日推荐