如何以编程方式在powerpoint幻灯片放映期间调用操作?

时间:2012-01-03 12:28:58

标签: c# vba vsto powerpoint powerpoint-vba

我正在使用Coded UI& amp;来自动化Powerpoint场景。 VSTO。在我的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];

2 个答案:

答案 0 :(得分:7)

不要问我为什么C#表现得像这样,但确实如此!

您必须发出两次命令才能正常工作......

尝试和测试

    private void button1_Click(object sender, EventArgs e)
    {
        //To launch Powepoint
        PowerPoint.Application objPPT = new PowerPoint.Application();
        objPPT.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;

        //Add new presentation
        PowerPoint.Presentations oPresSet = objPPT.Presentations;
        PowerPoint.Presentation oPres = oPresSet.Add(Microsoft.Office.Core.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\system32\notepad.exe";
        oSlide.Shapes[1].ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Action = PowerPoint.PpActionType.ppActionRunProgram;
        oSlide.Shapes[1].ActionSettings[PowerPoint.PpMouseActivation.ppMouseClick].Run = @"C:\WINDOWS\system32\notepad.exe";
        //start slideshow
        objPPT.ActivePresentation.SlideShowSettings.Run();

    }

HTH

西特

答案 1 :(得分:5)

这可能是一个比你希望的更复杂的解决方案,但如果你能以某种方式确定屏幕上“文本/形状”对象的X和Y坐标(可能使用Coded UI和VSTO库?) ,您可以使用User32“SendInput”方法模拟将鼠标移动到对象的位置,然后模拟鼠标单击。

以下是模拟用户输入的代码:

int x, y;
// ...  First obtain the X and Y coordinate of the "text/shape" object from APIs

//
InputEmulator inputEmulator = new InputEmulator();
inputEmulator.MoveMouse(x, y);
inputEmulator.ClickMouse();

这是我用来模拟Windows UI操作的InputEmulator类的精简版本:

class InputEmulator
{
    private const int INPUT_MOUSE = 0;
    private const uint MOUSEEVENTF_MOVE = 0x0001;
    private const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
    private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const uint MOUSEEVENTF_LEFTUP = 0x0004;

    public void MoveMouse(int x, int y)
    {
        INPUT[] inp = new INPUT[1];
        inp[0].type = INPUT_MOUSE;
        inp[0].mi = createMouseInput(x, y, 0, 0, MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE);


        SendInput((uint)1, inp, Marshal.SizeOf(inp[0].GetType()));
    }

    public void ClickMouse()
    {
        INPUT[] inp = new INPUT[2];
        inp[0].type = INPUT_MOUSE;
        inp[0].mi = createMouseInput(0, 0, 0, 0, MOUSEEVENTF_LEFTDOWN);
        inp[1].type = INPUT_MOUSE;
        inp[1].mi = createMouseInput(0, 0, 0, 0, MOUSEEVENTF_LEFTUP);
        SendInput((uint)inp.Length, inp, Marshal.SizeOf(inp[0].GetType()));
    }

    [DllImport("user32.dll", SetLastError = true)]
    private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

    private static MOUSEINPUT createMouseInput(int x, int y, uint data, uint t, uint flag)
    {
        MOUSEINPUT mi = new MOUSEINPUT();
        mi.dx = x;
        mi.dy = y;
        mi.mouseData = data;
        mi.time = t;
        //mi.dwFlags = MOUSEEVENTF_ABSOLUTE| MOUSEEVENTF_MOVE;
        mi.dwFlags = flag;
        return mi;
    }

    [StructLayout(LayoutKind.Explicit)]
    private struct INPUT
    {
        [FieldOffset(0)]
        public int type;
        [FieldOffset(sizeof(int))] //[FieldOffset(8)] for x64
        public MOUSEINPUT mi;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public uint mouseData;
        public uint dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
    }
}