【问题标题】:How to open a powerpoint chart backend sheet in new instance如何在新实例中打开 powerpoint 图表后端工作表
【发布时间】:2013-10-21 17:04:16
【问题描述】:

我正在使用 vba 更新一个 powerpoint 演示文稿。 ppt中有30个图表,我将把一些excel表中的数据处理到powerpoint图表的后端excel表中。但每次我这样做时,我都可以看到新的 Excel 工作表在任务栏中打开。即使它们没有显示在整个屏幕上

 Set CExcel = New Excel.Application
CExcel.Visible = False
Set CWB2 = CExcel.Workbooks.Open(PPPres.Slides(lngSldNo).Shape(strChartName).Chart.ChartData.Workbook)

这是我正在使用的代码,但它给我的错误是您无法打开这样的文件 你能告诉我如何在新实例中打开 powerpoint 图表的后端工作表而不显示文件及其选项卡

提前致谢

【问题讨论】:

  • 每次打开 Excel 应用程序的新实例时,您的代码的第一行都会打开。您只需添加CExcel.Visible = true 即可在您的屏幕上看到它们。但是,我知道这不是问题。 您从哪个应用程序运行您的代码 - Excel 或 PP? 您的代码的另一部分是什么 - 从开头多几行,在您现在呈现的内容旁边还有几行。
  • 我正在使用 Excel-VBA。我只想通过一个新实例打开一个 powerpoint 图表后端 excel 表,这样我就可以使它不可见。
  • 代码的其他部分呢?

标签: vba excel powerpoint


【解决方案1】:

Workbooks.Open 方法的第一个参数是一个包含文件路径的字符串,例如

Sub Test()
Dim EAPP As Excel.Application, EWB As Excel.Workbook

    Set EAPP = New Excel.Application
    Set EWB = EAPP.Workbooks.Open("C:\Users\Myself\Desktop\MyFile.xlsx")

    Set EWB = Nothing
    Set EAPP = Nothing
End Sub

不确定您的论点是否传达了这一点。 Application.Visible 属性默认为False,因此无需显式设置。我猜你已经创建了对 Excel 对象库的引用,以利用 Powerpoint 中的 Excel 对象进行早期绑定。

【讨论】:

  • 和我打开一些excel表格的方法一样,但是powerpoint图表后端表格没有位置,所以我不能给出位置
【解决方案2】:

逻辑:

  1. 获取具有Chart 对象的形状
  2. 获取您的Chart 对象,然后获取Chartdata
  3. 启动图表并设置您的工作簿和 Excel 应用程序对象

假设:

出于演示目的,我假设以下内容

  1. 演示文稿中的Slide1 有一种形状,即图表
  2. 我已经添加了这两个场景。即Automating Excel from PowerPointAutomating PowerPoint from Excel

这是你正在尝试的吗?

代码:从 PowerPoint 自动化 Excel

Option Explicit

Sub Sample()
    '~~> Excel Objects
    Dim oXlApp As Object, oXlWb As Object, oXlSheet As Object
    '~~~> Powerpoint objects
    Dim oPPChart As Chart
    Dim oPPChartData As ChartData

    '~~> Working with shape1
    With ActivePresentation.Slides(1).Shapes(1)
        If .HasChart Then
            Set oPPChart = .Chart
            Set oPPChartData = oPPChart.ChartData

            oPPChartData.Activate

            '~~> Set your Excel objects here
            Set oXlWb = oPPChartData.Workbook
            Set oXlApp = oXlWb.Parent

            oXlApp.Visible = False

            Debug.Print oXlApp.Name
            Debug.Print oXlWb.Name


            '
            '~~> Rest of your code
            '     
        End If
    End With

    '~~> Close And Cleanup
    oXlWb.Close False
    oXlApp.Quit

    Set oXlSheet = Nothing
    Set oXlWb = Nothing
    Set oXlApp = Nothing
End Sub

代码:从 Excel 自动化 PowerPoint

Option Explicit

Sub Sample()
    '~~> Excel Objects
    Dim oXlApp As Application, oXlWb As Workbook, oXlSheet As Worksheet

    '~~~> Powerpoint objects
    Dim oPPApp As Object, oPPprsn As Object, oPPSlide As Object
    Dim oPPChart As Object, oPPChartData As Object

    Application.ScreenUpdating = False

    '~~> Establish a Popwerpoint application object
    On Error Resume Next
    Set oPPApp = GetObject(, "PowerPoint.Application")

    '~~> If not found then create new instance
    If Err.Number <> 0 Then
        Set oPPApp = CreateObject("PowerPoint.Application")
    End If
    Err.Clear
    On Error GoTo 0

    '~~> open relevant powerpoint file
    Set oPPprsn = oPPApp.Presentations.Open("C:\Presentation1.pptx")
    Set oPPSlide = oPPprsn.Slides(1)

    '~~> Working with shape1
    With oPPSlide.Shapes(1)
        If .HasChart Then
            Set oPPChart = .Chart
            Set oPPChartData = oPPChart.ChartData

            oPPChartData.Activate

            '~~> Set your Excel objects here
            Set oXlWb = oPPChartData.Workbook
            Set oXlApp = oXlWb.Parent

            '~~> This is required if powerpoint chartdata
            '~~> opens in the same instance of this excel
            oXlWb.Windows(1).Visible = False
            'oXlApp.Visible = False

            Debug.Print oXlApp.Name
            Debug.Print oXlWb.Name

            '
            '~~> Rest of your code
            '
        End If
    End With

    oXlWb.Windows(1).Visible = True

    '~~> Close And Cleanup
    oXlWb.Close False

    oPPprsn.Close
    oPPApp.Quit

    Application.ScreenUpdating = True

    Set oPPChartData = Nothing
    Set oPPChart = Nothing
    Set oPPChartData = Nothing
    Set oPPSlide = Nothing
    Set oPPprsn = Nothing
    Set oPPApp = Nothing
End Sub

【讨论】:

  • 我确实尝试过这种方法,但工作簿仍在后台打开。我可以看到在任务栏中打开了新的 excel 文件。(一旦开始打开图表后端 excel 文件,excel 选项卡就会开始闪烁)
  • 请刷新页面并尝试第二种方法Automating PowerPoint from Excel张贴。
  • 第二个代码可以正常访问数据,但我仍然在后台打开了一个新窗口
猜你喜欢
  • 2018-12-24
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多