Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerPoint 2013 loses focus after Export OLE VBA command

I create an OLE instance of PowerPoint and send commands to it:

procedure ExportSlide(const SlideIndex : Integer);
var
  ppt : Variant;
begin
  ppt := GetActiveOleObject('Powerpoint.Application');  
  ppt.ActivePresentation.Slides.Item(SlideIndex).Export('c:\test.png', 'PNG', 640, 480);
  ppt := Unassigned;
end;

This code works very well with all PowerPoint versions (2000, 2002, 2003, 2007, 2010).

However PowerPoint 2013 sometimes loses focus. We can no longer change slides using keyboard. We must click on full screen slide to restore focus.

Comments: Is it an official bug in PowerPoint 2013?
Answers: Any fix or workaround?

like image 442
rjobidon Avatar asked Sep 30 '22 21:09

rjobidon


1 Answers

As a workaround, try playing with Activate on Application and SlideShowWindow:

procedure ExportSlide(const SlideIndex : Integer);
var
  ppt : Variant;
begin
  ppt = GetActiveOleObject('Powerpoint.Application');  

  ppt.Activate();

  ppt.ActivePresentation.Slides.Item(SlideIndex).Export('c:\test.png', 'PNG', 640, 480);

  ppt.ActivePresentation.SlideShowWindow.Activate();

  ppt := Unassigned;
end;

If that doesn't help, you should be able to take ppt.HWND or ppt.ActivePresentation.SlideShowWindow.HWND (which is the raw Windows handle) and force the focus to it via AttachThreadInput/SetForegroundWindow as I described here.

Updated based on the comment, the Activate method doesn't solve the problem, but the AttachThreadInput/SetForegroundWindow one apparently does. The code from linked answer overrides the Windows policy of preventing the focus manipulation from a process which doesn't currently have the focus itself. This is achieved by attaching together the thread input queues of the calling process (the one which is performing the automation), the process being automated (PowerPoint) and the process which currently has the focus (which might be different from the other two).

like image 174
noseratio Avatar answered Oct 04 '22 04:10

noseratio