2013-03-29 85 views
1

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

回答

4

所以,對於一些額外的替代@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 
+0

+ 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

+0

一個建議,因爲其目的是添加標題,你可以使用'.Shapes.AddTitle',而不是'.Shapes.AddTextbox'這可能是有益的在某些情況下...... –

+0

是的。您也可以使用'.AddTitle'來恢復幻燈片中以前刪除的標題佔位符。:) –

+0

對於絕對快速的響應爲+1 :) –

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 
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 
相關問題