Document object.storetoURL failing--failed: 0x20d(Error Area:Io Class:General Code:13)

This code used to work. (I don’t know what changed.) oDoc is a document object. This is supposed to save the document to a .pdf file with the url stored in the string sPdfUrl.

oFileAccess = createUnoService(“com.sun.star.ucb.SimpleFileAccess”)
args2(0).Name = “FilterName”
args2(1).Name = “ConvertOOoTargetToPDFTarget”
args2(1).Value = True
If sFileNameExtension = “odt” then
args2(0).Value = “writer_pdf_Export”
ElseIf sFileNameExtension = “ods” then
args2(0).Value = “calc_pdf_Export”
ElseIf sFileNameExtension = “odm” then
args2(0).Value = “writer_globaldocument_pdf_Export”
End If

If FileExists(sPdfUrl) Then
oFileAccess.setReadOnly(sPdfUrl, False)
End If
oDoc.storeToURL(sPdfUrl, args2())

That last line just gives me:

BASIC runtime error.
An exception occurred
Type: com.sun.star.io.IOException
Message: SfxBaseModel::impl_store <file:///L:/QMS%20Documents/Controlled/(QP)%20Measurement%20Capability%20Assurance.pdf> failed: 0x20d(Error Area:Io Class:General Code:13).

Oops! Turns out the URL it needed to save to was a .pdf file that was already open in my .pdf reader program. Once I closed that, everything worked fine. I need to put a safeguard in for that problem.

Create an Error handling part in your macro.