Hello World

This is a tutorial that will create the text “Hello World” within the current document of a OpenOffice Writer document.

To be able to get into the editor, if you click Tools->Macros->Organize Macros->OpenOffice Basic and then select Edit on the module1 within MyMacros->Standard.

The code required builds up an object to document and also a network object server that communicates with the document frame (createUnoService) the objects within the parameters e.g com.sun.star.frame.DispatchHelper are services that enable communication with dispatch object which in-turns ‘talks’ to the document object.

The code

sub helloworld
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
rem ----------------------------------------------------------------------
rem Create a universe network object service with the frame.
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
 
rem ----------------------------------------------------------------------
rem The type is a text string and the value of the string is Hello World
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Text"
args1(0).Value = "Hello World"
 
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())
 
rem ----------------------------------------------------------------------
rem Create a new paragraph
dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())
end sub

if you open up a new document and then goto the macro editor (click Tools->Macros->Organize Macros->OpenOffice Basic and then select Edit on the module1 within MyMacros->Standard) insert the code into the editor. To run the code, click on the Tools->Macro->Run Macro, then select the Helloworld subroutine from the MyMacros->Standard->Module1.

That is it, and the start of tutorials for OpenOffice being 🙂