【问题标题】:Break Links PowerPoint 2010断开链接 PowerPoint 2010
【发布时间】:2015-12-04 00:54:42
【问题描述】:

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

我只需要知道如何断开链接。我知道一些 VBA 并编写了一个代码来破坏它们,但是当我在 C# 中使用 RunMacro 方法调用宏时,它似乎不适用于我正在使用的方法(?-我是 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();
}

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

【问题讨论】:

    标签: c# powerpoint


    【解决方案1】:

    我在 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();
        }
    

    【讨论】:

      【解决方案2】:

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

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

      msoLinkedOLEObject = 10

      【讨论】:

        猜你喜欢
        • 2012-11-30
        • 2012-03-30
        • 1970-01-01
        • 2020-05-06
        • 1970-01-01
        • 1970-01-01
        • 2012-08-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多