在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
这是我从上面的代码(以相同的顺序)得到的两个结果的比较:
我对
InsertSymbol
方法的理解是,它将在最后一段的末尾插入符号,但看起来不像...我的第二个示例是否有误?还是我误解了description of the method?P.S.注意:header参数包含回车符和换行符,这就是为什么第二个捕获在同一行上具有所有点的原因,因为似乎替换了第一部分。
最佳答案
InsertSymbol实现的文档
Microsoft Word的Range.InsertSymbol和Selection.InsertSymbol实现描述为:
Microsoft Publisher的TextRange.InsertSymbol实现描述为:
然后有Office TextRange2.InsertSymbol和PowerPoint TextRange2.InsertSymbol方法描述为:
考虑到这一点,TextRange.InsertSymbol实现的PowerPoint文档有点不准确,其中包括所包含的代码示例的错误的解释(“第一句话之后”)。
TextRange之后的InsertSymbol
如果要在提供的TextRange
之后插入符号,我建议使用以下包装器函数:
Public Function InsertSymbolAfter(newTextRange As PowerPoint.TextRange, _
newFontName As String, _
newCharNumber As Long, _
Optional newUnicode As MsoTriState = msoFalse) As TextRange
If Right(newTextRange.Text, 1) = vbCr Then
Set InsertSymbolAfter = newTextRange.Characters(newTextRange.Characters.Count) _
.InsertSymbol(newFontName, newCharNumber, newUnicode)
newTextRange.InsertAfter vbCr
Else
Set newTextRange = newTextRange.InsertAfter("#")
Set InsertSymbolAfter = newTextRange _
.InsertSymbol(newFontName, newCharNumber, newUnicode)
End If
End Function
它区分两种情况:vbCR
(Chr(13)
,回车,CR),则在CR之前添加符号(CR用新符号代替,然后再添加)。 测试
可以通过以下方法对整个文本框架,段落或字符进行测试:
Private Sub testit()
Const WingDingsLeft As Long = 231
Const WingDingsRight As Long = 232
Const WingDingsUp As Long = 233
Const WingDingsDown As Long = 234
With ActivePresentation.Slides(1).Shapes(1).TextFrame
InsertSymbolAfter(.TextRange, "Wingdings", WingDingsUp)
InsertSymbolAfter(.TextRange.Paragraphs(1), "Wingdings", WingDingsDown)
InsertSymbolAfter(.TextRange.Characters(2, 1), "Wingdings", WingDingsRight)
End With
End Sub