UI showing even with hidden option

Hi all,
following macro runs correctly except that UI with excel file gets opened. Hidden option is not working.
Also tried to add both headless and hidden options on command line, no luck.
Any hint?

Sub ExportToPDF(inputArg as string, outputArg as string)

   	dim document as object
   	dim dispatcher as object
   	inputFile = ConvertToURL(inputArg)
   	outputFile = ConvertToURL(outputArg)

	dim args2(1) as new com.sun.star.beans.PropertyValue
	args2(0).Name = "MacroExecutionMode"
	args2(0).Value = 4
	args2(1).Name = "Hidden"
	args2(1).Value = True

	component = StarDesktop.loadComponentFromURL(inputFile, "_default",0,args2)

	document = component.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    sheets = component.getSheets()
    sheet = sheets.getByName("AvionicsDataSource")
    sheet.isVisible = False
    for each sheet in sheets.ElementNames
       sheet = sheets.getByName(sheet)
       sheet.AutomaticPrintArea = True
    styleFamilies = component.StyleFamilies
	pageStyles = styleFamilies.getByName("PageStyles")
	numStyles = pageStyles.Count 

	For count = 0 To numStyles - 1
		defaultStyle = pageStyles(count)
		defaultStyle.ScaleToPagesX = 1
		rem defaultStyle.ScaleToPagesY = 1
	Next count
    dim args1(1) as new com.sun.star.beans.PropertyValue
    args1(0).Name = "URL"
    args1(0).Value = outputFile
    args1(1).Name = "FilterName"
    args1(1).Value = "calc_pdf_Export"
    dispatcher.executeDispatch(document, ".uno:ExportDirectToPDF", "", 0, args1())
End Sub

This is the command line:

-headless "macro:///Standard.Module1.ExportToPDF(C:\testFolder\LibreOffice\Report.xlsx,C:\testFolder\LibreOffice\report.pdf)

Try changing the code so that you don’t use the Dispatcher. For example like this:

Sub ExportToPDF(inputArg As String, outputArg As String)
Dim args(1) As New com.sun.star.beans.PropertyValue
Dim inputFile As String, outputFile As String
Dim component As Variant 
Dim sheets As Variant 
Dim pageStyles As Variant 
Dim count As Long
	inputFile = ConvertToURL(inputArg)
	outputFile = ConvertToURL(outputArg)

	args(0).Name = "MacroExecutionMode" : args(0).Value = 4
	args(1).Name = "Hidden"				: args(1).Value = True

	component = StarDesktop.loadComponentFromURL(inputFile, "_default",0,args)
	sheets = component.getSheets()
	If sheets.hasByName("AvionicsDataSource") Then _
		sheets.getByName("AvionicsDataSource").isVisible = False
	For count = 0 To sheets.getCount() - 1
		sheets.getByIndex(count).AutomaticPrintArea = True
	Next count

	pageStyles = component.StyleFamilies.getByName("PageStyles")

	For count = 0 To pageStyles.getCount() - 1
		pageStyles.getByIndex(count).ScaleToPagesX = 1
	Next count

	args(0).Name = "URL"		: args(0).Value = outputFile
	args(1).Name = "FilterName"	: args(1).Value = "calc_pdf_Export"

	component.storeToURL(outputFile, args)
End Sub

I̶n̶ t̶h̶i̶s̶ c̶a̶s̶e̶, t̶h̶e̶ o̶f̶f̶i̶c̶e̶ w̶i̶l̶l̶ n̶o̶t̶ n̶e̶e̶d̶ v̶i̶s̶u̶a̶l̶ p̶a̶r̶t̶s̶ a̶n̶d̶ w̶i̶l̶l̶ t̶r̶y̶ t̶o̶ r̶e̶m̶a̶i̶n̶ i̶n̶v̶i̶s̶i̶b̶l̶e̶. (Wrong thesis, see comments - key –invisible instead –headless)

Hi, thanks, this script works. If called with -headless option there was still interface flashing for a moment. With -hidden option it is perfectly silent.

Note that “headless” was not a correct choice for your command line from the start.

Headless is for either atomic operations started from the command line (like conversion, printing, etc.), where LibreOffice starts, opens a document, performs the task, and exits. This way, it has a limited lifetime, and is OK to be hidden. Or LibreOffice may be launched “headless” in listening mode; then it allows an external application to connect (e.g., using pipes), and control its execution. That way, it’s also controllable, and may be hidden.

In the first case, it only takes filenames as arguments for respective atomic commands like --convert-to. In the second case, it uses --accept parameter. In no case does it expect something (an URL) to open outside of the atomic operation. So calling soffice --headless without any other arguments, or with URLs outside of atomic operations, would always result in UI.

A macro URL is just another thing that LO can open; it’s like telling “LibreOffice, open a Macro document”, analogous to “LibreOffice, open this Text document”. It has no knowledge that it would ultimately close (how could it know that until it finished execution of the macro); so it works just as it would for any other document opened normally. Just with the problem that headless breaks all kinds of interactions, like dialogs…

@mikekaganski Are you saying that we need to use a different key to launch the application?


@JohnSUN: yes :slight_smile: