Error when saving a pdf in macro

Hi,

I have been using a macro to export my work in Calc to pdf:

Sub ExportToPDF()
dim document as object
dim dispatcher as object

document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
path ="..."
Open path For Append As #1
Close #1

dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = path

dispatcher.executeDispatch(document, ".uno:ExportDirectToPDF", "", 0, args1())
End Sub

But lately the very last line creates an error:

Error saving the document Purchase Invoice:
General Error.
General input/output error.

The empty file is created, but very last line fails. I have reinstalled LibreOffice, that did not help. I tested it on another computer and fails the same way. The macro is attached to the file. Both computers are Win10. It is especially mysterious because it used to work before and the file is created hence I dont think it can be related to permissions.

Any ideas how I could make it work?

Why do you create the file using OpenClose calls? Just curious.

path must be in format URL

path = ConvertToURL("/home/USER/output.pdf")

better that dispatch, look:

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

Thank you, that worked.