【问题标题】:VBA Visio - how to run macro when shape is changedVBA Visio - 如何在形状更改时运行宏
【发布时间】:2014-07-10 23:48:55
【问题描述】:

我在 Visio 中有一个 VBA 代码,如果所述形状是超链接的,它将改变形状的颜色。现在,我使用一个简单的命令按钮来运行它。我希望宏在工作表中发生更改时运行。我知道在 excel 中如果我想这样做,我只需将我的代码放在 Workbook_Change 子中,但在 Visio 中我迷路了。

这是我当前的代码:

Private Sub CommandButton1_Click()

Dim Sh As Visio.shape
Dim Link As Hyperlink

For Each Sh In Visio.ActivePage.Shapes  '<~ loop through the shapes collection
For Each Link In Sh.Hyperlinks      '<~ loop through the links collection
    If Not Link.Address = "" Then   '<~ check for a blank address
        Sh.Cells("Fillbkgnd").Formula = "RGB(255,102,0)"
        Sh.Cells("Fillforegnd").Formula = "RGB(255, 102, 0)" '<~ apply a color to the shape
    End If
Next Link
Next Sh

End Sub

有什么想法吗?

【问题讨论】:

    标签: vba visio


    【解决方案1】:

    @JonFournier 我已经修改了这个,这是我在 ThisDocument 中的代码:

    Public WithEvents Pg As Visio.Page
    
    Private Sub Pg_CellChanged(ByVal Cell As IVCell)
    Set Pg = Pages("Page-1")
    If Cell.Section = visSectionHyperlink Then
        Dim Sh As Visio.shape
        Dim Link As Hyperlink
    
        For Each Sh In Visio.ActivePage.Shapes  '<~ loop through the shapes collection
            For Each Link In Sh.Hyperlinks      '<~ loop through the links collection
                If Not Link.Address = "" Then   '<~ check for a blank address
                    Sh.Cells("Fillbkgnd").Formula = "RGB(255,102,0)"
                    Sh.Cells("Fillforegnd").Formula = "RGB(255, 102, 0)"    '<~ apply a color to the shape
                End If
            Next Link
        Next Sh
    Else
    End If
    End Sub
    

    我放入的代码在与命令按钮配对时工作得非常好,但我希望它在形状改变时也能工作。我还应该在代码中添加什么来“实例化对象”或让它以我需要的方式运行。我似乎什么也做不了。感谢您的帮助。

    再次,很抱歉这是作为答案出现,我的工作防火墙由于某种原因不允许我发表评论。

    【讨论】:

      【解决方案2】:

      您可以在 Page 对象上捕获 CellChanged 事件,并检查更改的单元格是否在超链接 shapesheet 部分中。

      在类模块中:

      Public Withevents Pg as Visio.Page
      
      Private Sub Pg_CellChanged(ByVal Cell as IVCell)
          If Cell.Section = visSectionHyperlink Then
              ' call your code here
          End If
      End Sub
      

      您需要实例化该对象并使其保持活动状态以监控您的活动页面,但我认为这是对您有用的东西的一般要点。

      如果您愿意,这也可以在 ThisDocument 中愉快地使用。

      【讨论】:

      • 感谢您的回复,很抱歉花了这么长时间才回复。我工作中的防火墙不允许我回复在 stackoverflow 上发布的帖子。我曾尝试操纵您的代码以实现我想要做的事情,但仍然没有运气。如果我找到解决方案,我会告诉你的。
      猜你喜欢
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多