Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. why pApplication.CreateInstance( _T("Excel.Application") ) FAILED?

why pApplication.CreateInstance( _T("Excel.Application") ) FAILED?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Le rner
    wrote on last edited by
    #1

    i am reading excel file . Excel::_ApplicationPtr pApplication; pApplication.CreateInstance( _T("Excel.Application") ); but i dont understand why pApplication.CreateInstance( _T("Excel.Application") ) failed on my PC while its working fine on another PC. please help me for this. thanks.

    CPalliniC 1 Reply Last reply
    0
    • L Le rner

      i am reading excel file . Excel::_ApplicationPtr pApplication; pApplication.CreateInstance( _T("Excel.Application") ); but i dont understand why pApplication.CreateInstance( _T("Excel.Application") ) failed on my PC while its working fine on another PC. please help me for this. thanks.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Le@rner wrote:

      but i dont understand why pApplication.CreateInstance( _T("Excel.Application") ) failed

      Because you didn't bother to check the return value.

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      L 1 Reply Last reply
      0
      • CPalliniC CPallini

        Le@rner wrote:

        but i dont understand why pApplication.CreateInstance( _T("Excel.Application") ) failed

        Because you didn't bother to check the return value.

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        L Offline
        L Offline
        Le rner
        wrote on last edited by
        #3

        no chk this

        if ( FAILED( pApplication.CreateInstance( _T("Excel.Application") ) ) )
        {
        AfxMessage( _T("Failed to initialize Excel!") );

        			return ;
        		}
        
        L CPalliniC 2 Replies Last reply
        0
        • L Le rner

          no chk this

          if ( FAILED( pApplication.CreateInstance( _T("Excel.Application") ) ) )
          {
          AfxMessage( _T("Failed to initialize Excel!") );

          			return ;
          		}
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          It is no use printing a message such as the above when something fails, as it provides no useful information. You need to capture the system error code from GetLastError() and print its details to find out why your program fails. I am assuming that you have all the necessary libraries installed on your PC in the first place. Oops: sorry you need to interpret your HRESULT as CPallini suggests.

          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

          CPalliniC 1 Reply Last reply
          0
          • L Le rner

            no chk this

            if ( FAILED( pApplication.CreateInstance( _T("Excel.Application") ) ) )
            {
            AfxMessage( _T("Failed to initialize Excel!") );

            			return ;
            		}
            
            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            That way, you are discarding a precious info, the HRESULT return value.

            HRESULT hr;
            hr = pApplication.CreateInstance( _T("Excel.Application");
            if ( FAILED(hr))
            {
            // show error message AND error code here
            return;
            }

            is far better.

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            L 1 Reply Last reply
            0
            • L Lost User

              It is no use printing a message such as the above when something fails, as it provides no useful information. You need to capture the system error code from GetLastError() and print its details to find out why your program fails. I am assuming that you have all the necessary libraries installed on your PC in the first place. Oops: sorry you need to interpret your HRESULT as CPallini suggests.

              Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              I believe that's the optimal way with standard API (like Win32). On the other hand, COM calls provides error code in return value.

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              L 1 Reply Last reply
              0
              • CPalliniC CPallini

                I believe that's the optimal way with standard API (like Win32). On the other hand, COM calls provides error code in return value.

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You are, of course, correct; it was my bad (mea culpa) and I have fixed it.

                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                1 Reply Last reply
                0
                • CPalliniC CPallini

                  That way, you are discarding a precious info, the HRESULT return value.

                  HRESULT hr;
                  hr = pApplication.CreateInstance( _T("Excel.Application");
                  if ( FAILED(hr))
                  {
                  // show error message AND error code here
                  return;
                  }

                  is far better.

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  L Offline
                  L Offline
                  Le rner
                  wrote on last edited by
                  #8

                  HRESULT hr;
                  hr = pApplication.CreateInstance( _T("Excel.Application");
                  if ( FAILED(hr))
                  {
                  //format Message Of GetLastError();

                  "An attempt was made to reference a token that does not exist" error comes when i format the message of GetLastError().
                  return;
                  }

                  CPalliniC 1 Reply Last reply
                  0
                  • L Le rner

                    HRESULT hr;
                    hr = pApplication.CreateInstance( _T("Excel.Application");
                    if ( FAILED(hr))
                    {
                    //format Message Of GetLastError();

                    "An attempt was made to reference a token that does not exist" error comes when i format the message of GetLastError().
                    return;
                    }

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    I don't know about, however it looks like Excel doesn't allow your application to access it (I guessed that from this page[^]: they face the same error message, see the "Configure Excel application to be accessed by non-System account" section. You may try to run your application 'As Administrator' to validate such hypothesys.

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    S 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      I don't know about, however it looks like Excel doesn't allow your application to access it (I guessed that from this page[^]: they face the same error message, see the "Configure Excel application to be accessed by non-System account" section. You may try to run your application 'As Administrator' to validate such hypothesys.

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      S Offline
                      S Offline
                      Sampath579
                      wrote on last edited by
                      #10

                      Hi. I am facing exactly same problem and posted in the query in below link. https://www.codeproject.com/Answers/5165616/Excel-applicationptr-createinstance-is-failing#answer1 Can any one explain how to resolve this CreateInstance() failure. Windows 10 - 64 bit and Office 365 64bit and VS2017 enterprise edition i am using.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups