locked
(C++) DocumentWindow::RangeFromPoint not work when Pane[2] is not active ? RRS feed

  • Question

  • PowerPoint

    Why DocumentWindow::RangeFromPoint always return E_FAIL(0x80004005)  if the second Pane of the DocumentWindow is not active?


    • Edited by Raptor K Wednesday, September 26, 2012 6:18 AM
    Wednesday, September 26, 2012 5:39 AM

Answers

  • Hello Raptor,

    I was able to reproduce your issue.  The error comes because the RangeFromPoint method is not accessible when the main PowerPoint Pane is not in focus.  The correct way to call RangeFromPoint is to activate this pane and then call RangeFromPoint.  The main PowerPoint pane id is 2.  So the calls should be as follows:-

    pDocWin->Panes->Item(2)->Activate();
    pDocWin->RangeFromPoint(10, 10);

    Below is the sample source code that illustrates this approach.

    // PowerPointSample.cpp : Defines the entry point for the console application.


    #include "stdafx.h"
    #include "comdef.h"

    #include <ocidl.h>

    #pragma warning(disable:4146)

    #import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\mso.dll"
    #import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB"

    #import "C:\Program Files (x86)\Microsoft Office\Office14\msppt.olb" named_guids,  rename( "RGB", "MsoRGB")


    #pragma warning(disable:4146)


    CComQIPtr<IDispatch> m_pApp;
    _bstr_t   m_sSrcFilePath = L"D:\\RangeFromPoint\\PresentationOrd.pptx";


    HRESULT  CallRangeFromPoint()
    {
     
     VARIANT varIndex;
     varIndex.vt =::VT_INT;
     varIndex.intVal = 2;
     HRESULT hr;
     PowerPoint::_ApplicationPtr pApp = NULL;
     
     IDispatchPtr ptrDisp = NULL;

     PowerPoint::DocumentWindowsPtr pDocWindows;
     PowerPoint::DocumentWindowPtr pDocWin;
     
     if (m_pApp)
     {
      try
      {
       
       m_pApp->QueryInterface(__uuidof(PowerPoint::_Application), (void**)&pApp);

       if (pApp != NULL)
       {

        PowerPoint::PresentationsPtr pPresentations = pApp->GetPresentations();

        PowerPoint::_PresentationPtr oPresentation = pPresentations->Open( m_sSrcFilePath, Office::MsoTriState::msoFalse, Office::MsoTriState::msoFalse, Office::MsoTriState::msoTrue ) ;
        pApp->PutVisible(Office::MsoTriState::msoTrue);
        
        //pDocWindows= pApp->Windows;
        pDocWindows = oPresentation->Windows;
        //pDocWindows = pApp->ActivePresentation->Windows;

        long lcnt = pDocWindows->GetCount();
        
        long slideCnt = oPresentation->GetSlides()->Count;
        // Assign the PowerPoint::DocumentWindowPtr
        if(slideCnt > 0)
        {
         
         pDocWin = pDocWindows->Item(1);
         pDocWin->Panes->Item(2)->Activate();
         
        
         ptrDisp = pDocWindows->Item(1)->RangeFromPoint(10,10);
        
         //equivalent call
         ptrDisp = pDocWin->RangeFromPoint(10, 10);
        }

       }
      }
      catch(_com_error &e)
      {
       MessageBox(0,e.ErrorMessage(),L"",MB_OK);
       MessageBox(0,e.Description(),L"",MB_OK);
       return E_FAIL;
      }

      pApp->Release();
      
     }
     return S_OK;
    }

     

    int _tmain(int argc, _TCHAR* argv[])
    {
     CoInitialize(NULL);
     CComQIPtr<IUnknown, &IID_IUnknown> pUnk;
     HRESULT hr = E_FAIL;

     
     hr = pUnk.CoCreateInstance(L"Powerpoint.Application", NULL, CLSCTX_LOCAL_SERVER);
     
     m_pApp = pUnk;
     
     //hr = CallAddinMethod();
     hr = CallRangeFromPoint();
     
     CoUninitialize();
     return 0;
    }

    Regards,
    Yet


    yet

    • Marked as answer by Raptor K Saturday, October 13, 2012 1:58 AM
    Friday, October 12, 2012 7:46 PM

