通过vba保留数据输入的文本格式

时间:2014-05-06 18:25:50

标签: vba powerpoint

我正在尝试使用vba将值从一个表复制到powerpoint中的另一个表。该文本在日期之后包含上标“st”,“nd”,“rd”等。我的代码是:

slide3.Shapes(9).Table.Cell(1, 1).Shape.TextFrame.TextRange = slide3.Shapes(2).Table.Cell(1, 1).Shape.TextFrame.TextRange

值转移得很好,但“st”,“nd”......与其他字符的格式相同。

我也希望复制上标格式。

1 个答案:

答案 0 :(得分:1)

不确定这是否可以在没有字符迭代的情况下完成,但我知道它可以通过这种方式完成。创建一个子例程,接受两个TextRange对象作为参数,然后您可以迭代每个字符。

像以下一样调用它:

CopyTextFormats slide3.Shapes(9).Table.Cell(1, 1).Shape.TextFrame.TextRange, _
                   slide3.Shapes(2).Table.Cell(1, 1).Shape.TextFrame.TextRange

这是子程序:

Sub CopyTextFormats(tRange1 As TextRange, tRange2 As TextRange)
Dim i As Long

'## "copy" the plain text from one table cell to the other
tRange2 = tRange1

'## Iterate the characters, in order to "copy" specific formatting properties:
For i = 1 To tRange1.Characters.Count

tRange2.Characters(i, 1).Font.Superscript = tRange1.Characters(i, 1).Font.Superscript
'### You can use this for other properties that you want to copy, too:
'
'tRange2.Characters(i, 1).Font.Bold = tRange1.Characters(i, 1).Font.Bold
'tRange2.Characters(i, 1).Font.Underline = tRange1.Characters(i, 1).Font.Underline
'
'If you don't want to copy a property, just omit it from this function

Next

End Sub