1

I have created an .xlsx file using the OpenXML framework and I am embedding it into a PowerPoint slide (also created using OpenXML) the problem is that I need a placeholder image for the OleObject the Excel sits in on the PowerPoint slide - i can use a standard placeholder, but this looks bad.

So I was hoping (most likely using Interop) to find a way of opening the .xlsx file and exporting the relevant cells as a bitmap (.jpg or .png ideally) which I can then feed into my OleObject as the placeholder image.

I came across this: http://csharp.net-informations.com/excel/csharp-excel-chart-export.htm

Which does what I am trying to do, but on a Chart object, so if someone knows a way to export cells (ideally a selection, rather than the whole worksheet) to a bitmap, that would be great!

Thanks,

Mike

2 Answers2

1

An Excel Range object has a CopyPicture method that does what you're looking for.

JP has a VBA example posted that you should give you what you need.

http://www.jpsoftwaretech.com/export-excel-range-to-a-picture-file/

Jesse
  • 1,937
  • 3
  • 19
  • 28
0

if someone knows a way to export cells (ideally a selection, rather than the whole worksheet) to a bitmap...

Formatted selection with 'All Borders'

learnt from How can I export from Excel to an image with specific size (or aspect ratio)?

Sub ExportExcelSelectionImage()

    exportpath = "C:\Users\nares\Desktop\ExcelScreenShot"
    Set xWs = ActiveWorkbook.ActiveSheet
    Set mySel = Selection

    'Captures current window view
    sView = ActiveWindow.View

    'Sets the current view to normal so there are no "Page X" overlays on the image
    ActiveWindow.View = xlNormalView

    'Temporarily disable screen updating
    Application.ScreenUpdating = False

    mySel.CopyPicture xlPrinter

    Set chartobj = xWs.ChartObjects.Add(0, 0, mySel.Width, mySel.Height)
    chartobj.Activate
    chartobj.Chart.Paste
    chartobj.Chart.Export exportpath & ".png", "png" ' & TimeValue(Now()) & ".png", "png"
    chartobj.Delete

    'Returns to the previous view
    ActiveWindow.View = sView

    'Re-enables screen updating
    Application.ScreenUpdating = True

End Sub

Image >>>>>>>>>>>

enter image description here

Naresh
  • 2,984
  • 2
  • 9
  • 15