簡體   English   中英

使用Excel VBA添加Powerpoint標題幻燈片

[英]Adding a Powerpoint Title slide using Excel VBA

我有從excel運行的VBA代碼,該代碼使用從excel文檔復制到圖表中生成6張幻燈片的PowerPoint演示文稿。 我將使用哪些代碼行插入標題幻燈片,並在該幻燈片上定義文本(標題+子標題)? 使用Excel 2007。

因此,@ 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

您必須使用.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

屏幕截圖

在此處輸入圖片說明

這是使用“添加”方法的另一種解決方案,並將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

這是我使用的解決方案:

'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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM