如何在不改变样式的情况下通过 Excel VBA 修改 Powerpoint 中的文本 [英] How to modify text in Powerpoint via Excel VBA without changing style
问题描述
我正在尝试使用 VBA 替换 Excel 幻灯片文本中的一组标签.我可以按如下方式获取幻灯片文本:
I am trying to replace a set of tags in the text of a powerpoint slide from Excel using VBA. I can get the slide text as follows:
Dim txt as String
txt = pptSlide.Shapes(jj).TextFrame.TextRange.Characters.text
然后我将我的标签替换为请求的值.但是当我设置做
I then run through replacing my tags with the requested values. However when I set do
pptSlide.Shapes(jj).TextFrame.TextRange.Characters.text = txt
问题:用户在文本框中设置的所有格式都丢失了.
Problem: All the formatting which the user has set up in the text box is lost.
背景:形状对象是 msoPlaceHolder 并包含一系列文本样式,包括带有标签的项目符号,例如应该用数字替换.VBA 应该不知道这种格式,只需要关注文本替换.
Background: The shape object is msoPlaceHolder and contains a range of text styles including bullet points with tags which should be replaced with numbers for instance. The VBA should be unaware of this formatting and need only concern itself with the text replacement.
谁能告诉我如何在保持用户设置的样式的同时修改文本.
Can anyone tell me on how to modify the text while keeping the style set up by the user.
谢谢.
如果有帮助,我正在使用 Office 2010.
Am using Office 2010 if that is helpful.
推荐答案
虽然 Steve Rindsberg 所说的属实,但我想我已经想出了一个不错的解决方法.它绝不是漂亮的,但它可以在不牺牲格式的情况下完成工作.对于没有您要更改的变量的任何文本框,它使用查找函数和错误控制.
While what Steve Rindsberg said is true I think I have come up with a decent workaround. It is by no means pretty but it gets the job done without sacrificing the formatting. It uses Find functions and Error Controlling for any text box that doesn't have the variable you are looking to change out.
i = 1
Set oPs = oPa.ActivePresentation.Slides(oPa.ActivePresentation.Slides.Count)
j = 1
Do Until i > oPa.ActivePresentation.Slides.Count
oPa.ActivePresentation.Slides(i).Select
Do Until j > oPa.ActivePresentation.Slides(i).Shapes.Count
If oPa.ActivePresentation.Slides(i).Shapes(j).HasTextFrame Then
If oPa.ActivePresentation.Slides(i).Shapes(j).TextFrame.HasText Then
On Error GoTo Err1
If oPa.ActivePresentation.Slides(i).Shapes(j).TextFrame.TextRange.Find("[specific search term]") = "[specific search term]" Then
m = oPa.ActivePresentation.Slides(i).Shapes(j).TextFrame.TextRange.Find("[specific search term]").Characters.Start
oPa.ActivePresentation.Slides(i).Shapes(j).TextFrame.TextRange.Characters(m).InsertBefore ([replace term])
oPa.ActivePresentation.Slides(i).Shapes(j).TextFrame.TextRange.Find("[specific search term]").Delete
ExitHere:
End If
End If
End If
j = j + 1
Loop
j = 1
i = i + 1
Loop
Exit Sub
Err1:
Resume ExitHere
End Sub
希望这会有所帮助!
这篇关于如何在不改变样式的情况下通过 Excel VBA 修改 Powerpoint 中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!