【问题标题】:Changing Font in PowerPoint with Input Box VBA使用输入框 VBA 更改 PowerPoint 中的字体
【发布时间】:2020-02-26 19:58:19
【问题描述】:

我正在尝试根据用户输入的字体更改 PowerPoint 演示文稿中所有文本的字体,但是当我尝试时它不会改变任何内容。我哪里错了?有没有更好的方法来实现这个……比如用字体下拉框或其他东西?因为我也想实现字体大小/粗体/斜体等。谢谢!

Sub ChangeFont()

Dim bpFontName As String

        bpFontName = InputBox("What font would you like to change EVERYTHING to?")


    With ActivePresentation
        For Each Slide In .Slides
            For Each Shape In Slide.Shapes
                With Shape
                    If .HasTextFrame Then
                        If .TextFrame.HasText Then
                            .TextFrame.TextRange.Font.Name = bpFontName
                            'Set font size below
                            .TextFrame.TextRange.Font.Size = 30
                            'Set if you want the font bold below - msoFalse = no
                            .TextFrame.TextRange.Font.Bold = msoTrue
                            'Set if you want the font bold below - msoFalse = no
                            .TextFrame.TextRange.Font.Italic = msoTrue
                        End If
                    End If
                End With
            Next
        Next
    End With
End Sub

【问题讨论】:

  • 如果在bpFintName = ... 处设置断点并按F8 单步执行代码会怎样?

标签: vba powerpoint


【解决方案1】:

首先,您没有对某些变量进行调暗,使用保留字(Slide、Shape)作为变量名是不好的做法。我已经把它修好了:

Sub ChangeFont()

Dim bpFontName As String
Dim oSld as Slide
Dim oSh as Shape

        bpFontName = InputBox("What font would you like to change EVERYTHING to?")


    With ActivePresentation
        For Each oSld In .Slides
            For Each oSh In oSld.Shapes
                With oSh
                    If .HasTextFrame Then
                        If .TextFrame.HasText Then
                            .TextFrame.TextRange.Font.Name = bpFontName
                            'Set font size below
                            .TextFrame.TextRange.Font.Size = 30
                            'Set if you want the font bold below - msoFalse = no
                            .TextFrame.TextRange.Font.Bold = msoTrue
                            'Set if you want the font bold below - msoFalse = no
                            .TextFrame.TextRange.Font.Italic = msoTrue
                        End If
                    End If
                End With
            Next
        Next
    End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多