有没有办法告诉PowerPoint在创建图表时不要打开Excel?

时间:2022-08-25 13:05:57

Slide.Shapes.AddChart() automatically opens Excel. Even if I quickly do Chart.ChartData.Workbook.Application.Visible = false, it still shows a little while. This makes automating chart creation error-prone as the user has to try not to touch the Excel applications that keeps popping up.

Slide.Shapes.AddChart()自动打开Excel。即使我快速执行Chart.ChartData.Workbook.Application.Visible = false,它仍会显示一段时间。这使得自动化图表创建容易出错,因为用户必须尽量不要触摸不断弹出的Excel应用程序。

Opening a presentation with WithWindow = false will still open Excel when creating new charts.

在创建新图表时,使用WithWindow = false打开演示文稿仍将打开Excel。

3 个解决方案

#1


8  

This behavior is "by design" and Microsoft is not interested in changing. This is the way the UI functions.

这种行为是“按设计”,微软对改变不感兴趣。这是UI的功能。

What you could do would be to create the chart in Excel (using either the interop or OpenXML), then import (insert) that file into PowerPoint.

你可以做的是在Excel中创建图表(使用interop或OpenXML),然后将该文件导入(插入)到PowerPoint中。

Check this link from MSDN

从MSDN检查此链接

#2


1  

Here's a possible work around.

这是一个可能的工作。

Sub ChartExample()
Dim s As Shape
Set s = Application.Presentations(1).Slides(1).Shapes.AddOLEObject(ClassName:="Excel.Chart")
End Sub

You would then manipulate the chart you added via the s.OLEFormat.Object. I only experimented slightly, but it does not open an external Excel application and I did not see any extreme flickering unless I activated the object. A trade off is that at least in Powerpoint 2010, you need to convert it to use all of the features. If this doesn't work you could always try web components.

然后,您将通过s.OLEFormat.Object操作您添加的图表。我只是略微尝试,但它没有打开外部Excel应用程序,除非我激活了对象,否则我没有看到任何极端闪烁。权衡是至少在Powerpoint 2010中,您需要将其转换为使用所有功能。如果这不起作用,您可以随时尝试Web组件。

Edit: I don't understand why this method causes a problem, but to try to assist further here is a little more code that shows actually manipulating the object. This was written with objects instead of workbooks etc, so that no references need to be made. It only demands the user have Excel on their machine.

编辑:我不明白为什么这个方法会导致问题,但是在这里尝试进一步提供帮助是一个更多的代码,显示实际操作对象。这是用对象而不是工作簿等编写的,因此不需要进行引用。它只要求用户在他们的机器上安装Excel。

Option Explicit
Const xlcolumns = 2
Sub ChartExample()
Dim s As Shape
Dim wb As Object, chart As Object, data As Object
Set s = Application.Presentations(1).Slides(1).Shapes.AddOLEObject(ClassName:="Excel.Chart")
Set wb = s.OLEFormat.Object
Set chart = wb.Sheets(1)
Set data = wb.Sheets(2)
'Set the range for the chart data
chart.setsourcedata Source:=data.Range("A1:C7"), PlotBy:= _
        xlcolumns
'Update data values for the chart
data.Range("B1").Value = "Column Label 1"
data.Range("C1").Value = "Column Label 2"
data.Range("A2:C7").clearcontents
data.Range("A2").Value = "Row Label"
data.Range("B2").Value = 7
data.Range("C2").Value = 11
End Sub

#3


0  

I would suggest another methdology to over come the same.

我建议采用另一种方法来实现同样的方法。

  1. In the powerpoint VBA add refrences to "Microsoft Excel 12.0 Object Library"

    在powerpoint VBA中添加对“Microsoft Excel 12.0对象库”的引用

  2. Ensure the user that for this operation none of the excel must be open via yuser form popup before the operation.

    确保用户在操作之前,不必通过yuser表单弹出打开此操作。

  3. In the VBA create an excel and set its parameters in the following code

    在VBA中创建一个excel并在以下代码中设置其参数

  4. Add the powerpoint chart, the user wouldnt be able to see the opening of the underlying excel sheet upon adding chart excet the excel tabs which can be controled via code.

    添加powerpoint图表,除了可以通过代码控制的excel选项卡之外,用户无法在添加图表时看到底层Excel工作表的打开。

Sample Code:

示例代码:

Option Explicit

Sub AddExcelChartSample()

    Dim xlApp As Excel.Application, xlWkbk As Excel.Workbook

    Dim pres As PowerPoint.Presentation, sld As PowerPoint.Slide, iCount As Integer, chtShape As PowerPoint.Shape

    'Open up the excel instance and set parameters


    Set xlApp = New Excel.Application
    With xlApp
        .WindowState = xlNormal
        .Top = -1000
        .Left = -1000
        .Height = 0
        .Width = 0
    End With


    Set sld = PowerPoint.ActiveWindow.View.Slide



    For iCount = 1 To 10

        Set chtShape = sld.Shapes.AddChart(xlLine)
        Set xlWkbk = chtShape.Chart.ChartData.Workbook
        With xlWkbk

            .Sheets(1).Range("A2").Value = "Test 1"
            .Sheets(1).Range("A3").Value = "Test 2"
            .Sheets(1).Range("A4").Value = "Test 3"

        End With

        chtShape.Chart.Refresh

        xlWkbk.Close False


    Next iCount


    xlApp.Quit

