[英]Adding a Powerpoint Title slide using Excel VBA
I have VBA code running from excel which produces a 6 slide powerpoint presentation using copied in charts from an excel document. 我有从excel运行的VBA代码,该代码使用从excel文档复制到图表中生成6张幻灯片的PowerPoint演示文稿。 What code lines would I use to insert a title slide, and define the text on that slide (title + sub title)?
我将使用哪些代码行插入标题幻灯片,并在该幻灯片上定义文本(标题+子标题)? Using Excel 2007.
使用Excel 2007。
So, some additional alternative for @Siddharth Rout proposal (which is good as well). 因此,@ Siddharth Rout建议的其他替代方案也很好。 I use
.AddTitle
method which could be beneficial in case of formatting of that shape. 我使用
.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
You have to use the .AddTextbox
to add the Title 您必须使用
.AddTextbox
添加标题
See this example 看这个例子
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
Screenshot 屏幕截图
Here's another solution which uses the "Add" method, and uses Powerpoint's slideLayout for a Title slide. 这是使用“添加”方法的另一种解决方案,并将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
Heres a solution I use: 这是我使用的解决方案:
'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.