2015-05-04 49 views
1

由于我的某个项目使用Visio的时间很紧,我需要查看所有页面中某些字符的所有形状(将其命名为“&”),然后在它后面改变n个字符的颜色,所以我写了一个类似于下面的代码,但它并没有经过一个文本块中的所有事件,它碰到第一个循环后退出......我只需要帮助来解决它我的想法现在是种冻结...如果我的问题是愚蠢的遗憾在Visio中查找某个字符并重新格式化以下文本

Sub test() 
    Dim PageObj As Visio.Page 
    Dim shpsObj As Visio.Shapes 
    Dim shpObj As Visio.Shape 
    Dim oShpChar As Visio.Characters 

    Set PageObj = ActivePage 
    Set shpsObj = PageObj.Shapes 

    For Each shpObj In shpsObj 
     'Dim iLength As Integer 
     Dim iBeginOffset As Integer, iEndOffset As Integer 
     Set oShpChar = shpObj.Characters 
     Do 
      iBeginOffset = InStr(oShpChar.Text, "&test") 
      'If iBeginOffset = 0 Then Exit Do ' # Not found -> end the loop 

      iEndOffset = iBeginOffset + 3 

      oShpChar.Begin = iBeginOffset 
      oShpChar.End = iEndOffset 

      oShpChar.CharProps(visCharacterColor) = 9 

      oShpChar.Begin = oShpChar.Begin + 1 
      oShpChar.End = oShpChar.CharCount 
     Loop While (iEndOffset < oShpChar.CharCount) 
    Next 
End Sub 

我只是标记它为Excel也因为整个概念是相同的......

回答

1

发现问题...

不幸的是,Microsoft Visio没有通过外部循环保存“Character.Begin”和“Character.End”属性的更新值,换句话说,它保留但不能被其他方法访问作为 “CharProps”。所以我在while循环之外引入了一个计数器来跟踪上述属性的每个新值,希望它可以帮助其他人解决它们的问题,这会花费我7小时 (我不是开发人员,所以如果我制作了,请纠正我在我的解释中有错误)!

Sub test() 
    Set PageObj = ActivePage 
    Set shpsObj = PageObj.Shapes 

    For Each shpObj In shpsObj 
     Dim searchWord As String 
     Dim placeHolder As Integer 
     Dim iLength As Integer 
     Dim iBeginOffset As Integer, iEndOffset As Integer 
     Set oShpChar = shpObj.Characters 

     searchWord = "&test" 
     iLength = oShpChar.CharCount 
     Do 
      iBeginOffset = InStr(oShpChar.Text, searchWord) 
      If iBeginOffset = 0 Then Exit Do ' searchWord Not found -> end the loop 

      iBeginOffset = iBeginOffset + placeHolder 
      placeHolder = iBeginOffset + Len(searchWord) - 1 
      iEndOffset = iBeginOffset + Len(searchWord) - 1 

      oShpChar.Begin = iBeginOffset 
      oShpChar.End = iEndOffset 

      If iEndOffset > iLength Then Exit Do ' Preventing the last run 
      oShpChar.CharProps(visCharacterColor) = 9 

      oShpChar.Begin = oShpChar.Begin + Len(searchWord) - 1 
      oShpChar.End = iLength 
     Loop While (iEndOffset < iLength) 
    Next 
End Sub 
相关问题