Results 1 to 5 of 5

Thread: [RESOLVED] Excel automation 2 dot issue with Charts collection

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Resolved [RESOLVED] Excel automation 2 dot issue with Charts collection

    I'm getting close to what I want but not quite there. I can get it to work if I'm just adding a ChartObject to the current sheet, but I want to create a new Chart sheet and can't seem to get the declarations and/or casting to come out right (even if I turn off Option Strict) so it leaves the Excel process running.

    vb.net Code:
    1. Dim xlApp As Excel.Application = Nothing
    2.         Dim xlWorkBooks As Excel.Workbooks = Nothing
    3.         Dim xlWorkBook As Excel.Workbook = Nothing
    4.         Dim xlWorkSheet As Excel.Worksheet = Nothing
    5.         Dim xlWorkSheets As Excel.Sheets = Nothing
    6.         Dim xlRange1 As Excel.Range = Nothing
    7.         Dim xlInterior As Excel.Interior = Nothing
    8.         Dim xlColumns As Excel.Range = Nothing
    9.  
    10.         xlApp = New Excel.Application
    11.         xlApp.DisplayAlerts = False
    12.         xlWorkBooks = xlApp.Workbooks
    13.         xlWorkBook = xlWorkBooks.Open(FileName)
    14.  
    15.         xlApp.Visible = False
    16.  
    17.         xlWorkSheets = xlWorkBook.Sheets
    18.         xlWorkSheet = CType(xlWorkSheets(1), Excel.Worksheet)
    19.  
    20.         'process the data for the chart into Sheet1
    21.  
    22.         Dim xlChart As Excel.Chart = Nothing
    23.         xlChart = CType(xlWorkBook.Charts.Add(After:=xlApp.ActiveSheet), Excel.Chart)

    The last line is what causes me problems. I tried several different ways to declare the Charts collection (for example, xlCharts = CType(xlWorkBook.Charts, Excel.Charts), but it would eventually give me an error.
    This operation failed because the QueryInterface call on the COM component for the interface with IID '{0002086D-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002
    A minor complaint is that it is also not putting the Chart sheet after the ActiveSheet even though that is returning "Sheet1" as it should.

    So what am I missing?

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: Excel automation 2 dot issue with Charts collection

    A workaround that is not ideal is creating a new sheet and moving the chart to it. However, this makes it look like a normal embedded chart (ie, sitting on top of a sheet of empty cells) rather than a chart sheet.

    vb.net Code:
    1. xlWorkSheet = CType(xlWorkSheets.Add, Excel.Worksheet)
    2.             xlWorkSheet.Name = "Torque"
    3.             xlChart.Location(Excel.XlChartLocation.xlLocationAsObject, "Torque")

    If I do the following, it automatically creates a new chart sheet, but then I have the original issue of leaving the Excel process hanging since this new sheet wasn't explicitly declared.

    vb.net Code:
    1. xlChart.Location(Excel.XlChartLocation.xlLocationAsNewSheet, "Torque")

    Creating a chart sheet is getting really annoying.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: Excel automation 2 dot issue with Charts collection

    I thought I had it but was denied again! What the heck is the difference between what I had done above (copied here)
    vb.net Code:
    1. xlWorkSheet = CType(xlWorkSheets.Add, Excel.Worksheet)
    2.             xlWorkSheet.Name = "Torque"
    3.             xlChart.Location(Excel.XlChartLocation.xlLocationAsObject, "Torque")

    and this? (Note: in this example objChart1 was the Excel.Chart variable for the chart I had created on Sheet1. xlChart is Excel.Chart that is for the chart sheet)

    vb.net Code:
    1. xlChart = CType(xlWorkSheets.Add(Type:=Excel.XlSheetType.xlChart), Excel.Chart)
    2.             xlChart.Name = "Torque"
    3.             objChart1.Location(Excel.XlChartLocation.xlLocationAsObject, "Torque")
    4.             Marshal.FinalReleaseComObject(xlChart)
    5.             xlChart = Nothing
    I even added the last 2 lines to keep it from holding the Excel process but that doesn't help.
    Last edited by topshot; Jun 6th, 2015 at 12:24 PM.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: Excel automation 2 dot issue with Charts collection

    Well, I guess nevermind. I don't know what I did exactly, but it is working now in either of these options. A chart sheet is being created without holding Excel open.

    vb.net Code:
    1. 'I did make all the names more consistent everywhere
    2.  
    3.         xlChartFlow.Location(Excel.XlChartLocation.xlLocationAsNewSheet, "Flow")
    4.  
    5. OR
    6.  
    7.         xlChartSheet = CType(xlWorkSheets.Add(Type:=Excel.XlSheetType.xlChart), Excel.Chart)
    8.         xlChartSheet.Name = "Flow"
    9.         xlChartFlow.Location(Excel.XlChartLocation.xlLocationAsObject, "Flow")

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: [RESOLVED] Excel automation 2 dot issue with Charts collection

    This post is just to remind me of this advice from TnTinMN if I ever need to deal with Excel again.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width