Print an external file via macro

Please…How to print, via macro , a external write file (.odt)?

I’ve tried this macro code:

Sub GetCurrentFolder
GlobalScope.BasicLibraries.loadLibrary(“Tools”)
Dim sCurrentPath, sFile as string
Dim oProps(0) as new com.sun.star.beans.PropertyValue

sCurrentPath = DirectoryNameoutofPath(ThisComponent.getURL(),"/") & "/" 
sFile = sCurrentPath & "SOMETHING_FILE.odt"
if FileExists(sFile) then
   oProps(0).Name = "FileName"
   oProps(0).Value = sFile
   ThisComponent.print(oProps())
end if   

End Sub

But nothing happens.

from commandline:

[path/to/]soffice[.exe] -p path/to/your/somefile.odt

Thanks, but it didn´t work

See Direct print configured cell range

I think something along the lines of

dim sFileName As String
dim aOptions(0) as new com.sun.star.beans.PropertyValue
dim aDescr(0) as new com.sun.star.beans.PropertyValue

aOptions(0).Name = "FileName"
aOptions(0).Value = convertToURL(sFileName)
ThisComponent.setPrinter(aDescr)
ThisComponent.print(aOptions)

Thanks, but Nothing happens too.

You need open file first, then send to print.

https://wiki.documentfoundation.org/Macros/Basic/Documents#Open_documents

Sub Main()

GlobalScope.BasicLibraries.loadLibrary("Tools")
Dim sCurrentPath, sFile as string
Dim oProps1(0) as new com.sun.star.beans.PropertyValue
Dim oProps2(0) as new com.sun.star.beans.PropertyValue

	sCurrentPath = DirectoryNameoutofPath(ThisComponent.getURL(),"/") & "/" 
	sFile = sCurrentPath & "SOMETHING_FILE.odt"	
	
    oProps1(0).Name = "Hidden"
    oProps1(0).Value = True

    doc = StarDesktop.loadComponentFromURL(sFile, "default", 0, oProps1())
    doc.print(oProps2())
    
    doc.close(True)
	
End Sub

Yes, The doc opens, but it doesn’t print.

No, in your code you are not opening the file, if the file you want to print is:

sFile = sCurrentPath & "SOMETHING_FILE.odt"

Sorry, I didn’t see the new code…but it worked…ONLY TWO THINGS
.
dim doc as object
.
and
.
doc.print(oProps2())
wait 50
doc.close(True)
.
if the doc closes immediately, it won’t be printed.
.
but it worked very well…thanks so much.

See here.
Setting the wait property of the print method is required.

Great…I added these code lines…the macro became cleaner…Thanks to masters.
.
.
.
The final macro got like this:
.
.
Sub PrintDELIVERYRECEIPT()
’ PRINT AN EXTERNAL FILE
GlobalScope.BasicLibraries.loadLibrary(“Tools”)
dim oDoc as object
Dim sCurrentPath, sFile as string
Dim oProps1(0) as new com.sun.star.beans.PropertyValue
Dim oProps2(0) as new com.sun.star.beans.PropertyValue

sCurrentPath = DirectoryNameoutofPath(ThisComponent.getURL(),"/") & "/" 
sFile = sCurrentPath & "DELIVERY-RECEIPT.odt"	
if FileExists(sFile) then 
   oProps1(0).Name = "Hidden"
   oProps1(0).Value = True

   oProps2(0).Name="Wait"
   oProps2(0).Value=True

   oDoc = StarDesktop.loadComponentFromURL(sFile, "default", 0, oProps1())
   oDoc.print(oProps2())
   oDoc.close(True)
end if   

End Sub