Windows – PowerPoint 2013在导出OLE VBA命令后失去焦点

前端之家收集整理的这篇文章主要介绍了Windows – PowerPoint 2013在导出OLE VBA命令后失去焦点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建PowerPoint的OLE实例并向其发送命令:

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;

代码适用于所有PowerPoint版本(2000,2002,2003,2007,2010).

但PowerPoint 2013有时会失去焦点.我们无法再使用键盘更改幻灯片.我们必须点击全屏幻灯片才能恢复焦点.

评论:这是PowerPoint 2013中的官方错误吗?
答案:任何修复或解决方法

解决方法

解决此问题,请尝试在Application和SlideShowWindow上使用Activate:

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

  ppt.Activate();

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

  ppt.ActivePresentation.SlideShowWindow.Activate();

  ppt := Unassigned;
end;

如果这没有帮助,您应该能够获取ppt.HWND或ppt.ActivePresentation.SlideShowWindow.HWND(这是原始Windows句柄)并通过AttachThreadInput / SetForegroundWindow强制关注它,如我所描述的here.

根据注释更新,Activate方法没有解决问题,但AttachThreadInput / SetForegroundWindow显然是这样做的.来自linked answer代码覆盖了Windows策略,该策略防止焦点操作来自当前没有焦点的进程.这是通过将调用进程的线程输入队列(执行自动化的线程),自动进程(PowerPoint)和当前具有焦点的进程(可能与其他两个不同)连接在一起来实现的.

猜你在找的Windows相关文章