简体   繁体   English

如何使用Excel电子表格中的2个单元格在PowerPoint幻灯片上创建标题

[英]How to create a title on a powerpoint slide using 2 cells from an excel spreadsheet

I'm trying to write some VBA code to take 2 cells from an excel spreadsheet and put them both in a title with some text at the beginning. 我正在尝试编写一些VBA代码,以从excel电子表格中提取2个单元格,并将它们都放在标题中,并在开头添加一些文本。 Can anyone help me going about doing this. 谁能帮我做这个。 Just for a bit of help in case I wasn't clear enough I'm wanting the title on the powerpoint to basically be: 如果需要一点帮助,以防万一我还不太清楚,我希望PowerPoint上的标题基本上是:

"Response from (Contents of Cell A1) of (Contents of Cell A2)" “来自(单元格A2的内容)的(单元格A1的内容)的响应”

I know there must be a way to do this but this is my first time trying to create something with VBA and I'm finding it a bit difficult. 我知道一定有办法做到这一点,但这是我第一次尝试使用VBA创建东西,但我发现这有点困难。

Based on your question, here is VBA code: 根据您的问题,以下是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  

You have to add Microsoft PowerPoint 12.0 Object Library reference in order to use this code. 您必须添加Microsoft PowerPoint 12.0对象库引用才能使用此代码。

Try this to get you going in the right direction: 尝试这样做,以使您朝正确的方向前进:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM