Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

(Excel Automation) Load Excel charts into VB.Net

Posted on 2003-02-27
5
Medium Priority
?
1,225 Views
Last Modified: 2007-12-19
I would like to plot (EXCEL) charts and display it in my VBA.

I have tried server methods


Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oWS As Excel.Worksheet
Dim oChart As Excel.Chart

oXL = CreateObject("Excel.Application")
.....
.....


Method 1)
oWS.ChartObjects(1).chart.chartarea.select()
oWS.ChartObjects(1).chart.chartarea.copypicture()

Dim Idata As IDataObject = Clipboard.GetDataObject()
picturebox.image = CType(Idata.GetData(DataFormats.Bitmap), Image)

I try to copy the chart direclt but it fail. I found that the "picture" in clipboard is not in Bitmap format, but MetaFilePic and I don't know how to load the "picutre" into the picture box directly.


Method 2)
oWS.Range("B5", "H14").Select()
oWS.Range("B5", "H14").Copy()

Dim Idata As IDataObject = Clipboard.GetDataObject()
picturebox.image = CType(Idata.GetData(DataFormats.Bitmap), Image)

This time it works, I set the chart width and Height and paste it on B5:H14. However, sometimes it is diffcuclt to control the size of the chart and it is urgy.


Method 3)
Dim Fname As String
Dim myDataObject As New DataObject()

Fname = "c:\" & oWS.ChartObjects(1).Name & ".gif"
oWS.ChartObjects(1).chart.Export(FileName:=Fname, FilterName:="GIF")

picturebox.Image = Image.FromFile(Fname)

This method also work, but I think this method is not good because I will generate many files. On the otherhand, once I load the file, I can't kill it because the system is using the gif(file violation).

Can someone teach me how to load the (EXCEL) Charts into VB.net?
If yes, can you post the syntax? Thx
0
Comment
Question by:leokwan2000
  • 2
4 Comments
 
LVL 9

Expert Comment

by:bhagyesht
ID: 8040176
I would say method 3 as the best in case u want to use the 'Excel' for chart in vb. yes u shall get a lot of files but then u can clean the file once u r through. like on closing the application or on startup just make sure that the files are in a folder and empty the folder on either the unload or load.
0
 

Accepted Solution

by:
leokwan2000 earned 0 total points
ID: 8072234
This method works, hope it can help others.

        Fname = "c:\" & oWS.ChartObjects(1).Name & ".gif"
        oWS.ChartObjects(1).chart.Export(FileName:=Fname, FilterName:="GIF")

        Dim myReader As FileStream = File.OpenRead(Fname)
        picturebox1.Image = Image.FromStream(myReader)
        myReader.Close()
       
        Try
            File.Delete(Fname)

        Catch exc As Exception
            ' Show error to user.
            MsgBox("no hope")
        End Try
0
 

Expert Comment

by:CleanupPing
ID: 9447460
leokwan2000:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9449244
refund points
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question