With the documents "XMultiServiceFactory" interface create a new
"TextField.Annotation" object. Set the author and as well as the content
of the note field by accessing and changing the objects properties. Next
create a "Date" structure and enter the date of creation. Apply the
"Date" structure as "date" property to the note object too. Finally add
the note object to the document, by using the "insertTextContent"
function.
For further details see http://wi.wu-wien.ac.at/rgf/diplomarbeiten/BakkStuff/2008/200809_Frysak/200809_Frysak_Automating_OOo_ooRexx_Nutshells.pdf.
-- try to get a script context, will be .nil, if script was not invoked by OOo
x_ScriptContext = uno.getScriptContext()
if (x_ScriptContext <> .nil) then
do
-- invoked by OOo as a macro
-- get context
x_ComponentContext = x_ScriptContext~getComponentContext
-- get desktop (an XDesktop)
x_Desktop = x_ScriptContext~getDesktop
-- get current document
x_Document = x_ScriptContext~getDocument
end
else
do
-- called from outside of OOo, create a connection
-- connect to Open Office and get component context
x_ComponentContext = UNO.connect()
-- create a desktop service and its interface
service = "{%see com.sun.star.frame.Desktop}"
s_Desktop = x_ComponentContext~getServiceManager~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}~createInstance(service)
x_Desktop = s_Desktop~{%see com.sun.star.frame.XDesktop%XDesktop}
-- get the last active document
x_Document = x_Desktop~getCurrentComponent()
end
-- get the text of the current document
x_TextDocument = x_Document~{%see com.sun.star.text.XTextDocument%XTextDocument}
x_Text = x_TextDocument~getText
-- get the current cursor position
s_CurrentController = x_TextDocument~getCurrentController()
x_TextViewCursorSupplier = s_CurrentController~{%see com.sun.star.text.XTextViewCursorSupplier%XTextViewCursorSupplier}
x_CurrentCursor = x_TextViewCursorSupplier~getViewCursor()
-- get current text cursor
x_TextCursor = x_Text~createTextCursorByRange(x_CurrentCursor~getStart())
-- create a factory to create a note object
x_MultiServiceFactory = x_Document~{%see com.sun.star.lang.XMultiServiceFactory%XMultiServiceFactory}
s_Annotation = x_MultiServiceFactory~createInstance("{%see com.sun.star.text.TextField.Annotation}")
-- get access to the note object properties and fill them
x_AnnotationProperties = s_Annotation~{%see com.sun.star.beans.XPropertySet%XPropertySet}
x_AnnotationProperties~setPropertyValue("Author", "JF")
x_AnnotationProperties~setPropertyValue("Content", "Thats right, Mr. Pitonyak")
-- get current date from rexx and parse, year month and day
currentdate = DATE("S",,,"/")
PARSE VAR currentdate year "/" month "/" day
-- create date object and set its date properties, then add the date object
-- to the note object
date = .bsf~new("{%see com.sun.star.util.Date}")
date~bsf.setFieldValue("Day", day)
date~bsf.setFieldValue("Month", month)
date~bsf.setFieldValue("Year", year)
x_AnnotationProperties~setPropertyValue("Date", date)
-- finally insert the note object into the text
x_TextContent = s_Annotation~{%see com.sun.star.text.XTextContent%XTextContent}
x_Text~insertTextContent(x_TextCursor, x_TextContent, .true)
::requires UNO.CLS