vba 以编程方式将字体格式应用于 PowerPoint 文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/965551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apply Font Formatting to PowerPoint Text Programmatically
提问by OneNerd
I am trying to use VBA to insert some text into a PowerPoint TextRange, I use something like this:
我正在尝试使用 VBA 将一些文本插入到 PowerPoint 中TextRange,我使用的是这样的:
ActiveWindow.Selection.SlideRange.Shapes("rec1").TextFrame.TextRange.Text = "Hi"
However, I can't figure out how to apply bold, italic and underline programmatically (I don't see a .RichText property or something similar).
但是,我无法弄清楚如何以编程方式应用粗体、斜体和下划线(我没有看到 .RichText 属性或类似的东西)。
What I have is some simple HTML text with bold, italic and underlined text I would like to convert over.
我所拥有的是一些简单的 HTML 文本,其中包含我想转换的粗体、斜体和带下划线的文本。
How to do this?
这该怎么做?
回答by Todd Main
This is easily accomplished by using the TextRange's Characters, Words, Sentences, Runsand Paragraphsobjects and then it's Fontobject to set Bold, Underline and Italic (amongst other properties). For example:
这很容易通过使用完成TextRange的Characters,Words,Sentences,Runs和Paragraphs对象,然后它的Font对象来设置加粗,下划线和斜体(除其他性质)。例如:
Sub setTextDetails()
Dim tr As TextRange
Set tr = ActiveWindow.Selection.SlideRange.Shapes(1).TextFrame.TextRange
With tr
.Text = "Hi There Buddy!"
.Words(1).Font.Bold = msoTrue
.Runs(1).Font.Italic = msoTrue
.Paragraphs(1).Font.Underline = msoTrue
End With
End Sub
回答by Matthew Jones
Try looking at MSDN's documentationon the TextRange object. It contains samples of how to access the Font properties of the TextRange object.
尝试查看有关TextRange 对象的MSDN 文档。它包含有关如何访问 TextRange 对象的 Font 属性的示例。
EDIT: You can access things like Bold and Italics programmatically in this manner:
编辑:您可以通过这种方式以编程方式访问粗体和斜体等内容:
TextRange.Font.Bold = msoTrue
EDIT EDIT: There are several methods by which you can select only certain text in a text range. See the following:
编辑 编辑:有几种方法可以让您仅选择文本范围中的某些文本。请参阅以下内容:
According to the sames from this link, you can select a portion of the text using one of these methods and set the font programmatically. For example:
根据此链接中的相同内容,您可以使用其中一种方法选择文本的一部分并以编程方式设置字体。例如:
Application.ActiveDocument.Pages(1).Shapes(2) _
.TextFrame.TextRange.Words(Start:=2, Length:=3) _
.Font.Bold = True
That example was taken from the Words Method link.
该示例取自 Words Method 链接。
回答by Andrew Scagnelli
In addition to the above answer, you should try to name the objects you'll be changing, since selecting them in the middle of a presentation could make PowerPoint act oddly. Create a new TextRange object and set it like this.
除了上述答案之外,您还应该尝试命名您将要更改的对象,因为在演示过程中选择它们可能会使 PowerPoint 运行异常。创建一个新的 TextRange 对象并像这样设置它。
dim mytextrange As TextRange
Set mytextrange = ActiveDocument.Pages(1).Shapes(2).TextFrame.TextRange
mytextrange.Words...
回答by Rafiki
Here is how you can do it to change the font of a specific text:
以下是更改特定文本字体的方法:
Sub changeFont()
Dim oPresentation As Presentation
Dim oSlide As Slide
Dim oShape As Shape
Dim stringSearched As String
stringSearched = "something"
'all opened presentations
For Each oPresentation In Presentations
'all slide in them
For Each oSlide In oPresentation.Slides
'all shapes (anything)
For Each oShape In oSlide.Shapes
'only those that contain text
If oShape.HasTextFrame Then
If InStr(oShape.TextFrame.TextRange.Text, stringSearched) > 0 Then
'here you need to define where the text ends and start
oShape.TextFrame.TextRange.Characters(InStr(oShape.TextFrame.TextRange.Text, stringSearched), Len(stringSearched)).Font.Underline = msoTrue
oShape.TextFrame.TextRange.Characters(InStr(oShape.TextFrame.TextRange.Text, stringSearched), Len(stringSearched)).Font.Italic = msoFalse
End If
End If
Next
Next
Next
End Sub

