我正在尝试使用 VBA 将一些文本插入到 PowerPointTextRange
中,我使用这样的东西:
ActiveWindow.Selection.SlideRange.Shapes("rec1").TextFrame.TextRange.Text = "Hi"
但是,我不知道如何以编程方式应用粗体、斜体和下划线(我没有看到 .RichText 属性或类似的东西)。
我所拥有的是一些简单的 HTML 文本,其中包含我想转换的粗体、斜体和下划线文本。
这该怎么做?
我正在尝试使用 VBA 将一些文本插入到 PowerPointTextRange
中,我使用这样的东西:
ActiveWindow.Selection.SlideRange.Shapes("rec1").TextFrame.TextRange.Text = "Hi"
但是,我不知道如何以编程方式应用粗体、斜体和下划线(我没有看到 .RichText 属性或类似的东西)。
我所拥有的是一些简单的 HTML 文本,其中包含我想转换的粗体、斜体和下划线文本。
这该怎么做?
这很容易通过使用TextRange
's Characters
、Words
、Sentences
和对象然后设置 Bold、Underline 和 Italic (以及其他属性)的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
尝试查看有关TextRange object的MSDN 文档。它包含有关如何访问 TextRange 对象的 Font 属性的示例。
编辑:您可以通过这种方式以编程方式访问粗体和斜体等内容:
TextRange.Font.Bold = msoTrue
编辑编辑:有几种方法可以只选择文本范围中的某些文本。请参阅以下内容:
根据此链接的相同内容,您可以使用其中一种方法选择部分文本并以编程方式设置字体。例如:
Application.ActiveDocument.Pages(1).Shapes(2) _
.TextFrame.TextRange.Words(Start:=2, Length:=3) _
.Font.Bold = True
该示例取自 Words Method 链接。
除了上述答案之外,您还应该尝试命名要更改的对象,因为在演示文稿中间选择它们可能会使 PowerPoint 行为异常。创建一个新的 TextRange 对象并像这样设置它。
dim mytextrange As TextRange
Set mytextrange = ActiveDocument.Pages(1).Shapes(2).TextFrame.TextRange
mytextrange.Words...
以下是更改特定文本字体的方法:
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