[Solved] Need help writing macro to do redlining in Writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
tiendatanduin
Posts: 9
Joined: Fri Dec 15, 2017 1:47 pm

[Solved] Need help writing macro to do redlining in Writer

Post by tiendatanduin »

Hi all,

I'm investigating the ability to write a macro to automatically perform redlining functionality with LibreOffice. Basically, the macro that I'm trying to write will perform the following task when LibreOffice is open:

1. Open the file: /tmp/source.doc
2. Select the redlining function by going to: Edit > Track Changes > Compare Document ...
3. Select file /tmp/revision.doc as the revision to compare
4. Save the redlined doc to /tmp/redlined.doc

I have been digging the internet for the last two days, and have not found anything that is helpful for my task :crazy: . Now I'm wondering if it's even possible to achieve what I want with LibreOffice macro. Any help is appreciated!

Thanks,
Dat

PS. I think everybody knows, but just in case, the "Record macro" function doesn't work for my task. I've tried it.
Last edited by robleyd on Mon Dec 18, 2017 10:59 am, edited 4 times in total.
Reason: Tagged [Solved] [robleyd, Moderator]
LibreOffice 5.2.3.3 and Mac OS X El Capitan
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Need help with writing macro to perform redlining in Wri

Post by Zizi64 »

Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Need help with writing macro to perform redlining in Wri

Post by Villeroy »

For *.doc files you need to do this in Microsoft Word anyway.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
RoryOF
Moderator
Posts: 34612
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Need help with writing macro to perform redlining in Wri

Post by RoryOF »

Could you not simply extract the two texts and use a utility such as windiff.exe or fc.exe to compare the files, piping the output to a third file?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
tiendatanduin
Posts: 9
Joined: Fri Dec 15, 2017 1:47 pm

Re: Need help with writing macro to perform redlining in Wri

Post by tiendatanduin »

Thanks @Zizi64 for the suggestions. I'm studying them now :D

@Villeroy: LibreOffice can do the doc comparison. The results might be different from MS Word comparison, but it's acceptable for our use cases.

@RoryOF: comparing text only is not good enough for us, because we need to compare formatting as well.

I'll update this thread when I have some results ...
LibreOffice 5.2.3.3 and Mac OS X El Capitan
tiendatanduin
Posts: 9
Joined: Fri Dec 15, 2017 1:47 pm

Re: Need help with writing macro to perform redlining in Wri

Post by tiendatanduin »

Thanks a lot for all your helps! After reading all of the above examples, I was able to create a macro that does exactly what I need (and a bonus to save to pdf). You guys are awesome!

Here is the macro, in case someone is looking for a similar solution:

Code: Select all

sub DocRedlining

dim sUrl as string
dim oDoc as object, oDocFrame as object, dispatcher as object

rem Note: in LibreOffice, you must open the revision first, then the source second, to get the correct redlining
rem Open /tmp/revision.docx
sUrl = convertToUrl("/tmp/revision.docx")
oDoc = stardesktop.LoadComponentFromURL(sUrl, "_blank", 0, Array())

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

rem Compare it with /tmp/source.docx
dim PropVal(0) as new com.sun.star.beans.PropertyValue
PropVal(0).Name = "URL"
PropVal(0).Value = convertToUrl("/tmp/source.docx")
dispatcher.executeDispatch(oDocFrame, ".uno:CompareDocuments", "", 0, PropVal())

rem Show the track changes
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ShowTrackedChanges"
args1(0).Value = true

dispatcher.executeDispatch(oDocFrame, ".uno:ShowTrackedChanges", "", 0, args1())

rem Dismiss the track changes box
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "AcceptTrackedChanges"
args2(0).Value = false
dispatcher.executeDispatch(oDocFrame, ".uno:AcceptTrackedChanges", "", 0, args2())

rem Save the file to /tmp/redlined.pdf
dim args3(2) as new com.sun.star.beans.PropertyValue
args3(0).Name = "URL"
args3(0).Value = "file:///tmp/redlined.pdf"
args3(1).Name = "FilterName"
args3(1).Value = "writer_pdf_Export"
args3(2).Name = "FilterData"
args3(2).Value = Array(Array("UseLosslessCompression",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Quality",0,90,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ReduceImageResolution",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("MaxImageResolution",0,300,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("UseTaggedPDF",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SelectPdfVersion",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportNotes",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ViewPDFAfterExport",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportBookmarks",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("OpenBookmarkLevels",0,-1,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("UseTransitionEffects",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("IsSkipEmptyPages",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportPlaceholders",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("IsAddStream",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("FormsType",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportFormFields",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("AllowDuplicateFieldNames",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerToolbar",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerMenubar",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("HideViewerWindowControls",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ResizeWindowToInitialPage",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("CenterWindow",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("OpenInFullScreenMode",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("DisplayPDFDocumentTitle",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("InitialView",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Magnification",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Zoom",0,100,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PageLayout",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("FirstPageOnLeft",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("InitialPage",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Printing",0,2,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Changes",0,4,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EnableCopyingOfContent",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EnableTextAccessForAccessibilityTools",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportLinksRelativeFsys",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PDFViewSelection",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ConvertOOoTargetToPDFTarget",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("ExportBookmarksToPDFDestination",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SignPDF",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("_OkButtonString",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Watermark",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("EncryptFile",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PreparedPasswords",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("RestrictPermissions",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PreparedPermissionPassword",0,Array(),com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SignatureLocation",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SignatureReason",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SignatureContactInfo",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SignaturePassword",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SignatureCertificate",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("SignatureTSA",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE))
dispatcher.executeDispatch(oDocFrame, ".uno:ExportToPDF", "", 0, args3())

rem Save the file to /tmp/redlined.docx
dim args4(1) as new com.sun.star.beans.PropertyValue
args4(0).Name = "URL"
args4(0).Value = "file:///tmp/redlined.docx"
args4(1).Name = "FilterName"
args4(1).Value = "MS Word 2007 XML"

dispatcher.executeDispatch(oDocFrame, ".uno:SaveAs", "", 0, args4())

rem Close the window
oDocFrame.Close(False)

end sub
Last edited by tiendatanduin on Mon Dec 18, 2017 12:51 pm, edited 2 times in total.
LibreOffice 5.2.3.3 and Mac OS X El Capitan
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Need help writing macro to do redlining in Writ

Post by Villeroy »

And when you open the document with MS Word you can see the tracked changes?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
tiendatanduin
Posts: 9
Joined: Fri Dec 15, 2017 1:47 pm

Re: [Solved] Need help writing macro to do redlining in Writ

Post by tiendatanduin »

Yes, you can see the tracked changes in the docx file correctly. It looks a bit different than if you use the MS Word to generate the redlining, but it's comparable and good enough for our needs.
LibreOffice 5.2.3.3 and Mac OS X El Capitan
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Need help writing macro to do redlining in Writ

Post by Villeroy »

Oh, that's new to me. When I tried some years ago, tracking changes did not work between the 2 applications. Thanks for the info
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
tiendatanduin
Posts: 9
Joined: Fri Dec 15, 2017 1:47 pm

Re: [Solved] Need help writing macro to do redlining in Writ

Post by tiendatanduin »

Glad to help :D
LibreOffice 5.2.3.3 and Mac OS X El Capitan
Post Reply