【问题标题】:Update the fill colour of each shape immediately after it is changed in a loop循环更改后立即更新每个形状的填充颜色
【发布时间】:2019-06-28 14:57:22
【问题描述】:

我试图让 Visio 在我更改每个形状后立即更新它的填充颜色。

我尝试过使用各种方法 - screenupdate、showchanges、sendkeys "%^g" 但对颜色不起作用。仅将屏幕尺寸更改 0.01% 就会强制应用更改文本,这至少是一些事情。

我可以单步执行代码并且它可以工作,但是当我运行它时,直到最后颜色都没有变化。

我正在使用以下方法更改每个对象的颜色:

Application.ActiveWindow.Page.Shapes.ItemFromID(servshape(y - 1)).CellsU("Fillforegnd").FormulaU = "RGB(253, 190, 0)"

代码遍历日期列表并在需要时更改对象的颜色,问题是它只显示最后的更改。

列表中每个项目的循环大约为一秒,足以看到任何变化。我希望有一个简单的刷新命令,但这似乎只适用于数据记录集。

有什么方法可以在更改对象填充颜色后立即刷新它?

【问题讨论】:

  • 另外,您的代码可以通过执行“For Each”循环来改进,除非您确定 ID 存在,否则使用 ItemFromID 很容易出错!如果有人在执行期间的任何时候单击“外部”应用程序,在代码开头将 ActiveWindow/Page 分配给变量也应该提高稳定性。

标签: vba visio


【解决方案1】:

应该使用DoEvents:

Option Explicit

Sub reColorAll()
    Dim pg As Visio.Page
    'Set pg = Application.ActiveWindow.Page
    Set pg = ActivePage ' Probably what you want



    Dim shp As Visio.Shape
    For Each shp In pg.Shapes
        If True Then 'test if shape is one of the ones you want, replace true with test
            If shp.CellExistsU("Fillforegnd", False) Then 'test if cell even exists
                shp.CellsU("Fillforegnd").FormulaU = "RGB(253, 190, 0)"
                DoEvents' force Application to update
            End If

            'Timer to simulate delay, can be removed for your case
            Dim pauseTime As Long
            Dim start As Long
            pauseTime = 1   ' Set duration in seconds
            start = Timer    ' Set start time.
            Do While Timer < start + pauseTime
            Loop
            'End Timer Code

        End If
    Next shp

End Sub

Timer Source:

【讨论】:

  • 太棒了!感谢您的帮助,效果很好,这让我发疯了!非常感谢,马特
  • @mattc123 没问题。既然这似乎解决了您的问题,可以将答案标记为已接受?
猜你喜欢
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多