【发布时间】:2012-01-03 12:28:57
【问题描述】:
我正在使用 Coded UI 和 VSTO 自动化 Powerpoint 场景。在我的 powerpoint 演示文稿中,我在形状上创建了一个“操作”设置来启动记事本。在幻灯片放映期间,我需要通过单击“文本/形状”来调用此操作,以便它打开 notepad.exe。谁能帮助我如何实现这一目标。我写了以下代码。
//To launch Powepoint
PowerPoint.Application objPPT = new PowerPoint.Application();
objPPT.Visible = Office.MsoTriState.msoTrue;
//Add new presentation
PowerPoint.Presentations oPresSet = objPPT.Presentations;
PowerPoint.Presentation oPres = oPresSet.Add(Office.MsoTriState.msoTrue);
//Add a slide
PowerPoint.Slides oSlides = oPres.Slides;
PowerPoint.Slide oSlide = oSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
//Add text
PowerPoint.TextRange tr = oSlide.Shapes[1].TextFrame.TextRange;
tr.Text = "Launch notepad";
tr.Select();
//Add Action settings on the shape
oSlide.Shapes[1].ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;
oSlide.Shapes[1].ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = "c:\\windows\\notepad.exe";
//start slideshow
objPPT.ActivePresentation.SlideShowSettings.Run();
这将启动演示文稿的幻灯片放映,并显示第一张幻灯片“在形状上定义动作设置的位置”。现在如何通过 API 自动启动 notepad.exe?不幸的是,编码的 UI 无法检测幻灯片中的对象。因此,可能无法使用 UI 鼠标单击选项。
[编辑] 能够取得一点点进步。我在幻灯片放映期间得到了形状对象。这是对上述代码的扩展。
PowerPoint.SlideShowWindow oSsWnd = objPPT.ActivePresentation.SlideShowWindow;
PowerPoint.Shape oShape = oSsWnd.View.Slide.Shapes[1];
【问题讨论】:
-
我不清楚你想要完成什么。如果要创建演示文稿,在幻灯片放映视图中启动它,然后启动记事本,为什么要通过 PowerPoint 来创建?创建并启动 PPT 节目后,让您的代码启动记事本。
-
这是一个自动化场景,用于验证操作是否正常工作。因此我只能这样做
-
我明白了。我不知道有什么方法可以自动点击屏幕上的任何特定形状或点。
-
如果您只想在运行命令正确的情况下发短信,您可以枚举该形状和
Shell(ActionSettings[Click].Run)if Action = ppActionRunProgram -
Paul B,我不知道如何在这里使用 Shell 函数。我能否进一步详细说明。
标签: c# vba vsto powerpoint