使用Excel VBA添加Powerpoint标题幻灯片

时间:2013-03-29 19:17:28

标签: excel vba excel-vba

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

4 个答案:

答案 0 :(得分:5)

所以,@ 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

<强>截图

enter image description here

答案 2 :(得分:0)

这是另一个使用“添加”方法的解决方案,并使用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

答案 3 :(得分:0)

这是我使用的解决方案:

'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