OpenOffice.org Macro

Over the next couple of posts, I'll be posting the portions of a script I used to Generate a listing of personnel from a database table in OpenOffice.org Writer using the Basic language interface.

What tasks are performed:
1. Styles are made and modified
2. Data is read and inserted into pre-formatted tables
3. The tables are modified (cells are merged and split)
4. New pages are entered
5. Names are Indexed and Headings added to the table of contents
6. Table of contents and an index are generated
7. The Table of Contents is modified to include hyperlinks around the name of the table of contents entry

In my work on this script, I got a lot of help from various public support sites reading previous posts and finding the answers I needed. As well, once I figured out how to use the documentation, I used that a lot to help figure out the parts that I couldn't find sample code for.

Part 1: Changing styles.

I made a procedure to change all the styles that I wanted in the document. I created styles and edited existing styles. I should be noted that when you want to do hyperlinks in the Table of Contents, I had to use the "Heading 1" etc... styles. Custom styles didn't work!

The procedure requires a Document object which can be made by the following code, which I will post later again as I complete the entire script over a series of posts.

This code creates a new empty document
===

Dim oDocument As Object
' Create document
Url = "private:factory/swriter"
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy())

===
This code modifies a number of styles built into documents, including those used by the table of contents and index. I've added my own custom paragraph style called "styleNormal".
===

