Import Excel Chart into Visual Basic

  • I'm not sure if it's possible to even do this but I need to import several excel charts into a form, just for displaying. It's easier to create the charts in excel and then import (i think). I have more control in excel with the design. Well, now i have to figure out how to import them. Is this possible? if so, how?


    I already have this declared but where do i take it from here?
    Dim XL As Excel.Application, XLW As Excel.Workbook, XLS As Excel.Worksheet
    Set XL = New Excel.Application
    Set XLW = XL.Workbooks.Open(filename)
    Set XLS = XLW.Worksheets.Item(1)

  • Hi efarhat and welcome,


    If you only want the charts for viewing in your VB app then the easiest thing to do is export them from Excel as images.


    To export a chart as an image file use the syntax;


    Chart.Export Filename:="mychart.gif", FilterName:="GIF"


    The image can then be loaded into a picturebox, image control or form using the LoadPicture method


    If I have mis-understood your question please post back.


    Cheers
    Andy

    [h4]Cheers
    Andy
    [/h4]

  • Andy,


    I don't think this code is working. I'm not using VBA. I'm using VB. I have a form with an image control on it and I want to import the Excel chart either as an object or image, I have no preference. (good idea) Do you know how to do it in VB?


    Essa

  • Hi,


    Are the charts you want to use from Excel dynamic?


    If so then you can expand on the code you started with, exporting the chart after you have changed the data and or properties.


    XLS.chartobjects(1).chart.export "mychart.gif","GIF"


    where you have a chart as an embeded object on sheet1.


    If on the other hand you can create the charts manually in Excel because they will not change.
    Then just select the chart and use the VBA syntax;


    activechart.export "mychart.gif","gif"


    Or do you need code in order to create the chart within Excel from VB ?
    If so try using the macro recorder in Excel and then copy and modify the code in VB.
    Or post a bit more detail or even an example.


    Cheers
    Andy

    [h4]Cheers
    Andy
    [/h4]