2

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.

Robbie Dee
  • 1,939
  • 16
  • 43
Sahil
  • 491
  • 2
  • 9
  • 18

2 Answers2

2

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.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • I did change the index of shape. But when I run the program it gave me error Shapes (unknown member) : Integer out of range. 2 is not in the valid range of 1 to 1. – Sahil Sep 26 '12 at 20:46
  • Then you should append the first and second pieces of text to fit them in the same shape rather than replace the text in both the first and second block. – Justin Niessner Sep 26 '12 at 20:47
  • I also try to use this with different layouts such as blank slide. I am facing the same problem over there as well. – Sahil Sep 26 '12 at 20:48
  • I can not do that as I have to add chart in between two text. I have to write text in top then display chart and below that I need to write text. – Sahil Sep 26 '12 at 20:49
  • @Sahil - Then I would suggest looking up documentation on how to create a new shape programatically and add it to the sheet. – Justin Niessner Sep 26 '12 at 20:50
  • Is there any other way through that I can write multiple text at different position. – Sahil Sep 26 '12 at 20:50
  • @JustinNiessner:http://stackoverflow.com/questions/18433059/writing-custom-code-for-powerpoint any help? – HIRA THAKUR Aug 30 '13 at 07:58
1

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.

Christian Sauer
  • 10,351
  • 10
  • 53
  • 85