Sub 自定义工具栏()
Dim i%, r%, j% '循环变量,最大行'
Dim icoPath As String '图标路径'
Dim btn As CommandBarButton '按钮
Dim bt As String, gjl$ '标题,工具栏'
Dim arr, brr, crr, drr
icoPath = ThisWorkbook.path & "\tool\tb\" '图片路径=这个工作薄路径+\tool\ico\'
arr = ThisWorkbook.Sheets("清单").UsedRange
'--------------------------------------------------
Dim Toolbar As CommandBar, myVal$ '工具栏'临时变量
Dim d As Object
Set d = CreateObject("scripting.dictionary") '创建字典对象
Set regex1 = CreateObject("VBSCRIPT.REGEXP") 'RegEx为建立正则表达式
With regex1
.Global = True '设置全局可用
.Pattern = ".+"
End With
'--------------------------------------------------
On Error Resume Next
Application.ScreenUpdating = False
("MySoSo").Delete '删除mysoso工具栏'
Set Toolbar = ("MySoSo", msoBarTop) '创建一个名为mysoso的工具栏,位置放在最上面'
'--------------------------------------------------
With Toolbar
.Protection = msoBarNoResize '防止用户访问“添加或删除按钮”菜单
.Visible = True '显示工具栏'
.Position = msoBarTop '设置命令栏的位置为顶部'
'--------------------------------------------------
With .Controls.Add(Type:=msoControlButton) '新建一个按钮'
.Caption = "★自己定制★" '按钮名称'
.OnAction = "自定义菜单" '按钮执行的命令'
.FaceId = 609 '3198 '按钮图标'
.BeginGroup = True '显示分割线'
.Style = msoButtonIconAndCaptionBelow '样式等于图片+文本,文本在下面图标在上面'
.TooltipText = "把自己常用的工具通过自定义放到工具栏中" '鼠标停放时显示的文本'
End With
'--------------------------------------------------
With .Controls.Add(Type:=msoControlButton)
.Caption = "常用文件" '按钮名称'
.OnAction = "常用的文件" '按钮执行的命令'
.FaceId = 2105 '按钮图标'
.BeginGroup = True '显示分割线'
.Style = msoButtonIconAndCaptionBelow '样式等于图片+文本,文本在下面图标在上面'
End With
'--------------------------------------------------
With .Controls.Add(Type:=msoControlButton)
.Caption = "常用路径" '按钮名称'
.OnAction = "常用的路径" '按钮执行的命令'
.FaceId = 1660 '按钮图标'
'.BeginGroup = True '显示分割线'
.Style = msoButtonIconAndCaptionBelow '样式等于图片+文本,文本在下面图标在上面'
End With
'--------------------------------------------------
If GetSetting("PUPNAME", "APPNAME", "自定义工具栏") <> "" Then '如果注册表中有自定义工具栏项'
gjl = GetSetting("PUPNAME", "APPNAME", "自定义工具栏") '获取注册表中自定义工具栏的值'
Set c = regex1.Execute(gjl)
For j = 0 To c.Count - 1
d(c.Item(j).Value) = 0
Next j
'--------------------------------------------------
Else
brr = Array("科室定制", "智能文本", "智能加边框", "自动筛选", "相同值标色", "删除重复行", "数据查询", "颜色筛选")
For j = 0 To UBound(brr)
d(brr(j)) = 0
Next j
SaveSetting "PUPNAME", "APPNAME", "自定义工具栏", Join(brr, vbLf)
End If
'--------------------------------------------------
N = UBound(arr) '获取功能表格最大行'
For j = 2 To N
If d.exists(arr(j, 3)) Then '如果单元格中的值能在gjl中找到
bt = arr(j, 3)
'--------------------------------------------------
Set btn = .Controls.Add(msoControlButton, , , , True) '在mysoso工具栏上创建一个按钮'
With btn
.Caption = bt '按钮名称'
.OnAction = bt '按钮执行的命令'
.TooltipText = "【" & bt & "】" & arr(j, 4) '鼠标停放时显示的文本'
.BeginGroup = True '显示分割线'
.Style = msoButtonIconAndCaptionBelow '样式等于图片+文本,文本在下面图标在上面'
.Picture = LoadPicture(icoPath & bt & ".jpg") '获取路径下的图片作为图标'
End With
'--------------------------------------------------
End If
Next j
'--------------------------------------------------
With .Controls.Add(Type:=msoControlButton)
.Caption = "SoSo工具" '按钮名称'
.OnAction = "显示界面" '按钮执行的命令'
.FaceId = 266 '按钮图标'
.BeginGroup = True '显示分割线'
.Style = msoButtonIconAndCaptionBelow '样式等于图片+文本,文本在下面图标在上面'
End With
'--------------------------------------------------
End With
Application.ScreenUpdating = True
Set Toolbar = Nothing
End Sub