使用 VBA 将数据从 Excel 导出到现有的 PowerPoint 幻灯片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15760629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:18:22  来源:igfitidea点击:

Exporting data from Excel to an existing PowerPoint slide using VBA

excelvbaexcel-vbapowerpoint

提问by user2235370

i am writing a small excel add-in for some simple data analytics but unfortunately i am not a VBA-programmer ;)

我正在为一些简单的数据分析编写一个小的 excel 插件,但不幸的是我不是 VBA 程序员;)

what i have done up to now:

到目前为止我做了什么:

  • fetch data from a huge excel workbook and perform calculations
  • write the results to a neatly preformatted excel 'skeleton' file
  • 从巨大的 excel 工作簿中获取数据并执行计算
  • 将结果写入一个整齐的预先格式化的 excel '骨架' 文件

what i want to do next:

我接下来想做什么:

  • write the very same data to an also already neatly prepared pptslide directly from the excel vba code.
  • 直接从 excel vba 代码将完全相同的数据写入已经准备好的 ppt幻灯片。

i think it is possible to define some sort of textboxes inside the slide and then just put the values into them... just havent found anything yet !

我认为可以在幻灯片中定义某种文本框,然后将值放入其中......只是还没有找到任何东西!

any help on this is appreciated ;)

对此的任何帮助表示赞赏;)

this is excel&powerpoint 2007, but i want to do it as version independent as possible.

这是 excel&powerpoint 2007,但我想尽可能独立于版本。

TIA

TIA

回答by Our Man in Bananas

here is some code I found on Mahipal Padigela's VBA website

这是我在Mahipal Padigela 的 VBA 网站上找到的一些代码

(it is also referenced on StackOverFlow)

(它也在StackOverFlow上被引用)

''# Code by Mahipal Padigela
''# Open Microsoft Powerpoint,Choose/Insert a Table type Slide(No.4), then double click to add a...
''# ...Table(3 Cols & 2 Rows) then rename the Table to "Table1", Save and Close the Presentation
''# Open Microsoft Excel, add some test data to Sheet1(This example assumes that you have some data in...
''# ... Rows 1,2 and Columns 1,2,3)
''# Open VBA editor(Alt+F11),Insert a Module and Paste the following code in to the code window
''# Reference 'Microsoft Powerpoint Object Library' (VBA IDE-->tools-->references)
''# Change "strPresPath" with full path of the Powerpoint Presentation created earlier.
''# Change "strNewPresPath" to where you want to save the new Presnetation to be created later
''# Close VB Editor and run this Macro from Excel window(Alt+F8) 

Dim oPPTApp As PowerPoint.Application
Dim oPPTShape As PowerPoint.Shape
Dim oPPTFile As PowerPoint.Presentation
Dim SlideNum As Integer
Sub PPTableMacro()
    Dim strPresPath As String, strExcelFilePath As String, strNewPresPath As String
    strPresPath = "H:\PowerPoint\Presentation1.ppt"
    strNewPresPath = "H:\PowerPoint\new1.ppt"

    Set oPPTApp = CreateObject("PowerPoint.Application")
    oPPTApp.Visible = msoTrue
    Set oPPTFile = oPPTApp.Presentations.Open(strPresPath)
    SlideNum = 1
    oPPTFile.Slides(SlideNum).Select
    Set oPPTShape = oPPTFile.Slides(SlideNum).Shapes("Table1")

    Sheets("Sheet1").Activate
    with oPPTShape.Table
        .Cell(1, 1).Shape.TextFrame.TextRange.Text = Cells(1, 1).Text
        .Cell(1, 2).Shape.TextFrame.TextRange.Text = Cells(1, 2).Text
        .Cell(1, 3).Shape.TextFrame.TextRange.Text = Cells(1, 3).Text
        .Cell(2, 1).Shape.TextFrame.TextRange.Text = Cells(2, 1).Text
        .Cell(2, 2).Shape.TextFrame.TextRange.Text = Cells(2, 2).Text
        .Cell(2, 3).Shape.TextFrame.TextRange.Text = Cells(2, 3).Text
    end with 

    oPPTFile.SaveAs strNewPresPath
    oPPTFile.Close
    oPPTApp.Quit

    Set oPPTShape = Nothing
    Set oPPTFile = Nothing
    Set oPPTApp = Nothing

    MsgBox "Presentation Created", vbOKOnly + vbInformation
End Sub

and here is some extra help on Automating Powerpoint with VBAand http://www.mahipalreddy.com/vba/ppvba.htm

这里有一些关于使用 VBAhttp://www.mahipalreddy.com/vba/ppvba.htm 自动化 Powerpoint 的额外帮助

to get the names of individual slides try something like the below:

要获取单个幻灯片的名称,请尝试以下操作:

Dim oSlide As Slide

For Each oSlide In ActiveWindow.Presentation.Slides
    Debug.Print oSlide.Name
Next
End Sub

that should powerpoint you in the right direction ! (so droll)

这应该指引你朝着正确的方向前进!(太逗了)

Philip

菲利普