如何在Visio中通过VBA为流程框创建带文本的自定义通知图标?
Visio自定义深蓝色通知图标VBA实现方案
核心实现思路
- 自定义深蓝色圆形图标,内置可编辑的编号文本字段
- 将图标绑定为流程框的子形状,实现随框移动、缩放的“边框附属”效果
- 支持批量为选中的流程框添加图标
VBA代码实现
Sub AddCustomNotificationIcon() Dim vsoPage As Visio.Page Dim vsoTargetShape As Visio.Shape Dim vsoNotificationIcon As Visio.Shape Dim iconSize As Double Dim borderOffset As Double Dim inputNumber As String ' 图标基础参数(可按需调整) iconSize = 0.25 ' 图标尺寸(英寸) borderOffset = 0.05 ' 图标与流程框边框的间距 ' 获取当前活动页面 Set vsoPage = Visio.ActivePage If vsoPage Is Nothing Then MsgBox "请先打开一个Visio页面", vbExclamation Exit Sub End If ' 遍历选中的流程框形状 For Each vsoTargetShape In vsoPage.Application.ActiveWindow.Selection ' 仅处理标准流程框(可根据实际形状的Master名称调整判断条件) If vsoTargetShape.Master.Name Like "*Process*" Or vsoTargetShape.Master.Name Like "*Rectangle*" Then ' 在流程框右上角创建深蓝色圆形图标 Set vsoNotificationIcon = vsoPage.DrawOval( _ vsoTargetShape.Cells("PinX").ResultIU + vsoTargetShape.Cells("Width").ResultIU / 2 + borderOffset, _ vsoTargetShape.Cells("PinY").ResultIU + vsoTargetShape.Cells("Height").ResultIU / 2 + borderOffset, _ vsoTargetShape.Cells("PinX").ResultIU + vsoTargetShape.Cells("Width").ResultIU / 2 + borderOffset + iconSize, _ vsoTargetShape.Cells("PinY").ResultIU + vsoTargetShape.Cells("Height").ResultIU / 2 + borderOffset + iconSize) ' 设置图标样式与文本 With vsoNotificationIcon ' 填充深蓝色 .CellsU("FillForegnd").FormulaU = "RGB(0,51,102)" .CellsU("FillPattern").FormulaU = "1" ' 实心填充 ' 隐藏边框 .CellsU("LineWeight").FormulaU = "0" ' 获取并设置编号文本 inputNumber = InputBox("输入图标编号(如3.1、3.12):", "设置编号", "3.1") If inputNumber <> "" Then .Text = inputNumber ' 文本样式:白色、居中、小号字体 .CellsU("Char.Color").FormulaU = "RGB(255,255,255)" .CellsU("Para.HorzAlign").FormulaU = "2" .CellsU("Char.Size").FormulaU = "8" End If ' 将图标绑定为流程框的子形状,随父形状同步变动 .MoveToContainer vsoTargetShape, visContainerFitNone ' 固定图标在父框右上角的相对位置 .CellsU("PinX").FormulaU = vsoTargetShape.Name & "!PinX + " & vsoTargetShape.Name & "!Width*0.5 + " & borderOffset .CellsU("PinY").FormulaU = vsoTargetShape.Name & "!PinY + " & vsoTargetShape.Name & "!Height*0.5 + " & borderOffset End With End If Next vsoTargetShape End Sub
使用步骤
- 打开Visio,按
Alt+F11打开VBA编辑器 - 插入新模块,粘贴上述代码
- 在Visio画布中选中需要添加图标的流程框
- 运行宏
AddCustomNotificationIcon,按提示输入编号即可
扩展优化提示
- 调整图标位置:修改代码中
PinX/PinY的公式,可将图标移至左下角、左上角等位置 - 批量预设编号:可替换输入框为下拉选择菜单,快速选择常用编号
- 统一样式管理:将图标样式封装为独立函数,方便后续统一修改颜色、尺寸
内容的提问来源于stack exchange,提问作者emmie
相关产品推荐
相关产品推荐

