[英]How to create a title on a powerpoint slide using 2 cells from an excel spreadsheet
我正在嘗試編寫一些VBA代碼,以從excel電子表格中提取2個單元格,並將它們都放在標題中,並在開頭添加一些文本。 誰能幫我做這個。 如果需要一點幫助,以防萬一我還不太清楚,我希望PowerPoint上的標題基本上是:
“來自(單元格A2的內容)的(單元格A1的內容)的響應”
我知道一定有辦法做到這一點,但這是我第一次嘗試使用VBA創建東西,但我發現這有點困難。
根據您的問題,以下是VBA代碼:
strFirst = (Contents of Cell A1) 'your code to read the value of A1
strScond = (Contents of Cell A2) 'your code to read the value of A2
strTitle = "Response from " & strFirst & " of " & strScond
Set pp = CreateObject("PowerPoint.Application")
Set PPPres = pp.Presentations.Add
pp.Visible = True
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutTitle)
'Some code to play with main (1st) slide
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutChart) 'ppLayoutChart would be depending upon your content/ your choice
PPSlide.Select
PPSlide.Shapes(1).Select
Set myTitle = PPSlide.Shapes.Title
myTitle.TextFrame.TextRange.Characters.Text = strTitle
pp.ActivePresentation.SaveAs ("some path")
pp.ActivePresentation.Close
pp.Quit
您必須添加Microsoft PowerPoint 12.0對象庫引用才能使用此代碼。
嘗試這樣做,以使您朝正確的方向前進:
Option Explicit
#Const EARLYBINDING = False
' ===========================================================================================
' Copy Specific cells to a Title shape in PowerPoint.
' Written by : Jamie Garroch of YOUpresent Ltd. (UK)
' Date : 07 JULY 2015
' For more amazing PowerPoint stuff visit us at from http://youpresent.co.uk/
' ===========================================================================================
' Copyright (c) 2015 YOUpresent Ltd.
' Source code is provide under Creative Commons Attribution License
' This means you must give credit for our original creation in the following form:
' "Includes code created by YOUpresent Ltd. (YOUpresent.co.uk)"
' Commons Deed @ http://creativecommons.org/licenses/by/3.0/
' License Legal @ http://creativecommons.org/licenses/by/3.0/legalcode
' ===========================================================================================
' Macro Execution Environment : Designed to run in Excel VBA.
' ===========================================================================================
' You can use Early Binding (with the advantage that IntelliSense adds) by adding a reference
' to the PowerPoint Object Library and setting the compiler constant EARLYBINDING to True
' but delete it afterwards otherwise you will face a nightmare of compatibility!!!
' ===========================================================================================
Public Sub CopyCellsToPowerPoint()
#If EARLYBINDING Then
' Define Early Binding PowerPoint objects so you can use IntelliSense while debuggging
' Requires a reference (Tools/References) to the Microsoft PowerPoint XX.Y Object Library
Dim oPPT As PowerPoint.Application
Dim oPres As PowerPoint.Presentation
Dim oSld As PowerPoint.Slide
Dim oShp As PowerPoint.Shape
#Else
' Define Late Binding PowerPoint objects
' Remove the reference to the Microsoft PowerPoint Object Library
Dim oPPT As Object
Dim oPres As Object
Dim oSld As Object
Dim oShp As Object
Const ppLayoutTitle = 1
#End If
' Define Excel objects
Dim oWB As Workbook
Dim oWS As Worksheet
' Define other variables
Dim sText As String
' Create an instance of PowerPoint
Set oPPT = CreateObject("PowerPoint.Application")
' Create a new Presentation
Set oPres = oPPT.Presentations.Add(WithWindow:=msoTrue)
' Insert a slide using the title layout
Set oSld = oPres.Slides.Add(1, ppLayoutTitle)
' Set a reference to the Excel workbook and sheet
Set oWB = Workbooks(1)
Set oWS = oWB.Worksheets(1)
' Create the title text from the A1 and A2 cells in the worksheet
sText = "Response from " & oWS.Cells(1, 1) & " of " & oWS.Cells(2, 1)
oSld.Shapes.Title.TextFrame.TextRange.Text = sText
' Clear objects
Set oPPT = Nothing
Set oPres = Nothing
Set oSld = Nothing
Set oShp = Nothing
Set oWB = Nothing
Set oWS = Nothing
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.