如何在新实例中打开powerpoint图表后端工作表

时间:2013-10-21 05:59:43

标签: excel vba excel-vba powerpoint

我正在使用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图表的后端表并且不显示文件及其选项卡

提前致谢

2 个答案:

答案 0 :(得分:0)

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对象进行早期绑定。

答案 1 :(得分:0)

<强>逻辑:

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

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

      演示文稿中的
    1. Slide1有一个形状,即图表
    2. 我添加了两个方案。即Automating Excel from PowerPointAutomating PowerPoint from Excel
    3. 这是你在尝试的吗?

      代码:从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