title: Exporting Excel Range as image (VB.NET)
body: <p>I have a working excel vba macro that does what I want from <a href="http://snipplr.com/view/54755/" rel="nofollow">here</a> and I am trying to convert it to VB.NET.</p>
  
  <p>The code from VBA:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>What this does is take an excel range and then put it into a chart that is a copy of the range and save it as a JPG.</p>
  
  <p>Here is my most recent attempt at making it VB.NET:</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>I am running into trouble converting the <code>ActiveChart.Paste</code> method. I can't get it to work in VB.NET. It either throws an error or It just leaves an empty box when I do it in VB.NET (if I add <code>.chart</code> before the paste it runs, but doesn't paste any values), but in VBA it fills in the values of interest. I have tried creating a chart object, but that did not seem to work either. </p>
  
  <p>I feel like I am close to having it sorted out, but I can't quite get it. I suppose I could leave it as a VBA macro and call it from VB.NET, but that seems absurd on some level. Any help would be greatly appreciated. I am also open to different approaches, its just this is the thing I came across that worked well in VBA, so I figured it was a good starting point.</p>
  
  <p>Thanks as always!</p>