|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
没想到过了这么多年,经好友推荐,找到到了宝藏资源,谢谢。
结合前面各个前辈的回复,我做个终结版汇总:
楼主原版如下:
- Option Explicit
- '增加32/64位兼容API声明
- #If VBA7 And Win64 Then
- Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long)
- #Else
- Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long)
- #End If
- Public MyRibbon As IRibbonUI
- Sub IRibbonUI_onLoad(ribbon As IRibbonUI) '原版楼主估计CustomeUI少写了一个e。这里修正补上。
- SaveSetting "CustomeUI", ThisWorkbook.Name, "RibbonPointer", ObjPtr(ribbon)
- '注册表保存方案;容易受权限影响,如果用户没有注册表操作权限就要保存失败。
- Set MyRibbon = ribbon
- End Sub
- Function GetRibbon(ByVal lRibbonPointer As LongPtr) As Object '原始版本 将指针lRibbonPointer 定义为LongPtr,提高兼容性
- If Not Rib Is Nothing Then
- Set GetRibbon = Rib
- Exit Function
- End If
-
- Dim objRibbon As Object
- Dim lngRibPointer As LongPtr '32位用long,64位用longlong,大神建议用Longptr,兼容32/64位。
-
- lngRibPointer = GetSetting("CustomeUI", ThisWorkbook.Name, "RibbonPointer")
- CopyMemory objRibbon, lngRibPointer, LenB(lngRibPointer)
-
- Set GetRibbon = objRibbon
- Set objRibbon = Nothing
-
- End Function
复制代码
大神修改精简版的版本:
- Option Explicit
- #If VBA7 And Win64 Then
- Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long)
- #Else
- Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long)
- #End If
- Public MyRibbon As IRibbonUI
- Sub IRibbonUI_onLoad(ribbon As IRibbonUI)
- SaveSetting "CustomeUI", ThisWorkbook.Name, "RibbonPointer", ObjPtr(ribbon)
- '注册表保存方案;容易受权限影响,如果用户没有注册表操作权限就要保存失败。
- Set MyRibbon = ribbon
- End Sub
- Function GetRibbon_V2() As Object '修改版V2 ''指针已存在注册表中,无需另外传递,其他多余部分删去,根据某位贴友建议将long修改为longptr
-
- Dim objRibbon As Object, lngRibPointer As LongPtr
-
- lngRibPointer = GetSetting("CustomeUI", ThisWorkbook.Name, "RibbonPointer")
-
- CopyMemory objRibbon, lngRibPointer, LenB(lngRibPointer)
-
- Set GetRibbon = objRibbon
-
- End Function
- '
- '3 调用:
- '可以在VBA任何程序段中调用,(我是在错误处理部分调用),如
- Sub a()
- '.....
-
- On Error GoTo herr
-
- herr:
- Set MyRibbon = GetRibbon
- MyRibbon.Invalidate
- End Sub
复制代码
我再各位前辈的基础上,用Excel系统级隐藏名称来做系统级变量,代替注册表保存指针地址的方法。可以提高整个程序的兼容性:
- Option Explicit
- '增加 32/64位兼容性API声明
- #If VBA7 And Win64 Then
- Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long)
- #Else
- Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long)
- #End If
- Public MyRibbon As IRibbonUI
- Sub IRibbonUI_onLoad(ribbon As IRibbonUI)
- 'SaveSetting "CustomeUI", ThisWorkbook.Name, "RibbonPointer", ObjPtr(ribbon)
- '注册表保存方案;容易受权限影响,如果用户没有注册表操作权限就要保存失败。
- 'SetHideName "RibbonPointer", ObjPtr(ribbon) '这个是封装好的给Excel系统级变量赋值的方法。
- 'Excel Application 级别变量,只要Excel程序不关闭,变量一直有效; '不受任何环境影响。
- Application.ExecuteExcel4Macro "SET.NAME(""RibbonPointer""," & ObjPtr(ribbon) & ")" '这个仅限整数型的,如果是文本型变量需要在value上再加一层嵌套""""
-
- Set MyRibbon = ribbon
-
- End Sub
- Function GetRibbon() As Object '修改版V3 '指针存到隐藏Name中
-
- Dim objRibbon As Object, lngRibPointer As LongPtr
-
- lngRibPointer = Application.ExecuteExcel4Macro("RibbonPointer")
- CopyMemory objRibbon, lngRibPointer, LenB(lngRibPointer)
-
- Set GetRibbon = objRibbon
-
- End Function
- '
- '3 调用:
- '可以在VBA任何程序段中调用,(我是在错误处理部分调用),如
- Sub a()
- '.....
-
- On Error GoTo herr
-
- herr:
- Set MyRibbon = GetRibbon
- MyRibbon.Invalidate
- End Sub
- '再来一个例子,激活某个Tab
- Sub 激活某个Tab()
- Set MyRibbon = GetRibbon()
- MyRibbon.ActivateTabMso "TabData"
- 'MyRibbon.ActivateTab "方方格子"
- End Sub
- '再来个例子,需要用MyRibbon的时候提前赋值一次就可以
- Sub PageBreaks_clicked(control As IRibbonControl, pressed As Boolean)
- ActiveSheet.DisplayPageBreaks = pressed
- Set MyRibbon = GetRibbon()
- MyRibbon.InvalidateControl ("StyleInsp")
- End Sub
复制代码
|
|