Creating text objects

Text objects are another type of Shape object. However, handling text is more complicated than handling other shapes.

In CorelDRAW, text is manipulated as a TextRange object. Text ranges can be accessed by using any of the following properties of Shape.Text:

• Frames collection — In this scenario, each TextFrame object's default member is a TextRange object, which is the text within the frame.

• Story — In this scenario, the TextRange object contains all of the text in all of the frames linked to the current Shape object on all of the pages in the document.

The text in a TextRange object can be manipulated as a single block of text, and its properties (such as font, size, and style) can be set simultaneously. Alternatively, the TextRange object has several properties that are collections of smaller text ranges: Columns, Paragraphs, Lines, Words, and Characters.

You can create two kinds of text: artistic text and paragraph text.

To create a new artistic text shape, use the member function CreateArtisticText of the Layer object. The following code creates an artistic text shape of the words Hello World, with the left end of the line at 1 inch and the baseline at 4 inches from the origin:

Dim sh As Shape

ActiveDocument.Unit = cdrInch

Set sh = ActiveLayer.CreateArtisticText(1, 4, "Hello World")

There are many optional parameters for this function, such that you can setting attributes such as italic, bold, size, and alignment.

To create a new paragraph text shape, use the member function CreateParagraphText of the Layer object. Paragraph text differs from artistic text in that it flows within a rectangular container, rather than being as wide as necessary before reaching a line feed. The first four parameters of the function are left, top, right, bottom:

Dim sh As Shape

ActiveDocument.Unit = cdrInch

Set sh = ActiveLayer.CreateParagraphText(1, 4, 5, 2, "Hi There", _

Alignment := cdrCenterAlignment)

You can format text. To do this, first get a reference to a TextRange object, and then apply the formatting to it. Frames, columns, paragraphs, lines, and the story can all be used to get a reference to a TextRange object.

The following code formats the first paragraph of the story into a heading style and the second and third paragraphs into a body-text style:

Dim txt As TextRange

' Format the first paragraph

Set txt = ActiveShape.Text.Story.Paragraphs(1)

txt.ChangeCase cdrTextUpperCase txt.Font = "Verdana"

txt.Bold = True

' Format the second and third paragraphs

Set txt = ActiveShape.Text.Story.Paragraphs(2, 2)

txt.Font = "Times New Roman"

txt.Style = cdrNormalFontStyle

All of the formatting options from the Format Text dialog box in CorelDRAW can be applied programmatically by using VBA. See the Text object in the VBA Object Browser for more information.

You can fit text to a path by using the Text object's member function FitToPath, which simply attaches a text object to the outline of a shape such that the text flows along the path.

The following code creates a new text object and attaches it to the selected shape: Dim sh As Shape, sPath As Shape ActiveDocument.Unit = cdrInch Set sPath = ActiveShape

Set sh = ActiveLayer.CreateArtisticText(1, 4, "Hello World") sh.Text.FitToPath sPath

Paragraph text shapes can be flowed inside closed shapes to form non-rectangular frames. This is done by placing the Text object inside the Shape object by using the member function PlaceTextInside of the Shape object. Given that a text shape — artistic or paragraph — is selected in CorelDRAW, the following code creates a 5-inch by 2 -inch ellipse and places the selected text inside it:

Dim txt As Shape, sh As Shape

ActiveDocument.Unit = cdrInch

Set txt = ActiveShape

Set sh = ActiveLayer.CreateEllipse(0, 2, 5, 0) sh.PlaceTextInside txt

+3 -1

Post a comment