sub SetStyles(oDocument As Object)
REM Create Styles
oStyleFamilies = oDocument.StyleFamilies
oParagraphStyles = oStyleFamilies.getByName("ParagraphStyles")
'
Header1 = oParagraphStyles.getByName("Heading 1")
Header1.CharFontName = "Arial"
Header1.CharHeight = 18
Header1.ParaAdjust = com.sun.star.style.ParagraphAdjust.LEFT
Header1.ParaFirstLineIndent = 0
Header1.ParaTopMargin = 0
Header1.ParaBottomMargin = 0
Header1.CharWeight = com.sun.star.awt.FontWeight.BOLD
Header1.CharUnderline = com.sun.star.awt.FontUnderline.NONE
Header1.setPropertyValue("CharPosture",com.sun.star.awt.FontSlant.NONE)
Header1.PageDescName = "Default"
Header1.ParaBackColor = 13421772 REM Produces a light gray colour - I got this number from
REM the Hexedecimal value of CCCCCC (i think it's RGB - two digits for each)
'
Header2 = oParagraphStyles.getByName("Heading 2")
Header2.ParaTopMargin = 0
Header2.ParaBottomMargin = 0
Header2.CharFontName = "Arial"
Header2.CharHeight = 15
Header2.ParaAdjust = com.sun.star.style.ParagraphAdjust.LEFT
Header2.ParaFirstLineIndent = 0
Header2.CharWeight = com.sun.star.awt.FontWeight.BOLD
Header2.CharUnderline = com.sun.star.awt.FontUnderline.NONE
Header2.setPropertyValue("CharPosture",com.sun.star.awt.FontSlant.NONE)
Header2.ParaBackColor = 13421772
REM
Header3 = oParagraphStyles.getByName("Heading 3")
Header3.ParaTopMargin = 0
Header3.ParaBottomMargin = 0
Header3.setPropertyValue("CharPosture",com.sun.star.awt.FontSlant.NONE)
Header3.CharFontName = "Arial"
Header3.CharHeight = 12
Header3.ParaAdjust = com.sun.star.style.ParagraphAdjust.LEFT
Header3.ParaFirstLineIndent = 0
Header3.CharWeight = com.sun.star.awt.FontWeight.BOLD
Header3.CharUnderline = com.sun.star.awt.FontUnderline.NONE
Header3.ParaBackColor = 13421772
REM
Header4 = oParagraphStyles.getByName("Heading 4")
Header4.ParaTopMargin = 0
Header4.ParaBottomMargin = 0
Header4.setPropertyValue("CharPosture",com.sun.star.awt.FontSlant.NONE)
Header4.CharFontName = "Arial"
Header4.CharHeight = 12
Header4.ParaAdjust = com.sun.star.style.ParagraphAdjust.LEFT
Header4.ParaFirstLineIndent = 0
Header4.CharWeight = com.sun.star.awt.FontWeight.BOLD
Header4.CharUnderline = com.sun.star.awt.FontUnderline.NONE
Header4.ParaBackColor = 13421772
REM
Header5 = oParagraphStyles.getByName("Heading 5")
Header5.ParaTopMargin = 0
Header5.ParaBottomMargin = 0
Header5.setPropertyValue("CharPosture",com.sun.star.awt.FontSlant.NONE)
Header5.CharFontName = "Arial"
Header5.CharHeight = 12
Header5.ParaAdjust = com.sun.star.style.ParagraphAdjust.LEFT
Header5.ParaFirstLineIndent = 0
Header5.CharWeight = com.sun.star.awt.FontWeight.NORMAL
Header5.CharUnderline = com.sun.star.awt.FontUnderline.SINGLE
Header5.ParaBackColor = 13421772
REM
Header6 = oParagraphStyles.getByName("Heading 6")
Header6.ParaTopMargin = 0
Header6.ParaBottomMargin = 0
Header6.setPropertyValue("CharPosture",com.sun.star.awt.FontSlant.NONE)
Header6.CharFontName = "Arial"
Header6.CharHeight = 9
Header6.ParaAdjust = com.sun.star.style.ParagraphAdjust.LEFT
Header6.ParaFirstLineIndent = 0
Header6.CharWeight = com.sun.star.awt.FontWeight.NORMAL
Header6.CharUnderline = com.sun.star.awt.FontUnderline.NONE
Header6.ParaBackColor = 13421772
REM
styleNormal = oDocument.createInstance("com.sun.star.style.ParagraphStyle")
oParagraphStyles.insertByName("styleNormal", styleNormal)
styleNormal.Name = "styleNormal"
styleNormal.CharFontName = "Arial"
styleNormal.CharHeight = 8
styleNormal.CharWeight = com.sun.star.awt.FontWeight.NORMAL
styleNormal.CharUnderline = com.sun.star.awt.FontUnderline.NONE
REM ----------------- Table of Contents Fonts ---------------------------------
contentsheading = oParagraphStyles.getByName("Contents Heading")
contentsheading.CharFontName = "Arial"
contentsheading.CharHeight = 16
contentsheading.CharWeight = com.sun.star.awt.FontWeight.BOLD
REM
contents1 = oParagraphStyles.getByName("Contents 1")
contents1.CharFontName = "Arial"
contents1.CharHeight = 10
REM
contents2 = oParagraphStyles.getByName("Contents 2")
contents2.CharFontName = "Arial"
contents2.CharHeight = 10
REM
contents3 = oParagraphStyles.getByName("Contents 3")
contents3.CharFontName = "Arial"
contents3.CharHeight = 10
REM
contents4 = oParagraphStyles.getByName("Contents 4")
contents4.CharFontName = "Arial"
contents4.CharHeight = 10
REM
contents5 = oParagraphStyles.getByName("Contents 5")
contents5.CharFontName = "Arial"
contents5.CharHeight = 10
REM
contents6 = oParagraphStyles.getByName("Contents 6")
contents6.CharFontName = "Arial"
contents6.CharHeight = 10
REM -------------------------- Index Formatting ------
index1 = oParagraphStyles.getByName("Index 1")
index1.CharFontName = "Arial"
index1.CharHeight = 8
REM
index2 = oParagraphStyles.getByName("Index 2")
index2.CharFontName = "Arial"
index2.CharHeight = 8
REM
index3 = oParagraphStyles.getByName("Index 3")
index3.CharFontName = "Arial"
index3.CharHeight = 8
REM
indexSeparator = oParagraphStyles.getByName("Index Separator")
indexSeparator.CharFontName = "Arial"
indexSeparator.CharHeight = 10
end sub

===

Popular posts from this blog