vba Excel 将图表导出到 wmf 或 emf?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1791369/
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 11:02:42  来源:igfitidea点击:

Excel export chart to wmf or emf?

excelexcel-vbavba

提问by Alex Andronov

I am trying to export a chart from Excel to either the wmf or emf format.

我正在尝试将图表从 Excel 导出为 wmf 或 emf 格式。

The code works if you export to GIF but not with WMF as the filtername.

如果您导出为 GIF 但不使用 WMF 作为过滤器名称,则该代码有效。

This works:

这有效:

Chart.Export FileName:="current_sales.gif", FilterName:="GIF"

But

Chart.Export FileName:="current_sales.wmf", FilterName:="WMF"

fails giving the error:

失败给出错误:

Run-time error '1004': Application-defined or object-defined error

运行时错误“1004”:应用程序定义或对象定义错误

Powerpoint allows you to export to WMF. And I have "successfully" exported by copying the graph into Powerpoint and having Powerpoint export the image to WMF but there has to be an easier way - I hope.

Powerpoint 允许您导出到 WMF。我通过将图形复制到 Powerpoint 并让 Powerpoint 将图像导出到 WMF 来“成功”导出,但必须有更简单的方法 - 我希望。

I wonder if there may be a way of registering the WMF filter for Excel but I am unsure how to do this. Please help! Thanks.

我想知道是否有办法为 Excel 注册 WMF 过滤器,但我不确定如何执行此操作。请帮忙!谢谢。

回答by Dostee

This copy, save method worked for me,i put it into 3 sections (declarations, saves as EMF function, and the select/copy/function call section):

这个副本,保存方法对我有用我把它分成 3 个部分(声明,另存为 EMF 函数,以及选择/复制/函数调用部分):

*I found this articledetailing how to save to EMF then doctored it a bit to use the an ActiveChart instead of a arbitrary selection.

*我发现这篇文章详细介绍了如何保存到 EMF,然后对其进行了一些修改以使用 ActiveChart 而不是任意选择。

First off a couple declarations:

首先是几个声明:

Option Explicit

Private Declare Function OpenClipboard _
    Lib "user32" ( _
        ByVal hwnd As Long) _
As Long

Private Declare Function CloseClipboard Lib "user32" () As Long

Private Declare Function GetClipboardData _
    Lib "user32" ( _
        ByVal wFormat As Long) _
As Long

Private Declare Function EmptyClipboard Lib "user32" () As Long

'// CreateMetaFileA DeleteEnhMetaFile
Private Declare Function CopyEnhMetaFileA _
    Lib "gdi32" ( _
        ByVal hENHSrc As Long, _
        ByVal lpszFile As String) _
As Long

Private Declare Function DeleteEnhMetaFile _
    Lib "gdi32" ( _
        ByVal hemf As Long) _
As Long

This is the actual save as emf function (the use of CopyEnhMetaFileA and DeleteEnhMetaFile are explained in the article):

这是实际的另存为emf函数(CopyEnhMetaFileA和DeleteEnhMetaFile的使用在文章中有说明):

Public Function fnSaveAsEMF(strFileName As String) As Boolean
Const CF_ENHMETAFILE As Long = 14

Dim ReturnValue As Long

    OpenClipboard 0

    ReturnValue = CopyEnhMetaFileA(GetClipboardData(CF_ENHMETAFILE), strFileName)

    EmptyClipboard

    CloseClipboard

    '// Release resources to it eg You can now delete it if required
    '// or write over it. This is a MUST
    DeleteEnhMetaFile ReturnValue

    fnSaveAsEMF = (ReturnValue <> 0)

End Function

Then the select, copy, and function call section:

然后是选择、复制和函数调用部分:

Sub SaveIt()
Charts.Add
    ActiveChart.ChartArea.Select
    Selection.Copy
    If fnSaveAsEMF("C:\Excel001.emf") Then
        MsgBox "Saved", vbInformation
    Else
        MsgBox "NOT Saved!", vbCritical
    End If

End Sub

回答by Pablo Rodriguez

Through this forum entryyou can find the marvelous Stephen Bullen's Excel Pageand download the PastePicture utilitywhich shows how you can export to the WMF format.

通过此论坛条目,您可以找到出色的Stephen Bullen 的 Excel 页面并下载PastePicture 实用程序,该实用程序显示了如何导出为 WMF 格式。

It basically copies the chart, pastes it into the clipboard as a picture and saves its content into a WMF file in a few lines of code.

它基本上是复制图表,将其作为图片粘贴到剪贴板中,然后用几行代码将其内容保存到 WMF 文件中。

回答by caving

Andy has answered this in detail Here.

安迪在这里详细回答了这个问题。

回答by t0mm13b

Funny this, this poster had the exact same problem on this forumhere, Came across this interesting article at ExcelForumin relation to this.

有趣的是,这张海报在这个论坛上遇到了完全相同的问题,在ExcelForum 上看到了与此相关的这篇有趣的文章。

Hope this is of help to you, Best regards, Tom.

希望这对你有帮助,最好的问候,汤姆。