简体   繁体   English

使用 VBA 替换 Excel 中的 Powerpoint 数据

[英]Replacing Powerpoint data from Excel using VBA

I have some "tables" in excel that I export to a powerpoint every week (I say "tables" because they are actually just ranges of cells and not actually tables).我在 excel 中有一些“表格”,我每周都会导出到 powerpoint(我说“表格”,因为它们实际上只是单元格范围,而不是表格)。 Right now I have a macro that deletes all of the shapes from the powerpoint each week, and then a different macro to export the tables to the powerpoint.现在我有一个宏,可以每周从 powerpoint 中删除所有形状,然后使用不同的宏将表格导出到 powerpoint。 The issue with this is that it deletes everything from the powerpoint, including the titles and comments on the slides.这样做的问题是它会删除幻灯片中的所有内容,包括幻灯片上的标题和评论。 So now I am trying to write code that just replaces the previous weeks tables.所以现在我正在尝试编写代码来替换前几周的表格。 So far this is the code I have, however it does not work when I get to到目前为止,这是我拥有的代码,但是当我到达时它不起作用

pptpress.Slides(1).Shapes("Picture 8").Table.Cell(r, c).Shape.TextFrame.TextRange = Sheet2.Cells(r, c).Value

I assume it doesn't work because the range of cells are not actually tables?我认为它不起作用,因为单元格范围实际上不是表格? Anyways, does anyone have any advice on how to replace previous weeks data?无论如何,有人对如何替换前几周的数据有任何建议吗? I'm struggling over here我在这里挣扎

Sub UpdateTables()

    Dim pptapp As New PowerPoint.Application
    Dim pptpress As PowerPoint.Presentation
    
    Dim left As Double
    Dim top As Double
    Dim height As Double
    Dim width As Double
    
    Dim r As Integer
    Dim c As Integer
    
    Set pptpress = pptapp.Presentations.Open("xxxxxx")

    'Table update
    For r = 4 To 30
    
     For c = 2 To 12
     
        pptpress.Slides(1).Shapes("Picture 8").Table.Cell(r, c).Shape.TextFrame.TextRange = Sheet2.Cells(r, c).Value
        
    Next c
   Next r


End Sub

I assume it doesn't work because the range of cells are not actually tables?我认为它不起作用,因为单元格范围实际上不是表格?

Yes.是的。 The fact that the shape's name is "Picture 8" is a give-away also.形状的名称是“图片 8”这一事实也是一个赠品。 In all likelihood, it's an EMF/WMF/etc picture that's been pasted into the PPT slide.很可能,这是一张粘贴到 PPT 幻灯片中的 EMF/WMF/etc 图片。

As @Dan suggests, it might be simplest to insert a new Table shape and work with it.正如@Dan 所建议的,插入一个新的表格形状并使用它可能是最简单的。 Or if you have a particular table format you need to preserve, add a table manually to the slide, format it, then have your code put the data into it instead.或者,如果您需要保留特定的表格格式,请手动将表格添加到幻灯片中,对其进行格式化,然后让您的代码将数据放入其中。

In order to replace old shapes (or in your case: tables), you could use a marker or a timestamp to identify what needs to be deleted.为了替换旧形状(或在您的情况下:表格),您可以使用标记或时间戳来识别需要删除的内容。 A great way for this are tags within PowerPoint.一个很好的方法是 PowerPoint 中的标签 You can attach these to presentations, slides and shapes (maybe even more).您可以将这些附加到演示文稿、幻灯片和形状(甚至更多)。 They are invisible to the user but can be read / written via code (eg VBA).它们对用户不可见,但可以通过代码(例如 VBA)读取/写入。

This is what you need to look for: https://docs.microsoft.com/en-gb//office/vba/api/powerpoint.tags这是您需要查找的内容: https://docs.microsoft.com/en-gb//office/vba/api/powerpoint.tags

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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