We will be migrating from Ask to Discourse on the first week of August, read the details here

Ask Your Question

Macro for inserting current date as DD/MM/YYYY and after that add a text in LO Writer

asked 2017-03-14 09:55:21 +0200

adrian.georgescu gravatar image

updated 2017-03-14 09:55:52 +0200

I can't find the info for writing a string with a custom format. I've tried recording a macro and using a date field but didn't work. my macro inserts a date but the format is DD.MM.YYYY not DD/MM/YYYY, please help me:

sub addDateText
    dim document   as object
    dim dispatcher as object

    document   = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

    dim sText as string
    sText = Format(Now(),"dd/mm/yyyy")

    dim textFormatat(0) as new com.sun.star.beans.PropertyValue
    textFormatat(0).Name = "Text"
    textFormatat(0).Value = sText+CHR$(9)+"[SomeText]"    
    dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, textFormatat())
end sub
edit retag flag offensive close merge delete


Ceterum censeo...
Dates are very often expected to retain their original meaning anywhere in the world. Due to the funny stubbornness very few countries (but among them a big one) in the world are suffering from, unambiguousness cannot be achieved using the slash-formats without adding additional information.
The only internationally approved readable date format is YYYY-MM-DD (ISO 8601). This format is rich in advantages. Why not use it?

Lupp gravatar imageLupp ( 2017-03-14 12:13:28 +0200 )edit

(and naturally sorting, I must add)

Mike Kaganski gravatar imageMike Kaganski ( 2017-03-14 12:20:18 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2017-03-14 11:39:38 +0200

librebel gravatar image

Hello adrian, you could try:

sText = Format( Now, "DD\/MM\/YYYY")

Also see link

edit flag offensive delete link more


Thanks, I was accustomed to MsOffice VBA and i didn't realize that / could be interpreted as special character.

adrian.georgescu gravatar imageadrian.georgescu ( 2017-03-14 12:01:17 +0200 )edit

I cannot confirm this. ...
Testing with LibO V5.3.1.1RC and also with V5.2.3.3 the code posted in the OQ (whether with format code in lower case or in upper case) worked flawlessly.
I would be happy to hear about the factual cause of the reported issue.

Lupp gravatar imageLupp ( 2017-03-14 13:58:29 +0200 )edit

@Lupp. LibOffice: (x64). Ok after reading your comment i've tried my original code but changed the document locale to en-US and worked. it produced the result i wanted ( 07/04/2017 [Some text] ) but if I change the locale of the document to other language than English (UK,US,Australia,Zimbabwe,...) it won't work (as desired) even if i change the Date accepted patterns in Options->Language Settings->Languages so i think it's a bug when you set lng. of the document other than EN

adrian.georgescu gravatar imageadrian.georgescu ( 2017-04-07 11:30:00 +0200 )edit

answered 2020-01-21 04:45:12 +0200

WhiteBear gravatar image

Hi, thanks for posting this solution. It works for me but with some slight problems. Firstly I dont need the words "SomeText" inserted. I tried deleting The part that says "[SomeText]", which gets rid of that line, but then there's an odd small box thingy that appears after the date. Also I'd like to insert two lines under the date if possible. Any help there?

edit flag offensive delete link more



Please do not post as an answer unless it actually is an answer to the question. Instead use add a comment or post as a new question.

Response to "Not-an-answer-question":


textFormatat(0).Value = sText+CHR$(9)+"[SomeText]"

with this:

textFormatat(0).Value = sText+CHR$(10)+CHR$(10)+CHR$(10)
Ratslinger gravatar imageRatslinger ( 2020-01-21 05:02:30 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-03-14 09:55:21 +0200

Seen: 4,779 times

Last updated: Jan 21 '20