OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Writer -- Simple Macro For Basic Letter Creation

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Morgen
Newbie
Newbie


Joined: 19 Jul 2010
Posts: 3

PostPosted: Mon Jan 31, 2011 5:45 pm    Post subject: Writer -- Simple Macro For Basic Letter Creation Reply with quote

I had a hard time finding information on simple text manipulation for creating form letters, so I figured I would post the results. This macro shows how to enter text, change paragraph styles, numbering, bold, italics and underlining. Using these calls directly to the API makes for much simpler macro compared to creating a dispatcher as the macro recorder would do. If anyone with more experience has any pointers or insight they would be welcome.

Code:
Sub Main

'Access the current document.
Dim Doc As Object
Doc = ThisComponent

'Create a cursor to enter data through.
Dim Cursor As Object
Cursor = Doc.Text.createTextCursor
'Make the cursor behave like a typical visible cursor.
Cursor.gotoEnd(False)

'Change the Paragraph Style.
Cursor.ParaStyleName = "Heading 3"
'Insert text at the cursor location.
Doc.Text.insertString(Cursor, "This is Heading 3 style.", False)
'Move to next line, equal to pressing the Enter key.
Doc.Text.insertControlCharacter(Cursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
Cursor.ParaStyleName = "Default"

'Change to a Numbering Style.
Cursor.NumberingStyleName = "List 1"
Doc.Text.insertString(Cursor,"This is List 1 numbering.", False)
Doc.Text.insertControlCharacter(Cursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
Cursor.NumberingStyleName = "NONE"

'Set the font to Bold.
Cursor.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.BOLD)
Doc.Text.insertString(Cursor, "This should be a Bold sentence.", False)
Doc.Text.insertControlCharacter(Cursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
Cursor.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.NORMAL)

'Change to Italicized font style.
Cursor.setPropertyValue("CharPosture", com.sun.star.awt.FontSlant.ITALIC)
Doc.Text.insertString(Cursor, "This sentence should be italicized", False)
Doc.Text.insertControlCharacter(Cursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
Cursor.setPropertyValue("CharPosture", com.sun.star.awt.FontSlant.NONE)

'Set the font to underlined.
Cursor.setPropertyValue("CharUnderline", com.sun.star.awt.FontUnderline.SINGLE)
Doc.Text.insertString(Cursor, "This sentence should be underlined.", False)
Doc.Text.insertControlCharacter(Cursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
Cursor.setPropertyValue("CharUnderline", com.sun.star.awt.FontUnderline.NONE)

End Sub


For additional information on the possible values to input for control characters or bold, italicized and underlined text see the follow API reference pages:

http://api.openoffice.org/docs/common/ref/com/sun/star/text/ControlCharacter.html
http://api.openoffice.org/docs/common/ref/com/sun/star/awt/FontWeight.html
http://api.openoffice.org/docs/common/ref/com/sun/star/awt/FontSlant.html
http://api.openoffice.org/docs/common/ref/com/sun/star/awt/FontUnderline.html

Hope this is helpful information.

Morgen
Back to top
View user's profile Send private message
Morgen
Newbie
Newbie


Joined: 19 Jul 2010
Posts: 3

PostPosted: Tue Feb 08, 2011 5:20 pm    Post subject: Follow Up Reply with quote

After some experimenting I have discovered that it might be better to use Character Styles to format text within a paragraph, where it differs from the overall Paragraph Style.

Code:
Cursor.CharStyleName = "YOUR STYLE HERE"


I found that setting bold, italic and underlining directly is undesirable because it overrides the indirect Paragraph Styles formatting. Additionally it is easier to access more advanced properties like superscripts, etc.

Morgen
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3618
Location: Columbus, Ohio, USA

PostPosted: Mon Feb 14, 2011 9:28 pm    Post subject: Reply with quote

Excellent post...
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group