Events 完成编辑后,如何以编程方式/自动更改Visio形状文本的文本格式?

Events 完成编辑后,如何以编程方式/自动更改Visio形状文本的文本格式?,events,macros,visio,Events,Macros,Visio,以下是我想要实现的目标: 我想使用Visio来记录项目。我有一些包含代码示例的Visio文本框。 代码示例中有注释。注释是行注释(行以#开头或位于以#开头的行末尾)# 我需要的是让Visio在编辑完该形状(文本方块)后自动将注释更改为另一种颜色 据我所知,这可以通过以下方式实现: -使用“TheText”事件并调用此(“函数名”) -“函数名”过程应该读取形状的文本,并在每一行上搜索“#”,然后将该行上的文本变为灰色,直到行尾 你能确认一下我是否走对了这条路吗? 我是Visio和VBA的初学者。

以下是我想要实现的目标: 我想使用Visio来记录项目。我有一些包含代码示例的Visio文本框。 代码示例中有注释。注释是行注释(行以#开头或位于以#开头的行末尾)#

我需要的是让Visio在编辑完该形状(文本方块)后自动将注释更改为另一种颜色

据我所知,这可以通过以下方式实现:

-使用“TheText”事件并调用此(“函数名”)

-“函数名”过程应该读取形状的文本,并在每一行上搜索“#”,然后将该行上的文本变为灰色,直到行尾

你能确认一下我是否走对了这条路吗? 我是Visio和VBA的初学者。 为了测试上面的内容,我使用了一个宏,它应该在编辑完成后立即移动形状

次宏()

端接头

而“TheText”事件单元格的this=CALLTHIS(“ThisDocument.Macro”)

我希望在编辑完成后形状会立即向右移动,但没有发生 我做错了什么

非常感谢
P

调用此shapesheet函数需要一个子/函数,其名称与第一个参数匹配,显然模块和例程名称之间用点分隔。第二个参数是VBA项目名称,这是可选的


CALLTHIS还希望例程的第一个参数是触发事件的Visio.Shape,因此Sub Macro()实际上应该是Sub Macro(ShpObj作为Visio.Shape)。我认为这样做之后,事件应该会起作用。

调用此shapesheet函数需要一个子/函数,其名称与第一个参数匹配,显然是用点分隔模块和例程名称。第二个参数是VBA项目名称,这是可选的


CALLTHIS还希望例程的第一个参数是触发事件的Visio.Shape,因此Sub Macro()实际上应该是Sub Macro(ShpObj作为Visio.Shape)。我认为这样做之后,事件应该会起作用。

调用此shapesheet函数需要一个子/函数,其名称与第一个参数匹配,显然是用点分隔模块和例程名称。第二个参数是VBA项目名称,这是可选的


CALLTHIS还希望例程的第一个参数是触发事件的Visio.Shape,因此Sub Macro()实际上应该是Sub Macro(ShpObj作为Visio.Shape)。我认为这样做之后,事件应该会起作用。

调用此shapesheet函数需要一个子/函数,其名称与第一个参数匹配,显然是用点分隔模块和例程名称。第二个参数是VBA项目名称,这是可选的


CALLTHIS还希望您的例程的第一个参数是触发事件的Visio.Shape,因此您的Sub Macro()实际上应该是Sub Macro(作为Visio.Shape的ShpObj)。我认为这样做之后,事件应该可以工作。

试试这个Sub,它的编程不是很好,但应该可以做到这一点

您需要将代码添加到VBA编辑器中的ThisDocument模块(按Alt+F11将其打开)

实际上,完成文本编辑后,TheText()事件不会调用格式化函数:在修改文本时(无论何时按下键、格式化或调整文本框大小),会反复调用格式化函数,这将非常低效

我将使用另一个事件,比如EvntDoubleClick(),或者用户定义的菜单选项j,来调用changeColor sub

  Sub changeColor(oShape As Visio.Shape)

     On Error GoTo Err_changeColor

     Dim iLength As Integer
     Dim iBeginOffset As Integer, iEndOffset As Integer
     Dim oShpChar As Visio.Characters

     Set oShpChar = oShape.Characters

     iLength = oShpChar.CharCount

     ' Main loop: We go through all the text selecting the adecuate portions to change
     Do
        ' Find the position of next # character
        iBeginOffset = InStr(oShpChar.Text, "#")
        If iBeginOffset = 0 Then Exit Do    ' # Not found -> end the loop

        ' Find the position of next LF to change color only until the end of the line
        iEndOffset = InStr(iBeginOffset, oShpChar.Text, vbLf)
        If iEndOffset = 0 Then iEndOffset = iLength - oShpChar.Begin      ' If not found, change everything

        ' Update the portion to change
        oShpChar.End = oShpChar.Begin + iEndOffset          ' We use the previous beginning position plus the offset
        oShpChar.Begin = oShpChar.Begin + iBeginOffset - 1  ' Idem. We want to change #'s color, too, thus the (-1)

        ' Change color of the selected text (between Begin and End) to green (9)
        oShpChar.CharProps(visCharacterColor) = 9

        oShpChar.Begin = oShpChar.Begin + 1    ' We don't want to find the same # again, so we undo the previous (-1)
        oShpChar.End = iLength                 ' We want to continue searching until the end

     Loop While (iEndOffset <> iLength)

  Exit_changeColor:

     If Not oShpChar Is Nothing Then Set oShpChar = Nothing
     Exit Sub

  Err_changeColor:

     MsgBox Err.Number & ": " & Err.Description, vbExclamation, "Error"
     Resume Exit_changeColor

  End Sub
Sub changeColor(oShape作为Visio.Shape)
On Error GoTo Err_CHANGECLOR
将长度设置为整数
Dim iBeginOffset为整数,iEndOffset为整数
将oShpChar设置为Visio.Characters
设置oShpChar=oShape.Characters
iLength=oShpChar.CharCount
'主循环:我们遍历所有文本,选择要更改的特定部分
做
'查找下一个#字符的位置
iBeginOffset=InStr(oShpChar.Text,“#”)
如果iBeginOffset=0,则退出Do'#Not found->结束循环
'查找下一个LF的位置以仅更改颜色,直到行尾
iEndOffset=InStr(iBeginOffset,oShpChar.Text,vbLf)
如果iEndOffset=0,则iEndOffset=ILENGHT-oShpChar.Begin“如果找不到,则更改所有内容
'更新要更改的部分
oShpChar.End=oShpChar.Begin+iEndOffset'我们使用上一个开始位置加上偏移量
oShpChar.Begin=oShpChar.Begin+iBeginOffset-1’Idem。我们也要更改#的颜色,因此(-1)
'将所选文本的颜色(介于开始和结束之间)更改为绿色(9)
oShpChar.CharProps(visCharacterColor)=9
oShpChar.Begin=oShpChar.Begin+1'我们不想再次找到相同的#,因此我们撤消了前面的(-1)
oShpChar.End=ILENGHT'我们希望继续搜索,直到结束
循环时间(iEndOffset ILENGHT)
退出更改颜色:
如果不是oShpChar,则设置oShpChar=Nothing
出口接头
Err_changeColor:
MsgBox错误编号&“:”错误描述(&R),VBE感叹号,“错误”
恢复退出并更改颜色
端接头

试试这款潜水艇,它的程序设计不是很好,但应该可以

您需要将代码添加到VBA编辑器中的ThisDocument模块(按Alt+F11将其打开)

实际上,完成文本编辑后,TheText()事件不会调用格式化函数:在修改文本时(无论何时按某个键、格式化或调整文本框大小),会反复调用该函数,这将
  Sub changeColor(oShape As Visio.Shape)

     On Error GoTo Err_changeColor

     Dim iLength As Integer
     Dim iBeginOffset As Integer, iEndOffset As Integer
     Dim oShpChar As Visio.Characters

     Set oShpChar = oShape.Characters

     iLength = oShpChar.CharCount

     ' Main loop: We go through all the text selecting the adecuate portions to change
     Do
        ' Find the position of next # character
        iBeginOffset = InStr(oShpChar.Text, "#")
        If iBeginOffset = 0 Then Exit Do    ' # Not found -> end the loop

        ' Find the position of next LF to change color only until the end of the line
        iEndOffset = InStr(iBeginOffset, oShpChar.Text, vbLf)
        If iEndOffset = 0 Then iEndOffset = iLength - oShpChar.Begin      ' If not found, change everything

        ' Update the portion to change
        oShpChar.End = oShpChar.Begin + iEndOffset          ' We use the previous beginning position plus the offset
        oShpChar.Begin = oShpChar.Begin + iBeginOffset - 1  ' Idem. We want to change #'s color, too, thus the (-1)

        ' Change color of the selected text (between Begin and End) to green (9)
        oShpChar.CharProps(visCharacterColor) = 9

        oShpChar.Begin = oShpChar.Begin + 1    ' We don't want to find the same # again, so we undo the previous (-1)
        oShpChar.End = iLength                 ' We want to continue searching until the end

     Loop While (iEndOffset <> iLength)

  Exit_changeColor:

     If Not oShpChar Is Nothing Then Set oShpChar = Nothing
     Exit Sub

  Err_changeColor:

     MsgBox Err.Number & ": " & Err.Description, vbExclamation, "Error"
     Resume Exit_changeColor

  End Sub