Exporting Picture to Gif - VBA to VB.Net

J

JP

Hi,
I'm trying to take a picture object in excel and export it to an image. I
found code to do this in VBA and I am trying to get it to work in VB.Net.
Below I have the original VBA along with my VB.Net code. In VB.net I keep
getting the error "Specified Cast is Not Valid." Any help? Suggestions?

Thanks,
JP

Here's the VBA code I found (using newgroup search):

Sub ExportPicture()
'
' Create blank chart to export picture through.
'
Dim objHolder As ChartObject
Dim objTemp As Object
Dim sngWidth As Single
Dim sngHeight As Single
Dim MyShape As String
Dim Filename As String
Dim shtTemp As Worksheet

Set shtTemp = ActiveSheet
Filename = "C:\temp\test.gif"
MyShape = "Picture 1"

Application.ScreenUpdating = False

Set objTemp = shtTemp.Shapes(MyShape)

' need to create chart holder
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.Location Where:=xlLocationAsObject, Name:=shtTemp.Name
Set objHolder = shtTemp.ChartObjects(shtTemp.ChartObjects.Count)

sngWidth = objTemp.Width
sngHeight = objTemp.Height
With objHolder
.Chart.ChartArea.Border.LineStyle = xlNone

.Width = sngWidth + 20
.Height = sngHeight + 20
objTemp.Copy
.Chart.Paste
With .Chart.Shapes(1)
.Placement = xlMove
.Left = -4
.Top = -4
End With
.Width = sngWidth + 1
.Height = sngHeight + 1
.Chart.Export Filename, "GIF"
.Chart.Shapes(1).Delete
.Delete
End With

Set objHolder = Nothing
Set objTemp = Nothing
Set shtTemp = Nothing


Application.ScreenUpdating = True

End Sub

Here's what I have of the VB.Net code:
Sub MakeGif()

Dim chartHolder As Excel.Chart
Dim oChart As Excel.Chart
Dim objTemp As Object
Dim sngWidth As Single
Dim sngHeight As Single
Dim MyShape As String
Dim FileName As String
Dim shtTemp As Excel.Worksheet

shtTemp = xlBook.Worksheets("Sheet1")
FileName = "C:\Test.gif"
MyShape = "Picture 1"
objTemp = shtTemp.Shapes.Item(MyShape)

'create Chart Holder
oChart = shtTemp.Parent.Charts.Add
oChart.ChartType = Excel.XlChartType.xlColumnClustered
oChart.Location(Excel.XlChartLocation.xlLocationAsObject,
shtTemp.Name)
chartHolder = shtTemp.ChartObjects(shtTemp.ChartObjects.Count)
**********ERROR HERE*********

sngWidth = objTemp.Width
sngHeight = objTemp.Height

chartHolder.ChartObjects.Width = sngWidth + 20
chartHolder.ChartObjects.Height = sngHeight + 20
objTemp.copy()
chartHolder.Paste()
With chartHolder.Shapes.Item(1)
.Placement = Excel.XlPlacement.xlMove
.Left = -4
.Top = -4
End With
With chartHolder
.ChartObjects.Width = sngWidth + 1
.ChartObjects.Height = sngHeight + 1
.ChartObjects.Chart.Export(FileName, "GIF")
.ChartObjects.Shapes(1).Delete()
.Delete()
End With

chartHolder = Nothing
objTemp = Nothing
shtTemp = Nothing

End Sub
 
J

JP

Ok, after messing it around for a little bit, it turns out I declared one of
my variables wrong, causing me to get the error.

Dim chartHolder As Excel.Chart ****WRONG****

it should've been

Dim chartHolder As Excel.ChartObject
 
G

Guest

Hi,

I'm trying to do the same thing but I can't seem to find how to define the
file format

I'm using Microsoft Excel 11.0 com library.

Private oXLChart As Excel.Chart
<removed all the code to create the chart>

this works

oXLChart.SaveAs("C:\temp\katienewchart.html", Excel.XlFileFormat.xlHtml)

but this throws an exception
oXLChart.Export("C:\temp\katienewchart.gif", "FilterName:=GIF", True)
or
oXLChart.Export("C:\temp\katienewchart.gif", "GIF", True)

I think it's because I can't find the filtername object in excel 11.0 - not
sure if it exists.

You wouldn't know by chance how to do it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top