vba Excel 或 Powerpoint 2007/2010 中的滚动图表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10224002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:52:36  来源:igfitidea点击:

Scrolling chart in Excel or Powerpoint 2007/2010

excelexcel-vbaactivexpowerpointpowerpoint-vbavba

提问by tumchaaditya

I have a chart which has large number of points on X-axis.(e.g. ECG).

我有一个在 X 轴上有大量点的图表。(例如心电图)。

How do I put this chart as a horizontally scrollableobject in Powerpoint 2007/2010? If i just paste it, it gets resized to fit width and becomes unreadable.

如何在 Powerpoint 2007/2010 中将此图表作为可水平滚动的对象?如果我只是粘贴它,它会调整大小以适应宽度并且变得不可读。

I want to maintain the height by including a horizontal scroll bar for chart.

我想通过包含图表的水平滚动条来保持高度。

采纳答案by Siddharth Rout

I am not sureif that can be done. Having said that I can give you an interesting alternative. :)

不确定这是否可以做到。话虽如此,我可以给你一个有趣的选择。:)

Let's say our chart looks like this in Excel

假设我们的图表在 Excel 中看起来像这样

enter image description here

在此处输入图片说明

Right click on the chart and click on Copy. Open Ms Paint and paste that picture. Save that picture, say as C:\MyChart.Jpg

右键单击图表并单击Copy。打开 Ms Paint 并粘贴该图片。保存那张图片,说为C:\MyChart.Jpg

Next open MS Powerpoint and navigate to the DeveloperTab. (See Snapshot) In the developer tab, click on additional controls button and select "Microsoft Web Browser" and insert that control in your respective slide. Size it accordingly. Also place a Command Button. Name it Show Chartor anything else what you feel is right :)

接下来打开 MS Powerpoint 并导航到Developer选项卡。(请参阅快照)在开发人员选项卡中,单击其他控件按钮并选择“Microsoft Web 浏览器”并将该控件插入您各自的幻灯片中。相应地调整大小。还放置一个命令按钮。命名它Show Chart或其他任何您认为正确的东西:)

enter image description here

在此处输入图片说明

Double click on the command button and paste this code there

双击命令按钮并将此代码粘贴到那里

Private Sub CommandButton1_Click()
    WebBrowser1.Navigate "C:\MyChart.jpg"
End Sub

Now press F5to run the presentation. Your screen will look like this.

现在按F5运行演示。您的屏幕将如下所示。

enter image description here

在此处输入图片说明

When you press the command button, you will get what you wanted :)

当你按下命令按钮时,你会得到你想要的:)

enter image description here

在此处输入图片说明

DISADVANTAGES OF THIS METHOD

这种方法的缺点

1) You cannot edit the chart in MS Powerpoint. You will have to do that in Excel and repeat the entire procedure to save it as an image.

1) 您不能在 MS Powerpoint 中编辑图表。您必须在 Excel 中执行此操作并重复整个过程以将其另存为图像。

2) You cannot distribute your PPT. You will have to send the image separately with the PPT and also you will have to change the command button code (assuming that PPT and image stay in the same folder) to

2) 您不能分发您的 PPT。您必须将图像与 PPT 分开发送,并且您还必须将命令按钮代码(假设 PPT 和图像保留在同一文件夹中)更改为

Private Sub CommandButton1_Click()
    WebBrowser1.Navigate ActivePresentation.Path & "\MyChart.jpg"
End Sub

OR

或者

You will have to embed the xls file in the ppt and write a complex code to extract the chart from the excel file and save it to the users temp directory. You can then use that image in the Webbrowser1

您必须将 xls 文件嵌入 ppt 并编写复杂的代码以从 excel 文件中提取图表并将其保存给用户temp directory。然后,您可以在Webbrowser1

回答by Siddharth Rout

Since this approached the problem from a different angle, I am posting an entirely new answer :)

由于这是从不同角度解决问题,我发布了一个全新的答案:)

This method follows up on my last comment

这个方法是我上次评论的后续

You will have to embed the xls file in the ppt and write a complex code to extract the chart from the excel file and save it to the users temp directory. You can then use that image in the Webbrowser1

