Cindy Meister
Hi Jan
ah, but you see, TextRange2 is a new object that is definitely linked to the new graphics engine. It may appear to you that you're working with the same "Shape" as in previous versions, but in fact you are not. The "AutoShapes" accessed via the user interface are part of the new graphics engine...
Have you tried reading the Help for TextRange2 via Excel's VBA Editor That explains what these things are, and from the look of it, it builds on the WordProcessingXML and uses the basic approach of working with formatted text in Word.
The following takes an existing AutoShape on the ActiveSheet and adds some text. This creates one "Run" in the TextRange. It makes the second word red, which creates three runs. Inserting a paragraph mark before the third word (which is coincidently the third Run) creates a second paragraph, but you still have only three runs at this point.
Dim shp As Excel.Shape
Set shp = ActiveSheet.Shapes(1)
With shp.TextFrame2.TextRange
.Text = "Testing things out"
.Words(2).Font.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Runs(3).InsertBefore vbCr
Debug.Print .Runs.Count
.Paragraphs(2).ParagraphFormat.Alignment = msoAlignRight
End With
That's the simple way of looking at it. Just as when working in Word, you get more control if you work more closely with the object model, as the following bit of sample code. Note carefully the use of the TextRange2 object (corresponding to the Range object in Word). But you really should be exploring this in the excel.programming newsgroup, where the people are more familiar with the object model. This isn't really a VSTO question :-)
Sub TestShapeAttributes()
Dim shp As Excel.Shape
Dim rng As Office.TextRange2
Dim rngWord2 As Office.TextRange2
Dim rngRun3 As Office.TextRange2
Dim rngPara As Office.TextRange2
Dim fnt As Office.Font2
Set shp = ActiveSheet.Shapes(1)
Set rng = shp.TextFrame2.TextRange
rng.Text = "TEsting things out"
Set rngWord2 = rng.Words(2)
Set fnt = rngWord2.Font
With fnt
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Bold = msoTrue
End With
Set rngRun3 = rng.Runs(3)
rngRun3.InsertBefore vbCr
Set rngPara = rng.Paragraphs(2)
rngPara.ParagraphFormat.Alignment = msoAlignRight
Debug.Print rng.Runs.Count
End Sub