Excel Linksources / no object array enumerator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ali Tahbaz

    Excel Linksources / no object array enumerator

    I'm having trouble iterating through LinkSources in an Excel workbook
    using C#. I first wrote the below code in VBA to get a quick, correct
    result,

    Dim x As Variant
    For Each x In ThisWorkbook.Li nkSources
    Debug.Print x
    Next

    but am so far unable to successfully convert the code to workable
    C# syntax. C#'s compiler complains

    "foreach statement cannot operate on variables of type 'object'
    because 'object' does not contain a definition for 'GetEnumerator' , or
    it is inaccessible"

    when I try to use the below code :

    foreach(string Src in wb.LinkSources( rfl.Missing.Val ue)) {
    Console.WriteLi ne(Src);
    }

    What can I do to list the links in a workbook from C#?

    Thanks in advance,
    Ali Tahbaz, MCSD
  • arjang

    #2
    Re: Excel Linksources / no object array enumerator

    have a look at this:


    Ajang (No MCSD)

    "Ali Tahbaz" <tahbaza@my-deja.com> wrote in message
    news:e070430d.0 405271319.68c86 1cd@posting.goo gle.com...[color=blue]
    > I'm having trouble iterating through LinkSources in an Excel workbook
    > using C#. I first wrote the below code in VBA to get a quick, correct
    > result,
    >
    > Dim x As Variant
    > For Each x In ThisWorkbook.Li nkSources
    > Debug.Print x
    > Next
    >
    > but am so far unable to successfully convert the code to workable
    > C# syntax. C#'s compiler complains
    >
    > "foreach statement cannot operate on variables of type 'object'
    > because 'object' does not contain a definition for 'GetEnumerator' , or
    > it is inaccessible"
    >
    > when I try to use the below code :
    >
    > foreach(string Src in wb.LinkSources( rfl.Missing.Val ue)) {
    > Console.WriteLi ne(Src);
    > }
    >
    > What can I do to list the links in a workbook from C#?
    >
    > Thanks in advance,
    > Ali Tahbaz, MCSD[/color]


    Comment

    • Ali Tahbaz

      #3
      Re: Excel Linksources / no object array enumerator

      The link to a generic MS help file on .net+ Excel was interesting,
      but has little to do with the specific question I asked. (There is no
      reference to late bound object arrays returned to a C# client.)
      Has anyone run into this problem and overcome it?

      Ali

      "arjang" <delphipro@NOPS AMhotmail.com> wrote in message news:<eZqfK8DRE HA.4016@TK2MSFT NGP12.phx.gbl>. ..[color=blue]
      > have a look at this:
      > http://msdn.microsoft.com/library/de...l/excelobj.asp
      >
      > Ajang (No MCSD)
      >
      > "Ali Tahbaz" <tahbaza@my-deja.com> wrote in message
      > news:e070430d.0 405271319.68c86 1cd@posting.goo gle.com...[color=green]
      > > I'm having trouble iterating through LinkSources in an Excel workbook
      > > using C#. I first wrote the below code in VBA to get a quick, correct
      > > result,
      > >
      > > Dim x As Variant
      > > For Each x In ThisWorkbook.Li nkSources
      > > Debug.Print x
      > > Next
      > >
      > > but am so far unable to successfully convert the code to workable
      > > C# syntax. C#'s compiler complains
      > >
      > > "foreach statement cannot operate on variables of type 'object'
      > > because 'object' does not contain a definition for 'GetEnumerator' , or
      > > it is inaccessible"
      > >
      > > when I try to use the below code :
      > >
      > > foreach(string Src in wb.LinkSources( rfl.Missing.Val ue)) {
      > > Console.WriteLi ne(Src);
      > > }
      > >
      > > What can I do to list the links in a workbook from C#?
      > >
      > > Thanks in advance,
      > > Ali Tahbaz, MCSD[/color][/color]

      Comment

      • Ali Tahbaz

        #4
        FIX:Re: Excel Linksources / no object array enumerator

        For anyone else who needs to parse a variant array returned from Excel
        or elsewhere, here is what I've found that works (through trial and error) :
        (My specific problem was with returning the results of Excel's
        LinkSources method.)

        object src = (object)wb.Link Sources(Excel.X lLinkType.xlLin kTypeExcelLinks );
        System.Array variantArray = (Array)src;
        for(int i=1;i<=variantA rray.Length;i++ ) {
        Console.WriteLi ne(variantArray .GetValue(i).To String());
        }

        C# likes to catch the variant array as a plain object type. Once you've
        got the object type you can cast it to a System.Array and iterate through
        the contained items.

        Ali Tahbaz, MCSD

        tahbaza@my-deja.com (Ali Tahbaz) wrote in message news:<e070430d. 0405271319.68c8 61cd@posting.go ogle.com>...[color=blue]
        > I'm having trouble iterating through LinkSources in an Excel workbook
        > using C#. I first wrote the below code in VBA to get a quick, correct
        > result,
        >
        > Dim x As Variant
        > For Each x In ThisWorkbook.Li nkSources
        > Debug.Print x
        > Next
        >
        > but am so far unable to successfully convert the code to workable
        > C# syntax. C#'s compiler complains
        >
        > "foreach statement cannot operate on variables of type 'object'
        > because 'object' does not contain a definition for 'GetEnumerator' , or
        > it is inaccessible"
        >
        > when I try to use the below code :
        >
        > foreach(string Src in wb.LinkSources( rfl.Missing.Val ue)) {
        > Console.WriteLi ne(Src);
        > }
        >
        > What can I do to list the links in a workbook from C#?
        >
        > Thanks in advance,
        > Ali Tahbaz, MCSD[/color]

        Comment

        Working...