【问题标题】:C# powerpoint edit smartart textC# powerpoint 编辑 smartart 文本
【发布时间】:2020-04-20 03:23:47
【问题描述】:

我目前正在尝试替换 powerpoint smartart 对象中的一些文本。我可以获取文本,但我无法设置文本值,即使很多文档看起来很简单。

我正在使用

shp.SmartArt.AllNodes[i].TextFrame2.TextRange.Text = shapeString;

设置文本字符串,但无论我使用什么函数,我都会遇到错误

shp.SmartArt.AllNodes[i].TextFrame2.DeleteText();

返回与其他消息相同的错误消息。我可能需要让 powerpoint 可见,但由于某种原因,当我让它可见时,它会消失。 (杀毒?)

我可以用标准文本框替换 powerpoint 中的文本,因此我尝试重复 smartart 的步骤,但没有成功。

**************异常文本**************

System.Runtime.InteropServices.COMException (0x80004005):对 COM 组件的调用已返回错误 HRESULT E_FAIL。 在 Microsoft.Office.Core.TextRange2.set_Text(字符串 pbstrText) 在 *.Form1.BtnStart_Click(Object sender, EventArgs e) 中 *\Form1.cs:第 414 行 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.PerformClick() 在 System.Windows.Forms.Form.ProcessDialogKey(键 keyData) 在 System.Windows.Forms.Control.ProcessDialogKey(键 keyData) 在 System.Windows.Forms.Control.ProcessDialogKey(键 keyData) 在 System.Windows.Forms.Control.ProcessDialogKey(键 keyData) 在 System.Windows.Forms.Control.PreProcessMessage(消息和消息) 在 System.Windows.Forms.Control.PreProcessControlMessageInternal(控制目标,消息和消息) 在 System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG&msg)

**************加载的程序集**************

mscorlib 程序集版本:4.0.0.0 Win32 版本:4.8.4075.0 构建者:NET48REL1LAST 代码库:file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll

System.Windows.Forms 程序集版本:4.0.0.0 Win32 版本:4.8.4042.0 构建者:NET48REL1LAST_C 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_0.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

系统 程序集版本:4.0.0.0 Win32 版本:4.8.4001.0 构建者:NET48REL1LAST_C 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c> 561934e089/System.dll

系统.绘图 程序集版本:4.0.0.0 Win32 版本:4.8.3752.0 构建者:NET48REL1 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0> __b03f5f7f11d50a3a/System.Drawing.dll

系统.配置 程序集版本:4.0.0.0 Win32 版本:4.8.3752.0 构建者:NET48REL1 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

System.Core 程序集版本:4.0.0.0 Win32 版本:4.8.4075.0 构建者:NET48REL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b> 77a5c561934e089/System.Core.dll

System.Xml 程序集版本:4.0.0.0 Win32 版本:4.8.3752.0 构建者:NET48REL1 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b7> 7a5c561934e089/System.Xml.dll

Microsoft.CSharp 程序集版本:4.0.0.0 Win32 版本:4.8.3752.0 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0> .0__b03f5f7f11d50a3a/Microsoft.CSharp.dll

系统.动态 程序集版本:4.0.0.0 Win32 版本:4.8.3752.0 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0> __b03f5f7f11d50a3a/System.Dynamic.dll

匿名托管的 DynamicMethods 程序集 程序集版本:0.0.0.0 Win32 版本:4.8.4075.0 构建者:NET48REL1LAST 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll

自定义编组器 程序集版本:4.0.0.0 Win32 版本:4.8.3752.0 构建者:NET48REL1 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/CustomMarshalers/v4.0_4.0.0.0__b03f5f7f11d50a3a/CustomMarshalers.dll

Microsoft.Office.Interop.PowerPoint 程序集版本:15.0.0.0 Win32版本:15.0.4569.1507 代码库:file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.Office.Interop.PowerPoint/15.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.PowerPoint.dll

办公室 程序集版本:15.0.0.0 Win32版本:15.0.4613.1000 代码库: file:///C:/WINDOWS/assembly/GAC_MSIL/office/15.0.0.0__71e9bce111e9429c/office.dll

System.Dynamic.DynamicAssembly 程序集版本:0.0.0.0 Win32 版本:4.8.3752.0 代码库:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Dynamic.dll

不知道下一步该去哪里。

*************文档/参考*************

https://help.syncfusion.com/file-formats/presentation/smartart

//Open a PowerPoint Presentation
IPresentation pptxDoc = Presentation.Open("SampleDocument.pptx");

//Traverse through shape in the first slide.
foreach (IShape shape in pptxDoc.Slides[0].Shapes)
{
if (shape is ISmartArt)
{
//Traverse through all nodes inside SmartArt
foreach (ISmartArtNode mainNode in (shape as ISmartArt).Nodes)
{
if (mainNode.TextBody.Text == "Old Content")
//Change the node content
mainNode.TextBody.Paragraphs[0].TextParts[0].Text = "New Content";
}
}
}

//Save the Presentation.
pptxDoc.Save("SmartArt.pptx");

//Close the Presentation.
pptxDoc.Close();

【问题讨论】:

    标签: c# com powerpoint office-interop


    【解决方案1】:

    我最终使用 openxml 来解决这个问题。不幸的是,我在 openxml 和 interop 之间切换,直到找到更好的解决方案。

    using (PresentationDocument doc = PresentationDocument.Open(textBox1.Text, true))
    {
           PresentationPart pp = doc.PresentationPart;
           SlidePart sp1 = pp.SlideParts.ToList<SlidePart>()[0];
           foreach (DiagramDataPart ddp in sp1.DiagramDataParts.ToList<DiagramDataPart>())
           {
                 DataModelRoot dmr = ddp.DataModelRoot;
                 List<PointList> pl = dmr.Descendants<PointList>().ToList();
                 foreach (PointList item in pl)
                 {
                        List<Point> ps = item.Descendants<Point>().ToList();
                        foreach (Point p in ps)
                        {
                              if (p.InnerText.Equals("_HELLO_"))
                              {
                                    DocumentFormat.OpenXml.Drawing.Paragraph para = p.TextBody.GetFirstChild<DocumentFormat.OpenXml.Drawing.Paragraph>();
                                    DocumentFormat.OpenXml.Drawing.Run run1 = para.GetFirstChild<DocumentFormat.OpenXml.Drawing.Run>();
                                    DocumentFormat.OpenXml.Drawing.Text text1 = run1.GetFirstChild<DocumentFormat.OpenXml.Drawing.Text>();
                                    text1.Text = "World";
                                    var textreplacement = p.InnerText;
                              }
                        }
                  }
            }
            doc.Save();
    }
    

    【讨论】:

      【解决方案2】:

      我不使用 Interop,但您可以先尝试测试节点是否具有文本,然后再更改它。某些 SmartArt 变体具有无法以编程方式设置的文本节点,因此尝试在其上添加文本总是会失败。正在使用此 VBA 在此处设置 SmartArt 文本:

      If ActivePresentation.Slides(1).Shapes(1).SmartArt.AllNodes(X).TextFrame2.HasText Then
          ActivePresentation.Slides(1).Shapes(1).SmartArt.AllNodes(X).TextFrame2.TextRange.Text = "Text"
      End If
      

      【讨论】:

      • 这可能与我没有选择活动幻灯片有关。我相信 ppt 在我处理这个问题时会在后台运行。
      • PowerPoint 仅在您使用选择时才需要显示活动幻灯片。如果您使用范围,则根本不需要显示演示文稿。
      猜你喜欢
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      相关资源
      最近更新 更多