From The Document Foundation Wiki
< Macros‎ | Writer
Jump to: navigation, search

Template:TopMenu/en Template:Menu/en Template:Menu.Macros/en

Other languages:
English • ‎français • ‎italiano


This tutorial covers how to write a macro for Writer that inserts a custom comment at the cursor position (in order to for example preset the Author and/or Date fields).

We use[1] and services[2] as well as the structure[3] to obtain a time-stamp from the time of execution.


The following macro, which could for instance be linked with a keyboard shortcut, inserts a custom comment at the current cursor position:

Option Explicit

Sub InsertComment
    '''Insert a comment at position of cursor'''

    Dim oCurs As Object '
    Dim oAnnot As Object '
    Dim oDateTime As New

    oCurs = ThisComponent.CurrentController.getViewCursor()
    oAnnot = ThisComponent.createInstance( _

    With oDateTime '
        .Minutes = 20
        .Hours = 12
        .Day = 20
        .Month = 12
        .Year = 2012
    End With
    With oAnnot '
        .Author = "Author of the comment"
        .Content = "Content of the comment"
        .DateTimeValue = oDateTime
    End With

    oAnnot.attach(oCurs.Start) ' Insert at cursor start

End Sub ' InsertComment

Using Python:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import uno

def InsertComment():
    """Insert a comment at position of cursor
    dt = uno.createUnoStruct("")

    thisComponent = XSCRIPTCONTEXT.getDocument()
    curs = thisComponent.CurrentController.getViewCursor()
    annot = thisComponent.createInstance(

    dt.Minutes = 20
    dt.Hours = 12
    dt.Day = 20
    dt.Month = 12
    dt.Year = 2012

    annot.Author = "Author of the Comment"
    annot.Content = 'Content of the comment'
    annot.DateTimeValue = dt

    annot.attach(curs.Start)  # Insert at cursor start

Time-stamp calculation is not performed in these examples.