Search code examples
c#exceloffice-interopexcel-interop

How to get charts from Excel and add them to Word using Microsoft.Office.Interop?


I tried to export charts as images with Microsoft.Office.Interop.Excel.Chart.Export() method, but the quality of the images is bad (best result is with the PNG format). Is there any way how to set the resolution of images during export using only Microsoft.Office.Interop?

On the other hand, my coworker is using VBA and he can export charts as .emf files directly from Excel (using WinAPI), so I tried including them using the Microsoft.Office.Interop.Word.InlineShapes.AddPicture() method, but it seems that it cannot include .emf files directly.

Is there a different and maybe better way to do this?

UPDATE: I don't know why I thought that it is not possible include .emf file, but still I want to export hi-res images.


Solution

  • It's ugly but how about enlarging the chart before you export it as such:

        myChart.ScaleWidth(2, msoFalse, msoScaleFromTopLeft);
        myChart.ScaleHeight(2, msoFalse, msoScaleFromTopLeft); 
    

    and then after export, if you need to, shrink it back by replacing 2 with .5...

    When you import it into Word, you can perform ScaleWidth and ScaleHeight on the shape to get it the size you need.