2

I am trying to capture UI Automation events from PowerPoint 2007 (or ideally any version) when the user selects a new Ribbon tab. Using the SDK tools Inspect and AccEvent I have determined that a reasonable "parent" element to catch these events is the "Ribbon Tabs" element.

When I Scope AccEvent to that element, and register for SelectionItem_ElementSelected within the automation events, I get the events as I would expect - when a tab is clicked, AccEvent catches and logs it.

I am only allowed to post two links and can't inline images yet so I have made some mosaics to try and squeeze as much relevant info into each link as possible, here is the link relating to the above mentioned behavior:

http://hirstius.com/media/stackoverflow/UIA_sdk_tools.png

Based upon that, I came up with the following code to catch these events from my program:

// Prior code gets foreground window, determines if it's PPT, and gets a handle to it
currentApp = AutomationElement.FromHandle(foregroundWindow);

// Create condition to find the "Ribbon Tabs" element
Condition propCondition = new PropertyCondition(
  AutomationElement.NameProperty, "Ribbon Tabs",
  PropertyConditionFlags.IgnoreCase);

// Subscribe to events on the "Ribbon Tabs" Element
SubscribeToEvents(currentApp.FindFirst(TreeScope.Descendants, propCondition));

public void SubscribeToEvents(AutomationElement element)
{
  if (element != null)
  {
    Console.WriteLine("Subscribing to PowerPoint UIA Events on object {0} ({1})",
      elementItem.GetCurrentPropertyValue(AutomationElement.NameProperty),
      elementItem.GetCurrentPropertyValue(AutomationElement.AutomationIdProperty));
    UIAeventHandler = new AutomationEventHandler(OnUIAutomationEvent);

    // Subscribe to SelectionItemPattern.ElementSelectedEvent based off AccEvent
    Automation.AddAutomationEventHandler(
      SelectionItemPattern.ElementSelectedEvent,
      element,
      TreeScope.Descendants,
      UIAeventHandler);

    Console.WriteLine("Subscribed to PowerPoint UIA Events");
  }
}

private void OnUIAutomationEvent(object src, AutomationEventArgs e)
{
  // Make sure the element still exists
  AutomationElement sourceElement;
  try
  {
    sourceElement = src as AutomationElement;
  }
  catch (ElementNotAvailableException)
  {
    return;
  }
  Console.WriteLine("UIA Event ( {0} ) for item: {1}",
    e.EventId.ProgrammaticName,
    sourceElement.GetCurrentPropertyValue(AutomationElement.NameProperty));
}

This code results in, well, nothing.

If I subscribe to the top level "Window", still nothing.

If I instead just subscribe to the top level automation element, I DO get the expected events - but with a catch. In AccEvent, the events only appear when the tabs are clicked, truly "selected". When I bind to the Root AutomationElement, I get the events on mouseover/hover and nothing on click. I need the events to come only when the tab is actually selected (which is exactly the behavior AccEvent presents when Scoped to the "Ribbon Tabs" Element).

Results link: http://hirstius.com/media/stackoverflow/UIA_Result.png

I need a way to notify my .NET application when the user selects a new tab on the ribbon, am I missing something obvious?

Rex Remus
  • 171
  • 1
  • 9
  • Hi @rex-remus did you find the answer to this??? I have a similar requirement: [detect-events-on-powerpoint-shapes-from-c-sharp](http://stackoverflow.com/questions/16393918/detect-events-on-powerpoint-shapes-from-c-sharp) – Rafael Enriquez May 06 '13 at 14:42
  • 1
    I did not. And I'd like to get similar events on shapes that you are looking for. I'm not sure that C# operating externally can get this information via MSUIA - at least not without invoking a lot of unmanaged code to potentially intercept much lower level window interactions (which is NOT MSUIA anyway). Very interested if you have any success. – Rex Remus May 06 '13 at 18:42
  • I'm looking into the same thing right now. Would you mind posting the code that actually *does* work so we can compare? Also, there is no AutomationId on any of the tabs - what are you getting that for? – Todd Main May 31 '15 at 00:27
  • The code above is pretty much it. Sadly this was an old project for a previous company and I don't have access to the code any longer. The project ultimately died off for various other reasons, and I was never able to get the automation events I wanted before that happened. I wish you luck though and it would be great for the community if you could update this post with any possible solutions you may come across. – Rex Remus Jun 02 '15 at 18:42

0 Answers0