细品RibbonX(40):技巧—在注册表中保存值和从注册表中获取值
资料整理来自于论坛
完整版下载地址:http://download.youkuaiyun.com/download/nodeman/10264659
Loading ...
Windows注册表是一个数据库,用于存储与计算机不同方面相关的设置,例如用户设置、应用程序设备、硬件设置,等等。
VBA提供了与注册表直接交互的方式,这不仅允许我们获取其它程序和硬件的信息,而且也能够使我们选择应用程序中的重要信息并将其存储在注册表中。
本文中,我们的目的是存储和获取关于UI的信息,我们将使用专门为VBA(和VB)配置而留出的部分。并且,我们不需要使用Windows APIs。
VBA提供了用于处理注册表的两个函数:GetSetting和SaveSetting,这两个函数仅能访问下面的注册表句柄键:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings
如下图所示,当切换按钮时,即使关闭了该工程,其状态值仍保存,因此,在重新打开该工程时,按钮的状态为关闭前的状态,不会因为关闭工程而使按钮的状态值丢失。
定义上图所示界面的XML代码如下:
<customUIxmlns="http://schemas.microsoft.com/office/2006/01/customui"
onLoad="rxIRibbonUI_onLoad">
<ribbonstartFromScratch="false">
<tabs>
<tabidMso="TabHome">
<groupid="rxgrp1"
insertBeforeMso="GroupClipboard"
label="Toggle Custom Tabs">
<toggleButtonid="rxtglCustomReview"
label="Toggle Custom Review Tab"
imageMso="ReviewAcceptChangeMenu"
size="large"
getPressed="rxtglCustomReview_getPressed"
onAction="rxtglShared_Click"/>
<separatorid="rxsep1"/>
<toggleButtonid="rxtglDataHandling"
label="Toggle Data Handling Tab"
imageMso="Consolidate"
size="large"
getPressed="rxtglDataHandling_getPressed"
onAction="rxtglShared_Click"/>
</group>
</tab>
<!-- THE CUSTOM REVIEW TAB UI STARTS HERE -->
<tabid="rxtabCustomReview"
getVisible="rxtabCustomReview_getVisible"
label="Custom Review"
keytip="K">
<groupidMso="GroupClipboard"/>
<groupidMso="GroupFont"/>
<groupid="rxgrpProofingComments"
label="Proofing and Comments">
<boxid="rxbox1"boxStyle="vertical">
<buttonidMso="Spelling"/>
<buttonidMso="Thesaurus"/>
<buttonidMso="TranslationPane"/>
</box>
<separatorid="rxsep2"/>
<boxid="rxbox2"boxStyle="vertical">
<buttonidMso="ReviewNewComment"/>
<buttonidMso="ReviewDeleteComment"
label="Delete Comment"/>
<toggleButtonidMso="ReviewShowAllComments"/>
</box>
</group>
<groupid="rxgrpChanges"
label="Worksheet related changes">
<boxid="rxbox"boxStyle="vertical">
<buttonidMso="ReviewHighlightChanges"/>
<buttonidMso="ReviewProtectAndShareWorkbook"/>
<buttonidMso="ReviewAllowUsersToEditRanges"/>
<buttonidMso="SheetProtect"/>
</box>
</group>
</tab>
<!-- END OF CUSTOM REVIEW TAB -->
<tabid="rxtabDataHandling"
getVisible="rxtabDataHandling_getVisible"
label="Data Handling"
keytip="Z">
</tab>
</tabs>
</ribbon>
</customUI>
代码包含了一些必须由VBA处理的关键回调。设置了getPressed属性,因为它返回从注册表中获取toggleButton的按下状态的回调,状态是true或false。
接下来,处理Click事件(该事件通过OnAction属性定义为共享的回调)。每次单击时,都会将按钮当前状态保存到注册表。
最后,getVisible属性根据toggleButton的状态决定是否选项卡可见。
本例中,将下面的两个VBA函数封装到自已的函数中,这样可以传递参数到自定义函数中,并且在一定程度上能够控制程序:
Function getRegistry(ByVal strKey As String) As Boolean
On Error Resume Next
getRegistry = GetSetting("AddInProject", "TabVisibility", strKey)
If Err > 0 Then getRegistry = False
End Function
Function saveRegistry(ByVal strKey As String, _
ByVal blnSetting As Boolean)
SaveSetting "AddInProject", "TabVisibility", strKey, blnSetting
End Function
因为如果键不存在的话,getRegistry函数将返回错误,所以需要添加错误处理以捕获错误。如果发生错误,那么知道该键不存在,getRegistry函数应该返回False。
回调代码如下:
Public gblnShowCustomReviewTab As Boolean
Public gblnShowCustomDataHandlingTab As Boolean
Public gblnCalledOnOpen As Boolean
Public grxIRibbonUI As IRibbonUI
Sub rxIRibbonUI_onLoad(ribbon As IRibbonUI)
Set grxIRibbonUI = ribbon
End Sub
Sub rxtglCustomReview_getPressed(control As IRibbonControl, ByRef returnedVal)
returnedVal = getRegistry("CustomReviewTab")
gblnShowCustomReviewTab = getRegistry("CustomReviewTab")
grxIRibbonUI.InvalidateControl ("rxtabCustomReview")
End Sub
Sub rxtglDataHandling_getPressed(control As IRibbonControl, ByRef returnedVal)
returnedVal = getRegistry("CustomDataHandlingTab")
gblnShowCustomDataHandlingTab = getRegistry("CustomDataHandlingTab")
grxIRibbonUI.InvalidateControl ("rxtabDataHandling")
End Sub
Sub rxtglShared_Click(control As IRibbonControl, pressed As Boolean)
Select Case control.ID
Case "rxtglCustomReview"
gblnShowCustomReviewTab = pressed
saveRegistry "CustomReviewTab", pressed
gblnCalledOnOpen = False
Case "rxtglDataHandling"
gblnShowCustomDataHandlingTab = pressed
gblnCalledOnOpen = False
saveRegistry "CustomDataHandlingTab", pressed
End Select
grxIRibbonUI.Invalidate
End Sub
Sub rxtabCustomReview_getVisible(control As IRibbonControl, ByRef returnedVal)
returnedVal = gblnShowCustomReviewTab
If Not gblnCalledOnOpen Then
If gblnShowCustomReviewTab = True Then Application.SendKeys ("%K{RETURN}")
End If
End Sub
Sub rxtabDataHandling_getVisible(control As IRibbonControl, ByRef returnedVal)
returnedVal = gblnShowCustomDataHandlingTab
If Not gblnCalledOnOpen Then
If gblnShowCustomDataHandlingTab = True Then Application.SendKeys ("%Z{RETURN}")
End If
End Sub
现在,就可以试试了!关闭工作簿后再打开,处理控件的设置将根据记录在注册表键中的值更新。
本文介绍如何使用VBA在Excel中与Windows注册表进行交互,实现保存和读取UI状态的功能。通过GetSetting和SaveSetting函数,可以轻松地在注册表中存储和检索应用程序的配置信息。
1414

被折叠的 条评论
为什么被折叠?