您必须将 xls 文件嵌入 ppt 并编写复杂的代码以从 excel 文件中提取图表并将其保存到用户临时目录中。然后您可以在 Webbrowser1 中使用该图像

FOLLOWUP

跟进

@Siddharth Rout: Thanks! But, redistribution is exactly what I want. And It's not possible for me to have the image as a separate file(I am to give this to my customer).

@Siddharth Rout:谢谢!但是,重新分配正是我想要的。而且我不可能将图像作为单独的文件(我要将其提供给我的客户)。

Your worries should not worry you anymore ;)

你的担心不应该再让你担心了 ;)

DESIGN MODE

设计模式

In powerpoint slide insert the Excel Object which has the chart. Your slide should look like this

在powerpoint幻灯片中插入带有图表的Excel对象。您的幻灯片应如下所示

enter image description here

在此处输入图片说明

Now as shown in my other answer, insert a "Microsoft Web Browser" and "Command Button". Place the Webbrowser above the Excel Object to hide it.

现在如我的其他答案所示,插入“Microsoft Web 浏览器”和“命令按钮”。将 Webbrowser 放在 Excel 对象上方以隐藏它。

Your screen should look like this now.

你的屏幕现在应该是这样的。

enter image description here

在此处输入图片说明

Paste this code and run your presentation... That's it... Really ;)

粘贴此代码并运行您的演示文稿......就是这样......真的;)

CODE

代码

Option Explicit

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Const MAX_PATH As Long = 260

Dim ImageFile As String

Private Sub CommandButton1_Click()
    ExtractToTemp
    WebBrowser1.Navigate ImageFile
End Sub

Sub ExtractToTemp()
    Dim oSl As PowerPoint.Slide
    Dim oSh As PowerPoint.Shape

    Dim oXLApp As Object, oXLWB As Object, oXLSht As Object
    Dim mychart As Object

    Set oSl = ActivePresentation.Slides(1)

    Set oSh = oSl.Shapes(1)

    With oSh.OLEFormat.Object.Sheets(1)
        .Shapes(1).Copy
    End With

    '~~> Establish an EXCEL application object
    On Error Resume Next
    Set oXLApp = GetObject(, "Excel.Application")

    If Err.Number <> 0 Then
        Set oXLApp = CreateObject("Excel.Application")
    End If
    Err.Clear
    On Error GoTo 0

    oXLApp.Visible = False

    '~~> Open the relevant file
    Set oXLWB = oXLApp.Workbooks.Add
    Set oXLSht = oXLWB.Worksheets(1)

    oXLSht.Paste

    '~~> Save Picture Object
    ImageFile = TempPath & "Tester.jpg"

    If Len(Dir(ImageFile)) > 0 Then Kill ImageFile

    Set mychart = oXLSht.ChartObjects(1).Chart
    mychart.Export FileName:=ImageFile, FilterName:="jpg"

    '~~> Wait till the file is saved
    Do
        If FileExists(ImageFile) = True Then Exit Do
        DoEvents
    Loop

    '~~> Clean Up And Close Excel
    oXLWB.Close SaveChanges:=False
    oXLApp.Quit

    Set oXLWB = Nothing
    Set oXLApp = Nothing
End Sub

'~~> Get User's TempPath
Function TempPath() As String
    TempPath = String$(MAX_PATH, Chr$(0))
    GetTempPath MAX_PATH, TempPath
    TempPath = Replace(TempPath, Chr$(0), "")
End Function

'~~> Function tot check if file exists
Public Function FileExists(strFullPath As String) As Boolean
    On Error GoTo Whoa
    If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileExists = True
Whoa:
    On Error GoTo 0
End Function

SAMPLE FILE FOR TESTING: Please download this file and run the presentation. If you see the chart after clicking the button then it means it works :)

用于测试的示例文件:请下载此文件并运行演示文稿。如果您在单击按钮后看到图表,则表示它有效:)

https://skydrive.live.com/redir.aspx?cid=cdd3f8abe20bbe3b&resid=CDD3F8ABE20BBE3B!162&parid=root

https://skydrive.live.com/redir.aspx?cid=cdd3f8abe20bbe3b&resid=CDD3F8ABE20BBE3B!162&parid=root