Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Connect a number to a picture bank and import that picture to exce | Excel Discussion (Misc queries) | |||
how do I insert picture into cell so vlookup can return picture? | Excel Worksheet Functions | |||
insert a picture in to a comment but picture not save on hard disk | Excel Discussion (Misc queries) | |||
How to extract a picture from an Excel worksheet into a picture fi | Excel Discussion (Misc queries) | |||
I know selected picture is a shape but typename says is picture | Excel Programming |