End Sub

#1


8  

This behavior is "by design" and Microsoft is not interested in changing. This is the way the UI functions.

这种行为是“按设计”,微软对改变不感兴趣。这是UI的功能。

What you could do would be to create the chart in Excel (using either the interop or OpenXML), then import (insert) that file into PowerPoint.

你可以做的是在Excel中创建图表(使用interop或OpenXML),然后将该文件导入(插入)到PowerPoint中。

Check this link from MSDN

从MSDN检查此链接

#2


1  

Here's a possible work around.

这是一个可能的工作。

Sub ChartExample()
Dim s As Shape
Set s = Application.Presentations(1).Slides(1).Shapes.AddOLEObject(ClassName:="Excel.Chart")
End Sub

You would then manipulate the chart you added via the s.OLEFormat.Object. I only experimented slightly, but it does not open an external Excel application and I did not see any extreme flickering unless I activated the object. A trade off is that at least in Powerpoint 2010, you need to convert it to use all of the features. If this doesn't work you could always try web components.

然后,您将通过s.OLEFormat.Object操作您添加的图表。我只是略微尝试,但它没有打开外部Excel应用程序,除非我激活了对象,否则我没有看到任何极端闪烁。权衡是至少在Powerpoint 2010中,您需要将其转换为使用所有功能。如果这不起作用,您可以随时尝试Web组件。

Edit: I don't understand why this method causes a problem, but to try to assist further here is a little more code that shows actually manipulating the object. This was written with objects instead of workbooks etc, so that no references need to be made. It only demands the user have Excel on their machine.

编辑:我不明白为什么这个方法会导致问题,但是在这里尝试进一步提供帮助是一个更多的代码,显示实际操作对象。这是用对象而不是工作簿等编写的,因此不需要进行引用。它只要求用户在他们的机器上安装Excel。

Option Explicit
Const xlcolumns = 2
Sub ChartExample()
Dim s As Shape
Dim wb As Object, chart As Object, data As Object
Set s = Application.Presentations(1).Slides(1).Shapes.AddOLEObject(ClassName:="Excel.Chart")
Set wb = s.OLEFormat.Object
Set chart = wb.Sheets(1)
Set data = wb.Sheets(2)
'Set the range for the chart data
chart.setsourcedata Source:=data.Range("A1:C7"), PlotBy:= _
        xlcolumns
'Update data values for the chart
data.Range("B1").Value = "Column Label 1"
data.Range("C1").Value = "Column Label 2"
data.Range("A2:C7").clearcontents
data.Range("A2").Value = "Row Label"
data.Range("B2").Value = 7
data.Range("C2").Value = 11
End Sub

#3


0  

I would suggest another methdology to over come the same.

我建议采用另一种方法来实现同样的方法。

  1. In the powerpoint VBA add refrences to "Microsoft Excel 12.0 Object Library"

    在powerpoint VBA中添加对“Microsoft Excel 12.0对象库”的引用

  2. Ensure the user that for this operation none of the excel must be open via yuser form popup before the operation.

    确保用户在操作之前,不必通过yuser表单弹出打开此操作。

  3. In the VBA create an excel and set its parameters in the following code

    在VBA中创建一个excel并在以下代码中设置其参数

  4. Add the powerpoint chart, the user wouldnt be able to see the opening of the underlying excel sheet upon adding chart excet the excel tabs which can be controled via code.

    添加powerpoint图表,除了可以通过代码控制的excel选项卡之外,用户无法在添加图表时看到底层Excel工作表的打开。

Sample Code:

示例代码:

Option Explicit

Sub AddExcelChartSample()

    Dim xlApp As Excel.Application, xlWkbk As Excel.Workbook

    Dim pres As PowerPoint.Presentation, sld As PowerPoint.Slide, iCount As Integer, chtShape As PowerPoint.Shape

    'Open up the excel instance and set parameters


    Set xlApp = New Excel.Application
    With xlApp
        .WindowState = xlNormal
        .Top = -1000
        .Left = -1000
        .Height = 0
        .Width = 0
    End With


    Set sld = PowerPoint.ActiveWindow.View.Slide



    For iCount = 1 To 10

        Set chtShape = sld.Shapes.AddChart(xlLine)
        Set xlWkbk = chtShape.Chart.ChartData.Workbook
        With xlWkbk

            .Sheets(1).Range("A2").Value = "Test 1"
            .Sheets(1).Range("A3").Value = "Test 2"
            .Sheets(1).Range("A4").Value = "Test 3"

        End With

        chtShape.Chart.Refresh

        xlWkbk.Close False


    Next iCount


    xlApp.Quit

End Sub