gpt4 book ai didi

c# - 如何以编程方式在 powerpoint 幻灯片放映期间调用 Action ?

转载 作者:可可西里 更新时间:2023-11-01 03:10:20 24 4
gpt4 key购买 nike

我正在使用编码的 UI 和 VSTO 自动化 Powerpoint 场景。在我的 powerpoint 演示文稿中,我在形状上创建了一个“ Action ”设置以启动记事本。在幻灯片放映期间,我需要通过单击“文本/形状”来调用此操作,以便它打开 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();

这将启动演示文稿的幻灯片,并且将显示第一张幻灯片“在形状上定义 Action 设置的地方”。现在如何通过 API 自动启动 notepad.exe?不幸的是,编码的 UI 无法检测到幻灯片中的对象。因此可能无法使用 UI 鼠标单击选项。

[编辑]能够取得一点点进步。我在幻灯片放映期间有形状对象。这是对上述代码的扩展。

PowerPoint.SlideShowWindow oSsWnd = objPPT.ActivePresentation.SlideShowWindow;
PowerPoint.Shape oShape = oSsWnd.View.Slide.Shapes[1];

最佳答案

不要问我为什么 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();

}

关于c# - 如何以编程方式在 powerpoint 幻灯片放映期间调用 Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8712311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com