【问题标题】:Adding a Powerpoint Title slide using Excel VBA使用 Excel VBA 添加 Powerpoint 标题幻灯片
【发布时间】:2013-03-29 19:17:27
【问题描述】:

我有从 excel 运行的 VBA 代码,它使用从 excel 文档中复制的图表生成 6 幻灯片演示文稿。我将使用哪些代码行来插入标题幻灯片并定义该幻灯片上的文本(标题 + 副标题)?使用 Excel 2007。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    所以,@Siddharth Rout 提案的一些额外替代方案(这也很好)。我使用.AddTitle 方法,这在格式化该形状的情况下可能是有益的。

    Sub add_title()
    
    Dim shpCurrShape As Shape
    
    Dim ppPres As Presentation
    
    Set ppPres = ActivePresentation
    With ppPres.Slides(1)
    
    If Not .Shapes.HasTitle Then
        Set shpCurrShape = .Shapes.AddTitle
    Else
        Set shpCurrShape = .Shapes.Title
    End If
    
        With shpCurrShape
        With .TextFrame.TextRange
            '~~> Set text here
            .Text = "BLAH BLAH"
            '~~> Alignment
            .ParagraphFormat.Alignment = 3
           '~~> Working with font
           With .Font
              .Bold = msoTrue
              .Name = "Tahoma"
              .Size = 24
              .Color = RGB(0, 0, 0)
           End With
        End With
    End With
    End With
    End Sub
    

    【讨论】:

    • + 1 替代 :)
    【解决方案2】:

    您必须使用.AddTextbox 来添加标题

    看这个例子

    Dim shpCurrShape As Object
    
    '~~> If doing from within PP remove oPPApp else it is your PP object
    With oPPApp.ActivePresentation.Slides(1)
        '~~> Add Heading
        'expression.AddTextbox(Orientation, Left, Top, Width, Height)
        Set shpCurrShape = .Shapes.AddTextbox(1, 18, 48, 654, 29.08126)
    
        With shpCurrShape
            With .TextFrame.TextRange
                '~~> Set text here
                .Text = "BLAH BLAH"
                '~~> Alignment
                .ParagraphFormat.Alignment = 3
               '~~> Working with font
               With .Font
                  .Bold = msoTrue
                  .Name = "Tahoma"
                  .Size = 24
                  .Color = RGB(0, 0, 0)
               End With
            End With
        End With
    End With
    

    截图

    【讨论】:

    • 一个建议,因为目的是添加标题,您可以使用 .Shapes.AddTitle 而不是 .Shapes.AddTextbox,这在某些情况下可能是有益的......
    • 是的。您也可以使用.AddTitle 来恢复幻灯片中之前删除的标题占位符。:)
    【解决方案3】:

    这是另一个使用“添加”方法的解决方案,并使用 Powerpoint 的 slideLayout 作为标题幻灯片。

    Sub AddTitleSlide()
    Dim sld As Slide
    Dim ttlBox As Shape
    
    Set sld = ActivePresentation.Slides.Add(1, ppLayoutTitle)
    Set ttlBox = sld.Shapes("Title 1")
    
    ttlBox.TextFrame2.TextRange.Characters.Text = "Here is the slide title!"
    
    End Sub
    

    【讨论】:

      【解决方案4】:

      这是我使用的解决方案:

      'Setup PPTX File
      Set oPA = CreateObject("PowerPoint.Application")
      oPA.Visible = True
      Set oPP = oPA.ActivePresentation
      slideNumber = oPP.Slides.Count + 1
      Set oPS = oPP.Slides.Add(slideNumber, ppLayoutBlank)
      oPA.ActiveWindow.View.GotoSlide (slideNumber) 'this line makes testing easier otherwise not required
      Set sObj = oPP.Slides(slideNumber)
      sObj.Shapes(1).TextFrame.TextRange.Text = titleText
      
       'Include Text in Powerpoint
      oPP.Slides(slideNumber).CustomLayout = oPP.Designs(1).SlideMaster.CustomLayouts(X) 'X=Layout Number with a title page
      sObj.Shapes(1).TextFrame.TextRange.Text = titleText
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-26
        • 1970-01-01
        • 2022-12-23
        • 1970-01-01
        • 2015-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多