Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Exporting Picture to Gif - VBA to VB.Net

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 Whe=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.xlLocationAs Object,
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



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Exporting Picture to Gif - VBA to VB.Net

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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default Exporting Picture to Gif - VBA to VB.Net

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?




"JP" wrote:

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




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connect a number to a picture bank and import that picture to exce Dennis Hedo Excel Discussion (Misc queries) 1 March 22nd 10 02:17 PM
how do I insert picture into cell so vlookup can return picture? ah Excel Worksheet Functions 1 May 1st 07 04:38 AM
insert a picture in to a comment but picture not save on hard disk Pablo Excel Discussion (Misc queries) 0 February 21st 07 03:48 PM
How to extract a picture from an Excel worksheet into a picture fi SARANJAI Excel Discussion (Misc queries) 10 June 12th 05 05:00 AM
I know selected picture is a shape but typename says is picture Carlos Lozano Excel Programming 3 July 24th 04 05:44 PM


All times are GMT +1. The time now is 10:52 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"