在通过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 SubformatDifference 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下面是我从上面的代码中得到的两个结果的比较(按相同的顺序):

我对InsertSymbol方法的理解是,它会在最后一段末尾插入符号,但它看起来不像.我的第二个例子是错误的,还是我误解了对方法的描述?
P.S.注意到:标头参数保存了回车和行提要字符,这就是为什么第二次捕获在同一行上有所有的点,因为第一部分似乎被替换了。
发布于 2019-07-10 12:51:13
我可以做一个似乎很好的解决办法。
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在这个子部分中,我基本上复制了原来的行,用符号替换它,然后在符号周围重新添加字符串。
我现在可以这样称呼它:
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至于解释,阿瑟尔似乎发现了一些东西。该方法的实现显然类似于Word的实现,它需要在添加符号之前折叠文本范围。
在上面的自定义方法中,行orig.Paragraphs(orig.Paragraphs.Count + 1).InsertSymbol fontName, charCode基本上是通过在当前段落后面添加一个段落来强制当前段落折叠,这就是为什么InsertSybol方法随后按预期工作。
https://stackoverflow.com/questions/56483856
复制相似问题