vba - 为什么 TextRange.InsertSymbol 方法会替换我的 TextRange 中的文本?

标签 vba powerpoint powerpoint-2010

在powerpoint中通过VBA生成幻灯片的过程中,我需要在生成的文本中插入一个“Wingdings符号”,这是两个值的比较。我制作了这个方法,它完全按照我的意愿工作

Sub formatDifference(header As String, old As Integer, now As Integer, txt As TextRange)
    Dim diff As Integer
    diff = old - now

    With txt
        If (diff > 0) Then
            .InsertSymbol "Wingdings", getArrowCharCode("down")
                                       ' getArrowCharCode is a custom function to get the 
                                       ' char code as an Integer
        ElseIf (diff = 0) Then
            .InsertSymbol "Wingdings", getArrowCharCode("right")
        Else
            .InsertSymbol "Wingdings", getArrowCharCode("up")
        End If

        .InsertBefore header & now & " ("    ' <-- note this line
        .InsertAfter " " & Abs(diff) & ")"
    End With
End Sub
formatDifference Sub 基本上只是在文本中添加一个项目符号线(在下面的示例中,在添加非项目符号文本之前,该过程被调用了 4 次)。

我不明白的是当我用一些文本启动文本然后使用 InsertSymbol方法,文本似乎实际上被替换了,而不是在末尾附加符号。这是不同代码的示例:
Sub formatDifference(header As String, old As Integer, now As Integer, txt As TextRange)
    Dim diff As Integer
    diff = old - now

    With txt
        .InsertAfter header & now & " (" ' <-- line moved here
                                         '     it doesn't matter if I use 
                                         '     .Text = "initial text"', 
                                         '     it will do the same thing
        If (diff > 0) Then
            .InsertSymbol "Wingdings", getArrowCharCode("down")
        ElseIf (diff = 0) Then
            .InsertSymbol "Wingdings", getArrowCharCode("right")
        Else
            .InsertSymbol "Wingdings", getArrowCharCode("up")
        End If
        .InsertAfter " " & Abs(diff) & ")"
    End With
End Sub

这是我从上面的代码中得到的两个结果的比较(以相同的顺序):

Example of the difference the codes make

我的理解InsertSymbol方法是它会在最后一段的末尾插入符号,但它看起来不像......我的第二个例子是错误的还是我误解了description of the method ?

附言备注 :标题参数包含回车符和换行符,这就是为什么第二次捕获将所有点都放在同一行上的原因,因为第一部分似乎被替换了。

最佳答案

我可以提出一个似乎工作正常的解决方法。

Sub AppendSymbol(ByRef orig As TextRange, ByVal fontName As String, ByVal charCode As Integer, Optional ByVal position As Integer = -1)
    Dim strStart, strEnd As String

    If ((position < 0) Or (position >= Len(orig.text))) Then
        orig.Paragraphs(orig.Paragraphs.Count + 1).InsertSymbol fontName, charCode
        'this one just inserts the symbol at the end by forcing a new paragraph
    Else
        strStart = Left(orig.text, position)
        strEnd = Right(orig.text, Len(orig.text) - position)

        orig.Paragraphs(1).InsertSymbol fontName, charCode
        orig.InsertBefore strStart
        orig.InsertAfter strEnd
    End If
End Function

在那个子中,我基本上复制了原始行,用符号替换它,然后在符号周围重新附加字符串。

我现在可以这样调用 Sub:
Private Sub displayTotal(ByRef para As TextRange, ByVal prevCompare As Testing)
    Dim arrowDirection As String
    Dim tempDifference As Integer

    tempDifference = p_total - prevCompare.Total
    para.InsertAfter "Number of elements : " & p_total & " ("

    'calling the Sub
    AppendSymbol para, "Wingdings", getWingdingArrowChar(getArrowDirection(tempDifference))

    para.InsertAfter " " & Abs(tempDifference) & ")"
    para.IndentLevel = 2
    para.ParagraphFormat.Bullet = msoFalse
End Sub

至于解释,Asger似乎在做某事。该方法的实现显然类似于 Word 的实现,需要先折叠文本范围,然后才能添加符号。

在上面的自定义方法中,行 orig.Paragraphs(orig.Paragraphs.Count + 1).InsertSymbol fontName, charCode基本上是通过在当前段落之后添加一个段落来强制折叠当前段落,这就是 InsertSybol 的原因。方法然后按预期工作。

关于vba - 为什么 TextRange.InsertSymbol 方法会替换我的 TextRange 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56483856/

相关文章:

vba - 简报 VBA : Search for an character Arrow and replace with a shape arrow

sql - 如何将记录集值分配给二维数组

vba - 从 VBA 中的不同模块调用子例程

c# - 打开 powerpoint 演示文稿并隐藏窗口

flash - 将powerpoint转换为flash

c# - powerpoint 记录排练时间

.net - Office 功能区 XML 与 Office 标准功能区设计器?

ms-access - 如何检查调用堆栈

excel - 使用列标题和子标题填充 TreeView

vba - 在 PowerPoint VBA 中获取形状索引