I have a macro that saves a document to a .pdf file. Trouble is, .storeToURL fails if the destination .pdf URL is already open in Reader. Is there a way that I can have the macro check if that is the case (and ideally, close it)? Because Reader isn’t an editor, no lock file is created, so checking for that would be no help.
I’m thinking the simpleFileAccess.kill() method might do the trick. From what I’ve read in the api, it deletes the file.
I’m wondering what kill() will do with a file that’s already in use. (It might just throw an error.)
How (or if at all) such things can be done should depend on the OS and the user rights. .
If on Windows: Did you try to call TASKKILL
with /FI
(or probably /IM
) and, maybe, /F
?
(I personally never did it, but it should work.)
Did you try?
Yes. I got this error message:
BASIC runtime error.
An exception occurred
Type: com.sun.star.ucb.InteractiveAugmentedIOException
Message: a file status object could not be filled.
Was curious and found the time to test under Win10. The following code worked as intended:
Sub taskkill(Optional pTaskkillParams As String)
REM For testing only;
REM My PdfReader currently is Foxit.
REM The shown file of two opened files was the well-known text by Andrew Pitonyak.
If IsMissing(pTaskkillParams) Then _
pTaskkillParams = _
"/C TASKKILL /FI ""WINDOWTITLE eq Useful Macro Information - Foxit Reader"" /F"
cmdExe = ConvertToURL("C:\Windows\SysWOW64\CMD.exe")
Shell(cmdExe, 0, pTaskkillParams)
End Sub
Please note: The reader is closed, not just the one of more open files which spent the title.
An external handling of details would need a grip on the application I wouldn’t like to be granted to a different app.
You can try to open the PDF file for Append. Other way is try to rename it. But it is functional if the original PDF is opened in the Reader. But for example the interner browsers copy the PDF to the temporary directory and show a copy - and then the detection by this way is impossible.
Sub isPDFopen 'try to open the file for Append; opened file will do error
on local error goto bug
dim path$
path=ConvertToUrl("d:\aaa.pdf")
open path for append as #1
close #1
msgbox path & chr(13) & "isn't opened"
exit sub
bug:
msgbox path & chr(13) & "Opened!"
End Sub
sub isPDFopen2 'try to rename the file; opened file will do error
on local error goto bug
dim path$, s$
path=ConvertToUrl("d:/aaa.pdf") 'pdf
s=path & ".named" 'new name: ....pdf.named
name path as s 'rename the file
name s as path 'rename back
msgbox path & chr(13) & "isn't opened"
exit sub
bug:
msgbox path & chr(13) & "Opened!"
end sub
Thanks!
I tried implementing the isPDFopen approach. Unfortunately, the first time the file is open somewhere and this routine is run, the open for append causes a jump straight to ‘bug’, ‘so the close #1’ line doesn’t run. The result is that even after the .pdf reader is closed, that file will always show as opened. This perplexes me a great deal. It seems like if the ‘open’ line throws an error, it failed, and the file shouldn’t be open. Does anyone know how to avoid this? Edit: I tried the most obvious approach: putting a copy of the ‘close’ line in the ‘bug’ section, but then, that line in the ‘bug’ section throws:
BASIC runtime error.
Invalid file name or file number.
It would seem that in the goto jump, what #1 refers to is forgotten.
Last edit: The real problem was that when I implemented this approach, rather than putting this in as a separate macro, I just inserted the code into my existing macro, and when I did, I inadvertently put it in before the line that defined the string variable containint the pdf url, so an error was triggered not because the file was open, but because it was trying to open a blank file name for appending.
If the ‘open’ line throws an error, it means the PDF is opened in other application and the macro didn’t open PDF. If the ‘open’ line is OK, then the PDF isn’t opened in the Reader, so ‘open’ line will open the PDF, and in next line it will close PDF. So it isn’t possible to use close #1 after error in ‘open’ line.
But I’m confused you close the Reader and the macro shows you ‘Opened’ for the PDF. Do you really know the PDF isn’t open somewhere in background? What to reset a computer and try it again, and try also second macro with renaming :-)?