locked
PowerPoint --> Marshal.GetActiveObject("PowerPoint.Application"); RRS feed

  • Question

  • We were facing technical challenge on “Add new PPT Application to collection and activate the events for the Presentations under it”.

     

    Current code (please refer the Attachment of codesnippets.txt)

    pptApp = (PowerPoint.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("PowerPoint.Application");

     

    We are getting the execption on above line while running the application as “Exception Occured in PPTAppStarted... : Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))”

     

    But while debugging the code it is working fine.

     

    Please do the needful help.

    Thursday, April 8, 2010 7:02 AM

Answers

  • Hi,

    This KB shows a probable solution for your case, hope it can helps:

     

    Although the Office application is running, it might not be registered in the Running Object Table (ROT). A running instance of an Office application must be registered in the ROT before it can be attached to using GetObject (Visual Basic) or GetActiveObject (Visual C++).

     

    Using code, you can change focus from the Office application to your own application (or to some other application) to allow it to register itself in the ROT. Additionally, if your code is launching the Office application's exe file, you might need to wait for the Office application to finish loading before attempting to attach to the running instance.  


    Sincerely,
    Eric

    • Proposed as answer by Ter-ray-cy Wednesday, April 14, 2010 9:51 AM
    • Proposed as answer by Ter-ray-cy Wednesday, April 14, 2010 9:51 AM
    • Marked as answer by SamAgain Friday, April 16, 2010 9:52 AM
    • Unmarked as answer by SamAgain Friday, April 16, 2010 9:52 AM
    • Marked as answer by eryang Tuesday, April 27, 2010 5:52 AM
    Thursday, April 8, 2010 10:41 AM
  • Hi Subash,

    According to your description, I just could suggest you as following:

    1. Are you sure your application available in ROT?

    2. If not, try to fix it.

    3. If yes, are you sure your client(calling into an instance of PowerPoint) runs with IL "medium integrity". All office applications run with this IL, connection to the remote instance will fail from a client running with an higher IL, (for instance when you run as administrator)

    Hope this helpful to you!


    Please mark the right answer at right time.
    Bset Regards,
    Tracy
    • Proposed as answer by Ter-ray-cy Wednesday, April 14, 2010 9:51 AM
    • Marked as answer by SamAgain Friday, April 16, 2010 9:52 AM
    • Unmarked as answer by SamAgain Friday, April 16, 2010 9:52 AM
    • Marked as answer by eryang Tuesday, April 27, 2010 5:52 AM
    Thursday, April 8, 2010 8:04 AM
  • The process needs some time to initiate itself, so the when condition 'Process.GetProcessesByName("POWERPNT").Length != 1' is false, the process itself still in invalid status, you may wait for a while and then call 'Marshal.GetActiveObject("PowerPoint.Application");'


    Sincerely,
    Eric
    • Marked as answer by eryang Tuesday, April 27, 2010 5:52 AM
    Friday, April 23, 2010 2:04 AM

