Export Excel as Image (VB.NET)

I have an excel vba working macro that does what I want from here and am trying to convert it to VB.NET.

Code from VBA:

Sub bah()
''' Set Range you want to export to file
    Dim rgExp As Range: Set rgExp = Range("B2:C6")
    ''' Copy range as picture onto Clipboard
    rgExp.CopyPicture Appearance:=xlScreen, format:=xlBitmap
    ''' Create an empty chart with exact size of range copied
    With ActiveSheet.ChartObjects.Add(Left:=rgExp.Left, Top:=rgExp.Top, _
    Width:=rgExp.Width, Height:=rgExp.Height)
    .Name = "ChartVolumeMetricsDevEXPORT"
    .Activate
    End With
    ''' Paste into chart area, export to file, delete chart.
    ActiveChart.Paste
   ActiveSheet.ChartObjects("ChartVolumeMetricsDevEXPORT").Chart.Export "C:\Users\ajohnson\Desktop\workdamnit.jpg"
ActiveSheet.ChartObjects("ChartVolumeMetricsDevEXPORT").Delete
End Sub

What this means is to take the excel range and then put it in the chart, which is a copy of the range and save it as a jpg.

Here is my last attempt to do this VB.NET:

Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim xlRange As Excel.Range
xlWorkBook = xlApp.Workbooks.Open("C:\test.xlsx")
xlWorkSheet = xlWorkBook.Sheets("Sheet1")


 xlRange = xlWorkSheet.Range("B2:C6")
        With xlWorkSheet.ChartObjects.add(xlRange.Left, xlRange.Top, xlRange.Width, xlRange.Height)
            .name = "Chart1"
            .activate()
        End With
xlWorkSheet.ChartObjects("Chart1").Paste()
        xlWorkSheet.ChartObjects("Chart1").chart.export(Filename:="C:\Users\ajohnson\Desktop\saveit.jpg")
        xlWorkSheet.ChartObjects("Chart1").delete()

I'm having trouble converting a method ActiveChart.Paste. I can not get it to work in VB.NET. This either generates an error, or simply leaves an empty box when I do this in VB.NET (if I add .chartbefore it is inserted, but does not insert any values), but in VBA it fills the values ​​of interest. I tried to create a chart object, but that didn't work either.

, , , . , VBA VB.NET, - . . , , , VBA, , .

, !

+2
2

MSDN , . , chartobject , , , :

  xlRange = xlWorkSheet.Range("B2:C6")
    xlRange.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlPicture)
    Dim oChtobj As Excel.ChartObject = xlWorkSheet.ChartObjects.add(xlRange.Left, xlRange.Top, xlRange.Width, xlRange.Height)
    Dim oCht As Excel.Chart
    oCht = oChtobj.Chart
    oCht.Paste()
    oCht.Export(Filename:="C:\saveit.jpg")
    oChtobj.Delete()

, ( , , ), , ​​ , , , , - . excel jpg - (, Outlook, , ), .

+5

# Activate() COMException

Excel.Range xlRange = xlWorkSheet.Range("B2:C6");

range.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlPicture);
Excel.ChartObject chartObj = myWorksheet.ChartObjects().Add(range.Left, range.Top, range.Width, range.Height);

chartObj.Activate();  // Don't Forget!

Excel.Chart chart = chartObj.Chart;
chart.Paste();
chart.Export(@"C:\image.png");

chartObj.Delete();
+2

All Articles