Results 1 to 9 of 9

Thread: [RESOLVED] [2005] Determine variables in Excel Chart

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Resolved [RESOLVED] [2005] Determine variables in Excel Chart

    I hate to keep posting Excel questions but I have another question. I'm trying to open an Excel template that contains two worksheets and one or more charts. I want to be able to take the charts and load the chart name and variables in a treeview. I can get the chart names into the parent nodes but I'm having trouble getting the Variables associated to each chart. I've tried using the seriescollection but something just isn't right.

    I guess my question is how can I determine the variables in an Excel charts series?

    Below is what I have. The "var =" line is where my troubles begin.

    vb Code:
    1. Public Function GetCharts() As TreeNode
    2.         Dim parentNode As TreeNode = Nothing
    3.         Dim var As String = ""
    4.         Dim i as Integer = 1
    5.  
    6.         For Each oCht As Excel.Chart In oExcel.Workbooks(1).Charts
    7.  
    8.             parentNode = New TreeNode(oCht.Name)
    9.             Do
    10.                 var = CType(oCht.SeriesCollection, Excel.SeriesCollection).Item(i).Name
    11.                 i += 1
    12.                 parentNode.Nodes.Add(var)
    13.             Loop While var <> ""
    14.  
    15.         Next
    16.  
    17.         Return parentNode
    18.  
    19.     End Function

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Determine variables in Excel Chart

    After thinking about what is avaiable to a chart, I've realized the the variables do not exist directly. I need to access the range of the series then I can go to the worksheet that contains the series data and extract the variable names.

    This brings me back to the seriecollection method of the chart object. Does anyone know how to use this to get the range of the Y axis values?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Determine variables in Excel Chart

    I think that I'm getting close. I can get the number of variables per chart using the code below but when I try to run I get an exception when I cast the seriesCollection.item to a string.


    vb Code:
    1. Public Function GetCharts() As TreeNode
    2.         Dim parentNode As TreeNode = Nothing
    3.  
    4.         Try
    5.  
    6.             For Each oCht As Excel.Chart In oExcel.Workbooks(1).Charts
    7.                 Dim var As String
    8.  
    9.                 parentNode = New TreeNode(oCht.Name)
    10.  
    11.                 Dim cnt As Integer = CType(oCht.SeriesCollection, Excel.SeriesCollection).Count
    12.                 For i As Integer = 1 To cnt
    13.                     var = CStr(CType(oCht.SeriesCollection, Excel.SeriesCollection).Item(i))
    14.                     parentNode.Nodes.Add(var)
    15.                 Next
    16.             Next
    17.  
    18.         Catch ex As Exception
    19.             MessageBox.Show("GetCharts.GetVariables: " & ex.Message.ToString)
    20.         End Try
    21.  
    22.         Return parentNode
    23.  
    24.     End Function
    Attached Images Attached Images  

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Determine variables in Excel Chart

    bump

    I'm finally at a point that this is holding up progress. Any Excel/.Net experts out there that want to give this a go?

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Determine variables in Excel Chart

    Try adding another level of casting as you are casting the collection but not the item.
    Code:
    var = Ctype(CType(oCht.SeriesCollection, Excel.SeriesCollection).Item(i), WhatEverItIsSupossedToBeType).ToString
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Determine variables in Excel Chart

    I think that I'm trying to read data that is not available. I'm trying various versions of the code below and keep getting and error that says:
    "Unable to get the Name property of the Series Class".

    The property is available in intelisense but throws an exception when I acces it.

    vb Code:
    1. var=cstr(ctype(CType(oCht.SeriesCollection, Excel.SeriesCollection).Item(i),Excel.Series).Name)

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    A Little more information

    I'm trying to read this data from an Excel Template. I just tried to read the same data from an xls file and although I did not get exactly what I wanted, I was able to see the series name.

    So what is the difference in reading the chart format from a template (xlt) versus an xls ?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Determine variables in Excel Chart

    It looks like I'm going to have to find a new way to do what I want. I just tried to access the seriescollection of a template using VBA and I get the same results. For some reason, the data isn't available for template files.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Determine variables in Excel Chart

    HOLY CRAP....

    This had nothing to do with xlt versus xls. The series range can not be obtained programatically unless the series has at least one data point. The solution was to put a data point (0) into every column of my template's data sheet, extract the range and then delete the data.

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