使用 C# 更新 PowerPoint 中文件的链接

问题描述 投票:0回答:1

我正在使用 Excel 和 PowerPoint 模板构建自动报告,在保存到新文件路径之前我正在用 C# 编辑这些模板。我的 PowerPoint 模板嵌入了链接到 Excel 模板的对象(例如单元格范围和图表)。我想以编程方式更新这些对象链接到的 Excel 文件。这相当于进入 PowerPoint 文档,单击文件 -> 信息 -> 编辑文件链接 -> 更改源。

我目前正在尝试使用 NetOfficeFw.PowerPoint 库进行此操作,但到目前为止尚未成功。这是我最近尝试的一个例子:

public void UpdateLinks(string templatePath, string excelWorkbookPath)
{
     var application = new PowerPoint.Application();
     var presentation = application.Presentations.Open(templatePath, MsoTriState.msoFalse);

     foreach (PowerPoint.Slide slide in presentation.Slides)
     {
          foreach (var shape in slide.Shapes)
          {
               if (ShapeIsLinked(shape))
               {
                    shape.LinkFormat.SourceFullName = excelWorkbookPath;
               }
          }
     }
}

private bool ShapeIsLinked(PowerPoint.Shape shape)
{
     return shape.Type == MsoShapeType.msoLinkedPicture
         || shape.Type == MsoShapeType.msoLinked3DModel
         || shape.Type == MsoShapeType.msoLinkedGraphic
         || shape.Type == MsoShapeType.msoLinkedOLEObject;
}

这会引发错误:

NetOffice.Exceptions.PropertySetCOMException (0x80004005): Failed to proceed PropertySet on PowerPoint.LinkFormat=>SourceFullName. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: LinkFormat.SourceFullName : Failed.

如果需要,我对其他库开放,但最好是开源或更便宜的选项来实现这一点。谢谢!

c# powerpoint netoffice
1个回答
0
投票

所以我发现传递给 SourceFullName 的 excel 路径必须是现有文件。我传递了一个虚拟路径进行测试,只是为了看看它是否会真正更新路径,并且我假设它不会检查文件是否存在,直到我实际尝试打开 powerpoint 文件并更新链接。事实并非如此,它需要现有文件的路径。

© www.soinside.com 2019 - 2024. All rights reserved.