I seached but couldn't find the answer to this, so I apologize if this is a duplicate.
When assigning text to a slide portion I see we cannot use vbCrLf, vbCr, vbLf (causes an error) or \r, \n (shows up as literal text). So, how do I add a carriage return to the text in a Slide portion?
The simple things have become so difficult recently. I 'm using a template with various formats in the header, center, and footer. I want to add four words to the header (with a CR) WITHOUT losing the formatting of the existing textframe.
Formatting of the text is preserved by Portion object, I mean; it is preserved only, when you set the text with Portion.Text property and lost, if you set it via TextFrame.Text or Paragraph.Text properties.
I think, this is what you want, please see the code below and output presentation attached by me. Source presentation is also attached.
VB.NET CODE
Dim pres As Presentation = New Presentation("c:\srcPres.ppt")
Dim sld As Slide = pres.GetSlideByPosition(1)
Dim shp As Shape = sld.Shapes(0)
Dim tf As TextFrame = shp.TextFrame
Dim paras As Paragraphs = tf.Paragraphs
Dim para As Paragraph = paras(0)
'Now add two lines with the same formatting
Dim newPara As Paragraph = New Paragraph(para)
newPara.Portions(0).Text = "Range Instrumentation"
paras.Add(newPara)
'One more
newPara = New Paragraph(para)
newPara.Portions(0).Text = "Maintenance Status"
paras.Add(newPara)
pres.Write("c:\outPres.ppt")
No, to keep your formatting intact, you will have to get and save the copy of one portion object before clearing and place it back. e.g
Note:Clear method just erase all the objects from collections like from TextFrame.Paragraphs or TextFrame.Paragraphs[0].Portions
Portion oldPort = thld.Paragraphs[0].Portions[0];
Portion newPort = new Portion(oldPort);
//Clear the existing text cloned from oldPort
//leave one space or one character
newPort.Text =" ";
//Clear all paragraphs
//it will clear all text
thld.Paragraphs.Clear();
//Add new paragraph and new portion
Paragraph newPara = new Paragraph();
newPara.Portions.Add(newPort);
thld.Paragraphs.Add(newParagraph);