我创建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).
解决方法
要解决此问题,请尝试在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)和当前具有焦点的进程(可能与其他两个不同)连接在一起来实现的.