All replies

  • Hi Subash,

    According to your description, I just could suggest you as following:

    1. Are you sure your application available in ROT?

    2. If not, try to fix it.

    3. If yes, are you sure your client(calling into an instance of PowerPoint) runs with IL "medium integrity". All office applications run with this IL, connection to the remote instance will fail from a client running with an higher IL, (for instance when you run as administrator)

    Hope this helpful to you!


    Please mark the right answer at right time.
    Bset Regards,
    Tracy
    • Proposed as answer by Ter-ray-cy Wednesday, April 14, 2010 9:51 AM
    • Marked as answer by SamAgain Friday, April 16, 2010 9:52 AM
    • Unmarked as answer by SamAgain Friday, April 16, 2010 9:52 AM
    • Marked as answer by eryang Tuesday, April 27, 2010 5:52 AM
    Thursday, April 8, 2010 8:04 AM
  • Hi,

    This KB shows a probable solution for your case, hope it can helps:

     

    Although the Office application is running, it might not be registered in the Running Object Table (ROT). A running instance of an Office application must be registered in the ROT before it can be attached to using GetObject (Visual Basic) or GetActiveObject (Visual C++).

     

    Using code, you can change focus from the Office application to your own application (or to some other application) to allow it to register itself in the ROT. Additionally, if your code is launching the Office application's exe file, you might need to wait for the Office application to finish loading before attempting to attach to the running instance.  


    Sincerely,
    Eric

    • Proposed as answer by Ter-ray-cy Wednesday, April 14, 2010 9:51 AM
    • Proposed as answer by Ter-ray-cy Wednesday, April 14, 2010 9:51 AM
    • Marked as answer by SamAgain Friday, April 16, 2010 9:52 AM
    • Unmarked as answer by SamAgain Friday, April 16, 2010 9:52 AM
    • Marked as answer by eryang Tuesday, April 27, 2010 5:52 AM
    Thursday, April 8, 2010 10:41 AM
  • I have added

    this

     

    .Activate(); before the GetActiveObject even it is not working.

     

    But when i debug the code it is working fine only when i run without debug it is working also not throwing any exception.

    Friday, April 16, 2010 7:36 AM
  • Hi Subash,

     

     

    Could you please show your problem into more detail, so that I can reproduce the question and debug it in native.

    Thanks a lot! And have a nice day!

     


    Hope this helpful to you! If you have any further quetions, please feel free to let me know.
    Please mark the right answer at right time.
    Bset Regards,
    Tracy
    Friday, April 16, 2010 9:29 AM
  • Below am currently code which am working on.

    Functionality of requirement: When user is closed the powerpoint i need to capture the file properties and write into the text file

    When i run the below code in the  debug mode  it is working am able to capture the properties and write into the text file

    If i run with out debug some times throwing exception as Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))” or else no exception throwing but it is not writing into the textfile for both the case.

    Itis working fine Windows 2003 problem in Win XP OS.Please let me on this regards

     

    TmrPPTAppClsMon.Stop();

     

    bool blnTmrStts = TmrPPTAppClsMon.Enabled;

     

    //pptApp = null;

    pptApp =

    null;

     

    Thread.Sleep(1000);

     

     

    try

    {

     

    //It means that the object is already assigned a value

     

    if (iPPTPrcID != -1) {return; }

     

    Process[] prcArrPPT =Process.GetProcessesByName("POWERPNT");

     

    //Wait till the PPT Application initiates

     

    while (Process.GetProcessesByName("POWERPNT").Length != 1) { Thread.Sleep(500); }

     

     

    Object obj =null;

     

    obj = System.Runtime.InteropServices.

    Marshal.GetActiveObject("PowerPoint.Application");

     

    pptApp = (PowerPoint.

    Application)obj;

     

    GetWindowThreadProcessId(pptApp.HWND,

    out iPPTPrcID);

    pptApp.PresentationOpen +=

    new Microsoft.Office.Interop.PowerPoint.

     

    EApplication_PresentationOpenEventHandler(Presentation_Open);

    pptApp.SlideSelectionChanged +=

    new Microsoft.Office.Interop.PowerPoint.

     

    EApplication_SlideSelectionChangedEventHandler(SlideSelection_Changed);

    pptApp.PresentationClose +=

    new Microsoft.Office.Interop.PowerPoint.

     

    EApplication_PresentationCloseEventHandler(Presentation_Close);

    pptApp.PresentationSave +=

    new Microsoft.Office.Interop.PowerPoint.

     

    EApplication_PresentationSaveEventHandler(Presentation_Open);

     

    foreach (PowerPoint.Presentation PrstnItm in pptApp.Presentations)

    {

     

    while (Process.GetProcessesByName("POWERPNT").Length != 1) { Thread.Sleep(500); }

    Presentation_Open(PrstnItm);

    }

     

     

     

     

     

    }

    Friday, April 16, 2010 10:13 AM
  • Below am currently code which am working on.

    Functionality of requirement: When user is closed the powerpoint i need to capture the file properties and write into the text file

    When i run the below code in the  debug mode  it is working am able to capture the properties and write into the text file

    If i run with out debug some times throwing exception as Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))” or else no exception throwing but it is not writing into the textfile for both the case.

    Itis working fine Windows 2003 problem in Win XP OS.Please let me on this regards

     

    TmrPPTAppClsMon.Stop();

     

    bool blnTmrStts = TmrPPTAppClsMon.Enabled;

     

     

    //pptApp = null;

    pptApp =

    null;

     

     

    Thread.Sleep(1000);

     

    try

    {

     

    //It means that the object is already assigned a value

     

    if (iPPTPrcID != -1) {return; }

     

     

    Process[] prcArrPPT =Process.GetProcessesByName("POWERPNT");

     

     

    //Wait till the PPT Application initiates

     

    while (Process.GetProcessesByName("POWERPNT").Length != 1) { Thread.Sleep(500); }

     

    Object obj =null;

     

    Marshal.GetActiveObject("PowerPoint.Application");

     

    Application)obj;

     

    out iPPTPrcID);

    pptApp.PresentationOpen +=

     

    new Microsoft.Office.Interop.PowerPoint.

     

     

    EApplication_PresentationOpenEventHandler(Presentation_Open);

    pptApp.SlideSelectionChanged +=

     

    new Microsoft.Office.Interop.PowerPoint.

     

     

    EApplication_SlideSelectionChangedEventHandler(SlideSelection_Changed);

    pptApp.PresentationClose +=

     

    new Microsoft.Office.Interop.PowerPoint.

     

     

    EApplication_PresentationCloseEventHandler(Presentation_Close);

    pptApp.PresentationSave +=

     

    new Microsoft.Office.Interop.PowerPoint.

     

     

    EApplication_PresentationSaveEventHandler(Presentation_Open);

     

     

    foreach (PowerPoint.Presentation PrstnItm in pptApp.Presentations)

     

    while (Process.GetProcessesByName("POWERPNT").Length != 1) { Thread.Sleep(500); }

     

    Presentation_Open(PrstnItm);

    }

     

     

     

     

     

    }

    {

     

     

    GetWindowThreadProcessId(pptApp.HWND,

     

    pptApp = (PowerPoint.

     

    obj = System.Runtime.InteropServices.

     

     

     

     


    Friday, April 16, 2010 10:34 AM
  • The process needs some time to initiate itself, so the when condition 'Process.GetProcessesByName("POWERPNT").Length != 1' is false, the process itself still in invalid status, you may wait for a while and then call 'Marshal.GetActiveObject("PowerPoint.Application");'


    Sincerely,
    Eric
    • Marked as answer by eryang Tuesday, April 27, 2010 5:52 AM
    Friday, April 23, 2010 2:04 AM
  • Hi,

    It seems you got the right answer, right? So I marked it.

    If you have any further questions, you are surely welcome to ummark it and we will discuss this issue again.

    And have a nice day!


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 27, 2010 5:53 AM