Reputation: 511
I want to write text in powerpoint through automation in c#.
I am using Microsoft.Office.Interop.PowerPoint for that.
My sample code:
objSlide = objSlides.Add(1, PowerPoint.PpSlideLayout.ppLayoutCustom);
objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
objTextRng.Text = "first text";
objTextRng.Font.Name = "Calibri";
objTextRng.Font.Size = 20;
objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
objTextRng.Text = "second text";
objTextRng.Font.Name = "Calibri";
objTextRng.Font.Size = 20;
When I tried to run this code it will give me output for only second textrange which is "second text".
What do I need to do if I want to display both the text in same slide.
I also tried to use different textrange
, textframe
but I am not able to do the same.
Upvotes: 2
Views: 4619
Reputation: 10889
As vb.net code:
Dim n as Integer =2
for i = 1 to 2
Dim Orientation As Microsoft.Office.Core.MsoTextOrientation = Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal
Dim STextLeft As Single = 100*i
Dim STextWidth As Single = 100
Dim STextHeight As Single = 100
Dim STextTop As Single = 100*i
Dim TargetShape = objslide.shapes.AddTextbox(Orientation, STextLeft, STextTop, STextWidth, STextHeight)
TargetShape.textframe.textrange=i.tostring
Orientation=nothing
targetshape=nothing
next
This will create two textshapes. Please not that you have to set Orientation and Targetshape to nothing, simply because COM is crappy as hell. If you do not, powerpoint will stay open. You may have to call
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
at the end of your programm, too.
Upvotes: 1
Reputation: 245389
You're having issues because you access the same shape in both blocks of code:
objSlide.Shapes[1].TextFrame.TextRange;
Depending on how many shapes are in the slide, you may want the first block to reference index 0 or the second block to reference index 2. Either way, both blocks should be referencing different shapes.
Upvotes: 2