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

我试图写一些VBA代码,从Excel电子表格中取2个单元格,并将它们放在一个标题中,并在开始处显示一些文本。 任何人都可以帮助我去做这件事。 只是为了帮助一些我不够清楚的事情,我希望这篇文章的标题基本上是:

“(单元格A2的内容)的(单元格A1的内容)的响应”

我知道必须有办法做到这一点,但这是我第一次尝试用VBA创build一些东西,而且我觉得有点困难。

基于你的问题,这里是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