Export Excel charts to wmf or emf?

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

The code works if you export to GIF, but not with WMF as the file name.

It works:

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

But

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

does not give an error:

Runtime Error "1004": Detected or Object Defined Error

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

I wonder if there might be a way to register a WMF filter for Excel, but I'm not sure how to do this. Please help! Thanks.

+4
source share
4 answers

This copy, save method worked for me,. I put it in 3 sections (declarations, saves it as an EMF function and a section for selecting / copying / calling functions):

* I found in this article , which describes in detail how to save to EMF, and then learned a little how to use ActiveChart instead of arbitrary selection.

First do a couple of 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 an emf function (using CopyEnhMetaFileA and DeleteEnhMetaFile is explained in the article):

 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 section for selecting, copying and calling functions:

 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 
+7
source

Andy answered it in detail here .

+1
source

Through this forum entry, you can find the wonderful Stephen Bullen Excel Page and download the PastePicture utility , which shows how you can export to WMF format.

It basically copies the chart, pastes it to the clipboard as an image, and saves its contents to a WMF file in a few lines of code.

+1
source

Funny, this poster had the same problem on this forum here, got into this interesting ExcelForum article in relation to this.

Hope this helps you, Best Regards, Tom.

0
source

All Articles