All replies

  • Hi Raptor,

    Thank you for posting in the MSDN Forum.

    I'll involve some experts who are more familiar with your issue. This may take some time.

    It will be nice if you could provide some more detailed description on your error or provide more code.

    Much appreciate for your patient and understanding.

    Best regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Quist Zhang Thursday, September 27, 2012 8:21 AM
    Thursday, September 27, 2012 8:15 AM
  • Hello Raptor,

    I was able to reproduce your issue.  The error comes because the RangeFromPoint method is not accessible when the main PowerPoint Pane is not in focus.  The correct way to call RangeFromPoint is to activate this pane and then call RangeFromPoint.  The main PowerPoint pane id is 2.  So the calls should be as follows:-

    pDocWin->Panes->Item(2)->Activate();
    pDocWin->RangeFromPoint(10, 10);

    Below is the sample source code that illustrates this approach.

    // PowerPointSample.cpp : Defines the entry point for the console application.


    #include "stdafx.h"
    #include "comdef.h"

    #include <ocidl.h>

    #pragma warning(disable:4146)

    #import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\mso.dll"
    #import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB"

    #import "C:\Program Files (x86)\Microsoft Office\Office14\msppt.olb" named_guids,  rename( "RGB", "MsoRGB")


    #pragma warning(disable:4146)


    CComQIPtr<IDispatch> m_pApp;
    _bstr_t   m_sSrcFilePath = L"D:\\RangeFromPoint\\PresentationOrd.pptx";


    HRESULT  CallRangeFromPoint()
    {
     
     VARIANT varIndex;
     varIndex.vt =::VT_INT;
     varIndex.intVal = 2;
     HRESULT hr;
     PowerPoint::_ApplicationPtr pApp = NULL;
     
     IDispatchPtr ptrDisp = NULL;

     PowerPoint::DocumentWindowsPtr pDocWindows;
     PowerPoint::DocumentWindowPtr pDocWin;
     
     if (m_pApp)
     {
      try
      {
       
       m_pApp->QueryInterface(__uuidof(PowerPoint::_Application), (void**)&pApp);

       if (pApp != NULL)
       {

        PowerPoint::PresentationsPtr pPresentations = pApp->GetPresentations();

        PowerPoint::_PresentationPtr oPresentation = pPresentations->Open( m_sSrcFilePath, Office::MsoTriState::msoFalse, Office::MsoTriState::msoFalse, Office::MsoTriState::msoTrue ) ;
        pApp->PutVisible(Office::MsoTriState::msoTrue);
        
        //pDocWindows= pApp->Windows;
        pDocWindows = oPresentation->Windows;
        //pDocWindows = pApp->ActivePresentation->Windows;

        long lcnt = pDocWindows->GetCount();
        
        long slideCnt = oPresentation->GetSlides()->Count;
        // Assign the PowerPoint::DocumentWindowPtr
        if(slideCnt > 0)
        {
         
         pDocWin = pDocWindows->Item(1);
         pDocWin->Panes->Item(2)->Activate();
         
        
         ptrDisp = pDocWindows->Item(1)->RangeFromPoint(10,10);
        
         //equivalent call
         ptrDisp = pDocWin->RangeFromPoint(10, 10);
        }

       }
      }
      catch(_com_error &e)
      {
       MessageBox(0,e.ErrorMessage(),L"",MB_OK);
       MessageBox(0,e.Description(),L"",MB_OK);
       return E_FAIL;
      }

      pApp->Release();
      
     }
     return S_OK;
    }

     

    int _tmain(int argc, _TCHAR* argv[])
    {
     CoInitialize(NULL);
     CComQIPtr<IUnknown, &IID_IUnknown> pUnk;
     HRESULT hr = E_FAIL;

     
     hr = pUnk.CoCreateInstance(L"Powerpoint.Application", NULL, CLSCTX_LOCAL_SERVER);
     
     m_pApp = pUnk;
     
     //hr = CallAddinMethod();
     hr = CallRangeFromPoint();
     
     CoUninitialize();
     return 0;
    }

    Regards,
    Yet


    yet

    • Marked as answer by Raptor K Saturday, October 13, 2012 1:58 AM
    Friday, October 12, 2012 7:46 PM
  • Thanks, I used the same way. why must it activatied.

    Win8 Developer QQ Group 95331609

    Saturday, October 13, 2012 1:58 AM
  • The second pane should be activated because, only this pane can be used when calling the RangeFromPoint method.  The other panes do not expose a RangeFromPoint method which is the reason the error is thrown.

    Regards,

    yet


    yet

    Sunday, October 14, 2012 5:24 PM