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
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