1

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

提前致谢

4

2 回答 2

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

于 2013-10-21T07:25:45.273 回答
0

逻辑:

  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
于 2013-10-21T08:26:17.693 回答