How do I insert a Modification Date Field in a Writer Document using a Macro?

In LO Writer, I can insert a Modification Date field by clicking Insert>Field>More Fields… or hitting Ctrl+F2, going to the ‘DocInformation’ tab, Selecting ‘Modified’ under ‘Type’ and ‘Date’ under ‘Select’, and hitting “Insert”. If I want to do this using a macro, I know that generally, the approach for inserting fields is to create the field using Dim oField As Object : oField = oDoc.createInstance("com.sun.star.text..."), but for a Modification Date field, I’m not sure what the rest of that is. I’ve seen mentions of ...DocInfo.ModificationDate and ...DocInfo.ChangeDate, but both of those cause a “ServiceNotRegisteredException”. How do I do this?

I know it’s really dated, but I’m kind of married to my current version:
Version: 7.5.7.1 (X86_64) / LibreOffice Community
Build ID: 47eb0cf7efbacdee9b19ae25d6752381ede23126
CPU threads: 4; OS: Windows 10.0 Build 19045; UI render: Skia/Raster; VCL: win
Locale: en-US (en_US); UI: en-US
Calc: CL threaded

I think that you can insert the field with a macro but what you cannot is modify the creation and last edition dates.
My logic tells me that it is a call from the program to the operating system:

  • When modifying the last edition date you need to save the changes in the document,
    but then the program overwrites the changes you have made with the current (actual) date of the modification.

  • This may create a conflict or exception.

You need ChangeDateTime

Sub InsertChangeDateTime()
  Dim oDoc as Object, oText as Object, oField as Object
  oDoc = ThisComponent
  oText = oDoc.Text   
  
  oField = oDoc.createInstance("com.sun.star.text.textfield.docinfo.ChangeDateTime")
  oField.IsDate = True    
  
  oText.insertTextContent(oText.getEnd(), oField, False)
End Sub

Here is a Recorded macro:

REM  *****  BASIC  *****

sub InsertModDate
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(5) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Type"
args1(0).Value = 18
args1(1).Name = "SubType"
args1(1).Value = 773
args1(2).Name = "Name"
args1(2).Value = ""
args1(3).Name = "Content"
args1(3).Value = ""
args1(4).Name = "Format"
args1(4).Value = 10049
args1(5).Name = "Separator"
args1(5).Value = " "

dispatcher.executeDispatch(document, ".uno:InsertField", "", 0, args1())

end sub

It worked! However, I want to tweak the date format, so I need to understand how this works…

Or you just record your own macro, including changing the format.