1

我一直在努力使用 C# 和 PowerPoint,我之前发布过关于如何使用 C# 更新 ppt 中的链接,如果链接设置为手动,我没有得到任何回复,所以我想我会尝试通过设置链接来规避这个问题自动在文件中,然后当 C# 打开它时,它会更新它们,保存文件,然后破坏它们并将其另存为另一个文件名,但这并没有证明更容易。

我只需要知道如何断开链接。我知道一些 VBA 并编写了一个代码来破坏它们,但是当我使用 RunMacro 方法在 C# 中调用宏时,它似乎不适用于我正在使用的方法(? - 我是 C# 新手,所以我不'不完全理解为什么会这样,但如果你谷歌“运行宏 PowerPoint C#”,你会发现我确信我尝试过的方法。)请帮助,我完全不知所措。

我的脚本看起来像这样

Using PowerPoint = Microsoft.Office.Interop.PowerPoint;

public void Main()
    {

        PowerPoint.Application ppt = new PowerPoint.Application();
        PowerPoint.Presentation PRS = ppt.Presentations.Open(@"Filename.pptm",
        Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);


        PRS.UpdateLinks();
        PRS.Save
        //here is where I need to break the links
        PRS.SaveAs(@"filename with links broken.pptm", 
            Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentationMacroEnabled, MsoTriState.msoTrue);
        PRS.Close(); 
       ppt.Quit();
}

我尝试在打开文件之前将链接格式设置为手动,但这不会影响已经创建的任何形状,只会影响之后在程序中创建的新形状。

4

2 回答 2

2

我在 Powerpoint 中做了一个类似的项目,其中也涉及断开链接。在我的程序中,我从 Excel 文件中读取数据并将其放入 Powerpoint 演示文稿中。在我的 Powerpoint 模板文件中,我将所有指向我的 Excel 文件的链接按我希望的方式布置和格式化。程序开始运行并填充 Excel 文件。写入 Excel 完成后,我打开 Powerpoint 和 UpdateLinks() 到我的演示文稿。更新链接后,我会在 Powerpoint 中的 Shapes 上使用 for 循环断开链接。之后,我保存并关闭文档。下面是我用来根据我的每个 ppt 文件模板创建我的 Powerpoint 文件的函数(对于我的示例,我遍历多个 ppt 文件,因为每个 ppt 文件只包含一张幻灯片。

public void CreatePowerpoint()
    {
        string[] fileArray = Directory.GetFiles(@"C:\Users\Me\Desktop\test");
        Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
        for (int i = 0; i < fileArray.Length; i++)
        {
            string file = fileArray[i];

            Microsoft.Office.Interop.PowerPoint.Presentation powerpoint = pptApp.Presentations.Open(file);
            powerpoint.UpdateLinks();

            Microsoft.Office.Interop.PowerPoint.Slides slides = powerpoint.Slides;
            Microsoft.Office.Interop.PowerPoint.Slide slide = slides[1];
            Microsoft.Office.Interop.PowerPoint.Shapes pptShapes = (Microsoft.Office.Interop.PowerPoint.Shapes)slide.Shapes;

            foreach (Microsoft.Office.Interop.PowerPoint.Shape y in pptShapes)
            {
                Microsoft.Office.Interop.PowerPoint.Shape j = (Microsoft.Office.Interop.PowerPoint.Shape)y;
                try
                {
                    //If auto link update is disabled for links
                    //j.LinkFormat.j.Update();
                    if (j.LinkFormat != null)
                    {
                        j.LinkFormat.BreakLink();
                    }
                }
                catch (Exception)
                {

                }
            }

            powerpoint.SaveAs(@"C:\Users\Me\Desktop\test" + i + ".pptx");
            powerpoint.Close();
            Thread.Sleep(3000);
        }
        pptApp.Quit();
    }
于 2015-03-06T20:41:21.570 回答
-1

当然,在打开文件之前更改内容不会影响未打开的文件。遍历每张幻灯片上的形状集合和每个形状:

If .Type = msoLinkedOLEObject Then
    .LinkFormat.BreakLink
End If

msoLinkedOLEObject = 10

于 2012-12-12T02:21:22.487 回答