Reputation: 11
I am currently trying to replace some text in a powerpoint smartart object. I can get the text but I am unable to set the text value even when a lot of documents make it seem very easy.
I am using
shp.SmartArt.AllNodes[i].TextFrame2.TextRange.Text = shapeString;
to set the text string but I am coming up with an error no matter what function I use
shp.SmartArt.AllNodes[i].TextFrame2.DeleteText();
returns the same error message as the other message. I may need to make the powerpoint visible but for some reason when I make it visible it disappears. (anti-virus?)
I can replace text in the powerpoint with standard text boxes so I am trying to repeat the steps for smartart with no success.
************** Exception Text **************
System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component. at Microsoft.Office.Core.TextRange2.set_Text(String pbstrText) at *.Form1.BtnStart_Click(Object sender, EventArgs e) in *\Form1.cs:line 414 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.PerformClick() at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData) at System.Windows.Forms.Control.PreProcessMessage(Message& msg) at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg) at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
************** Loaded Assemblies **************
mscorlib Assembly Version: 4.0.0.0 Win32 Version: 4.8.4075.0 built by: NET48REL1LAST CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
System.Windows.Forms Assembly Version: 4.0.0.0 Win32 Version: 4.8.4042.0 built by: NET48REL1LAST_C CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_0.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
System Assembly Version: 4.0.0.0 Win32 Version: 4.8.4001.0 built by: NET48REL1LAST_C CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c> 561934e089/System.dll
System.Drawing Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 built by: NET48REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0> __b03f5f7f11d50a3a/System.Drawing.dll
System.Configuration Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 built by: NET48REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
System.Core Assembly Version: 4.0.0.0 Win32 Version: 4.8.4075.0 built by: NET48REL1LAST CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b> 77a5c561934e089/System.Core.dll
System.Xml Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 built by: NET48REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b7> 7a5c561934e089/System.Xml.dll
Microsoft.CSharp Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0> .0__b03f5f7f11d50a3a/Microsoft.CSharp.dll
System.Dynamic Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0> __b03f5f7f11d50a3a/System.Dynamic.dll
Anonymously Hosted DynamicMethods Assembly Assembly Version: 0.0.0.0 Win32 Version: 4.8.4075.0 built by: NET48REL1LAST CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll
CustomMarshalers Assembly Version: 4.0.0.0 Win32 Version: 4.8.3752.0 built by: NET48REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/CustomMarshalers/v4.0_4.0.0.0__b03f5f7f11d50a3a/CustomMarshalers.dll
Microsoft.Office.Interop.PowerPoint Assembly Version: 15.0.0.0 Win32 Version: 15.0.4569.1507 CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.Office.Interop.PowerPoint/15.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.PowerPoint.dll
office Assembly Version: 15.0.0.0 Win32 Version: 15.0.4613.1000 CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/office/15.0.0.0__71e9bce111e9429c/office.dll
System.Dynamic.DynamicAssembly Assembly Version: 0.0.0.0 Win32 Version: 4.8.3752.0 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Dynamic.dll
Not sure where to go next.
*************Documentation/Reference*************
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();
Upvotes: 1
Views: 898
Reputation: 11
I ended up using openxml to work around this issue. I am unfortunatly switching between openxml and interop until I can find a better fix.
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();
}
Upvotes: 0
Reputation: 4923
I don't work with Interop, but you might try testing first whether a node has text first before changing it. Some SmartArt variants have text nodes that can't be set programmatically, so trying to add text on them will always fail. This is working to set SmartArt text here, using this VBA:
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
Upvotes: 0