'System.InvalidCastException'

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

    #1

    'System.InvalidCastException'

    Hi !!!

    I hav a problem when I try to initialize my workbook (opening or creation)
    in my windows form...
    I got the Exception

    'System.Invalid CastException' : QueryInterface for interface
    Excel._Applicat ion failed

    I tried to change de CultureInfo but it doesn't work better.
    I am working with VS.Net and Excel 2000

    is there something else to do ?

    thanks

    Julien


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: 'System.Invalid CastException'

    Julien,

    Is it possible that you are running this on a machine that has a
    different version of Excel than the product was developed for? If so, then
    you will have to resort to either late binding, or performing a conditional
    switch, and have references to each version's interfaces.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Julien" <julien.pinquie @heuft.com> wrote in message
    news:eLCUbxhnDH A.2140@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hi !!!
    >
    > I hav a problem when I try to initialize my workbook (opening or creation)
    > in my windows form...
    > I got the Exception
    >
    > 'System.Invalid CastException' : QueryInterface for interface
    > Excel._Applicat ion failed
    >
    > I tried to change de CultureInfo but it doesn't work better.
    > I am working with VS.Net and Excel 2000
    >
    > is there something else to do ?
    >
    > thanks
    >
    > Julien
    >
    >[/color]


    Comment

    • Julien

      #3
      Re: 'System.Invalid CastException'

      I am trying to develop with excel 2000, maybe it will be better that I try
      Excel XP !!!


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> a écrit
      dans le message de news: #t8AWBinDHA.242 4@TK2MSFTNGP10. phx.gbl...[color=blue]
      > Julien,
      >
      > Is it possible that you are running this on a machine that has a
      > different version of Excel than the product was developed for? If so,[/color]
      then[color=blue]
      > you will have to resort to either late binding, or performing a[/color]
      conditional[color=blue]
      > switch, and have references to each version's interfaces.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Julien" <julien.pinquie @heuft.com> wrote in message
      > news:eLCUbxhnDH A.2140@TK2MSFTN GP09.phx.gbl...[color=green]
      > > Hi !!!
      > >
      > > I hav a problem when I try to initialize my workbook (opening or[/color][/color]
      creation)[color=blue][color=green]
      > > in my windows form...
      > > I got the Exception
      > >
      > > 'System.Invalid CastException' : QueryInterface for interface
      > > Excel._Applicat ion failed
      > >
      > > I tried to change de CultureInfo but it doesn't work better.
      > > I am working with VS.Net and Excel 2000
      > >
      > > is there something else to do ?
      > >
      > > thanks
      > >
      > > Julien
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Morten Wennevik

        #4
        Re: 'System.Invalid CastException'

        It might help if you would post some code of the problem.
        My guess is that what you are trying to send to excel isn't something
        excel can handle.
        Not sure what changing cultureinfo would fix. Then again, some code would
        be helpful.

        --
        Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: 'System.Invalid CastException'

          Julien,

          Where did you get the primary interop assemblies that you are using in
          ..NET to access Excel from?


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          "Julien" <julien.pinquie @heuft.com> wrote in message
          news:uagqvEinDH A.2404@TK2MSFTN GP12.phx.gbl...[color=blue]
          > I am trying to develop with excel 2000, maybe it will be better that I try
          > Excel XP !!!
          >
          >
          > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> a écrit
          > dans le message de news: #t8AWBinDHA.242 4@TK2MSFTNGP10. phx.gbl...[color=green]
          > > Julien,
          > >
          > > Is it possible that you are running this on a machine that has a
          > > different version of Excel than the product was developed for? If so,[/color]
          > then[color=green]
          > > you will have to resort to either late binding, or performing a[/color]
          > conditional[color=green]
          > > switch, and have references to each version's interfaces.
          > >
          > > Hope this helps.
          > >
          > >
          > > --
          > > - Nicholas Paldino [.NET/C# MVP]
          > > - mvp@spam.guard. caspershouse.co m
          > >
          > > "Julien" <julien.pinquie @heuft.com> wrote in message
          > > news:eLCUbxhnDH A.2140@TK2MSFTN GP09.phx.gbl...[color=darkred]
          > > > Hi !!!
          > > >
          > > > I hav a problem when I try to initialize my workbook (opening or[/color][/color]
          > creation)[color=green][color=darkred]
          > > > in my windows form...
          > > > I got the Exception
          > > >
          > > > 'System.Invalid CastException' : QueryInterface for interface
          > > > Excel._Applicat ion failed
          > > >
          > > > I tried to change de CultureInfo but it doesn't work better.
          > > > I am working with VS.Net and Excel 2000
          > > >
          > > > is there something else to do ?
          > > >
          > > > thanks
          > > >
          > > > Julien
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Julien

            #6
            Re: 'System.Invalid CastException'

            Thanks for replying, here is the code :

            =============== =============== =============== ==========
            private Excel.Applicati on ExcelObj = null;

            public Form1()
            {
            // Initialize the Windows Components
            InitializeCompo nent();
            ExcelObj = new Excel.Applicati on();
            // See if the Excel Application Object was successfully constructed
            if (ExcelObj == null)
            {
            MessageBox.Show ("ERROR: EXCEL couldn't be started!");
            System.Windows. Forms.Applicati on.Exit();
            }
            }

            private void menuItem2_Click (object sender, System.EventArg s e)
            {
            // *** open the workbook --> it goes wrong on this line
            Excel.Workbook theWorkbook =
            ExcelObj.Workbo oks.Open("c:\co unter.xls",Type .Missing, Type.Missing,
            Type.Missing,Ty pe.Missing,Type .Missing, Type.Missing,
            Excel.XlPlatfor m.xlWindows, Type.Missing,Ty pe.Missing,
            Type.Missing,Ty pe.Missing, Type.Missing);
            // *** get the collection of sheets in the workbook
            Excel.Sheets sheets = theWorkbook.Wor ksheets;
            // *** get the first and only worksheet from the collection of worksheets
            Excel.Worksheet worksheet = (Excel.Workshee t)sheets.get_It em(1);
            ....
            }
            =============== =============== =============== =============== ===

            concerning the primary interop assemblies, under VS.Net I choose project>add
            a reference>COM and then I select "Microsoft Excel 9 Object Librairy",
            filename is "excel9.olb " ; it puts the interop.excel.d ll and
            interop.office. dll in my project folder... I have problem to understand the
            what is Interop...

            Thanks for your help


            _______________ _______________ _______________ _______________ _________
            "Morten Wennevik" <MortenWennevik @hotmail.com> a écrit dans le message de
            news: oprxs3y0dbhntkf z@localhost...
            It might help if you would post some code of the problem.
            My guess is that what you are trying to send to excel isn't something
            excel can handle.
            Not sure what changing cultureinfo would fix. Then again, some code would
            be helpful.

            --
            Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


            Comment

            • Morten Wennevik

              #7
              Re: 'System.Invalid CastException'

              > // *** open the workbook --> it goes wrong on this line[color=blue]
              > Excel.Workbook theWorkbook =
              > ExcelObj.Workbo oks.Open([/color]
              "c:\counter.xls ",
              Type.Missing,
              Type.Missing,
              Type.Missing,
              Type.Missing,
              Type.Missing,
              Type.Missing,
              Excel.XlPlatfor m.xlWindows,
              Type.Missing,
              Type.Missing,
              Type.Missing,
              Type.Missing,
              Type.Missing);

              Something among these paramteres is of the wrong type, but I couldn't find
              much reference to WoorkBooks.Open in my helpfiles or MSDN, so I'm not sure
              what it expects.

              Choosing the right web hosting company is one of the most important — and most consequential — decisions you’ll make when starting or growing your website.

              used this method to open a woorkbook

              Excel.Workbook Workbook = objExcel.Workbo oks.Open(
              ImportFileName,
              0,
              true,
              5,
              "",
              "",
              true,
              Excel.XlPlatfor m.xlWindows,
              "\t",
              false,
              false,
              0,
              false);
              --
              Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

              Comment

              • Julien

                #8
                Re: 'System.Invalid CastException'

                I already try all possibilities but nothing works... I don't know if this is
                the code but maybe it comes from the interops ??


                "Morten Wennevik" <MortenWennevik @hotmail.com> a écrit dans le message de
                news: oprxs6x0wehntkf z@localhost...[color=blue]
                > // *** open the workbook --> it goes wrong on this line
                > Excel.Workbook theWorkbook =
                > ExcelObj.Workbo oks.Open([/color]
                "c:\counter.xls ",
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Excel.XlPlatfor m.xlWindows,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing);

                Something among these paramteres is of the wrong type, but I couldn't find
                much reference to WoorkBooks.Open in my helpfiles or MSDN, so I'm not sure
                what it expects.

                Choosing the right web hosting company is one of the most important — and most consequential — decisions you’ll make when starting or growing your website.

                used this method to open a woorkbook

                Excel.Workbook Workbook = objExcel.Workbo oks.Open(
                ImportFileName,
                0,
                true,
                5,
                "",
                "",
                true,
                Excel.XlPlatfor m.xlWindows,
                "\t",
                false,
                false,
                0,
                false);
                --
                Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


                Comment

                Working...