Change scaling mode with libreoffice macro

Hello,
i wrote a macro and it runs in Windowds successfully. However, there is no action about changing scaling mode in Linux (Centos 7). i guess there is a problem about argumans of page setup. The macro code is like below;

My expectation from macro: scaling the each sheet of excel/calc as one page (width and height) and export as PDF

Sub SaveSheetAsPDF(optional inputArg as string, optional outputArg as string)

dim document   as object
dim dispatcher as object
dim component as object
rem ----------------------------------------------------------------------
rem get access to the document

dim inputFile as string
dim outputPath as string

inputFile = "file:///"+inputArg
outputFile = "file:///"+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 = False

component = StarDesktop.loadComponentFromURL(inputFile, "_default",0,args2)
document   = component.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

dim args(4) as new com.sun.star.beans.PropertyValue
args(0).Name = "fitToHeight"
args(0).Value = 1
args(1).Name = "fitToWidth"
args(1).Value = 1
args(2).Name = "firstPageNumber"
args(2).Value = 0
args(3).Name = "scale"
args(3).Value = 100
args(4).Name = "orientation"
args(4).Value = "portrait"

dispatcher.executeDispatch(document, ".uno:PageSetup", "", 0, args())

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

Please always use “preformatted text” button when writing code (select the code you pasted, and click the button on the toolbar), or else it’s unreadable.

I corrected the code, thank you.

It’s more easy, if you first create page style then apply in each sheet.

Sub Main()
Dim args1(0) As New com.sun.star.beans.PropertyValue
Dim args2(0) As New com.sun.star.beans.PropertyValue

	args1(0).Name = "MacroExecutionMode"
	args1(0).Value = 4	
	source = ConvertToURL("/home/mau/toPDF.ods")
	doc = StarDesktop.loadComponentFromURL(source, "_default", 0, args1)
	
	For Each sheet In doc.Sheets
		sheet.PageStyle = "myStyle"
	Next
	
	args2(0).Name = "FilterName"
	args2(0).Value = "calc_pdf_Export"
	target = ConvertToURL("/home/mau/test.pdf")
	doc.storeToURL(target, args2)
End Sub

Dear, how can define “myStyle” in this macro?

You can used

doc = ThisComponent
name_style = "myStyle"
 
page_style = doc.getStyleFamilies().getByName("PageStyles")
If Not page_style.hasByName(name_style) Then
	my_style = doc.createInstance("com.sun.star.style.PageStyle") 
    page_style.insertByName(name_style, my_style)
    With my_style
        .Width = 27940            
        .Height = 21590           
        .TopMargin = 2000         
        .BottomMargin = 2000      
        .LeftMargin = 1000        
        .RightMargin = 1000       
        .CenterHorizontally = True    
        .ScaleToPagesX = 1        
        .ScaleToPagesY = 1
    End With
End If

now, i changed something in my macro and it runs succesfully. If there is more optimal solution, please recommend it

sub ExportToPDF (Optional outputFile as String)

dim document as object
dim dispatcher as object
dim NumStyle as integer
dim target as string

target = ConvertToURL(outputFile)

oStyleFamilies = ThisComponent.StyleFamilies
oPageStyles = oStyleFamilies.getByName("PageStyles")
numStyle = oPageStyles.Count 

For iCount = 0 To numStyle-1
oDefaultStyle = oPageStyles(iCount)
oDefaultStyle.ScaleToPagesX = 1
oDefaultStyle.ScaleToPagesY = 1
Next iCount

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

dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = target
args1(1).Name = "FilterName"
args1(1).Value = "calc_pdf_Export"
dispatcher.executeDispatch(document, ".uno:ExportDirectToPDF", "", 0, args1())
ThisComponent.close(true)

end sub