Problem with Macro: File opened but dispatcher throws error

Hi all,

I try to write a macro that accepts all changes and saves a document via the command line in headless mode. Starting opening soffice in headless and running the macro works! I have to add, that I am totally new to macros in libreoffice.

But when I try to call the dispatcher it throws an error. I read, that the dispatcher only works with UI.

What possibilities do I have to call the uno function to accept all changes?

Sub AcceptChangesAndSave(sSourcePath As String)
Dim sPath As String
Dim oDoc As Object

Dim loadComponentProperties(0) As New com.sun.star.beans.PropertyValue
Rem Prevent screen flashing
 	loadComponentProperties(0).Name= "Hidden"
 	loadComponentProperties(0).Value= True

Rem Load document.
	GlobalScope.BasicLibraries.LoadLibrary("Tools")
	sPath = ConvertToURL(sSourcePath)

Rem If a file exists:
	If FileExists(sPath) Then
Rem Open Document:
		oDoc = OpenDocument(sPath,loadComponentProperties)
		dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

REM		dispatcher.executeDispatch(oDoc, ".uno:AcceptAllTrackedChanges", "", 0, Array())

Rem Save:
		oDoc.store()
Rem and close
    	oDoc.close(True)
    EndIf

end sub

The command that throws an error is:
dispatcher.executeDispatch(oDoc, “.uno:AcceptAllTrackedChanges”, “”, 0, Array())

The error is:

Bildschirmfoto von 2022-07-29 09-49-50

Thanks for any tips!

DomCa

Dispatch macros simulate interactions with the GUI. I would think that your dispatch macro simply works when you open the document unhidden.

Solved it.
I have to load the frame of the document first:

		oDocFrame = oDoc.CurrentController.Frame
		dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

		dispatcher.executeDispatch(oDocFrame, ".uno:AcceptAllTrackedChanges", "", 0, Array())

1 Like

EXACTLY: the …executeDispatch(… MUST be called with an instance of oDoc.CurrentController.Frame !