Opening a file with a macro given the path in URL form

I attempt to accomplish this with the following code snippet…

oDoc = StarDesktop.LoadComponentFromUrl(filePath, "_blank", 0, Noargs())

The variable filePath contains the string “file://Gem_Invoices/49S803A.pdf”
But I get the following error…

BASIC runtime error.
An exception occurred 
Type: com.sun.star.lang.IllegalArgumentException
Message: Unsupported URL <file://Gem_Invoices/49S803A.pdf>: "type detection failed".

Help me please, I am not very good at reading paths in the form of URL. This “/Gem_Invoices/” - where is this? Is this folder somewhere? Where? Perhaps you should tell the macro where to look for Gem_Invoices, and the file in this folder will be easier to find?

file://Gem_Invoices/49S803A.pdf is really not a valid URL. It should be absolute (and start with file:///c:/path/to/Gem_Invoices/49S803A.pdf - notice the number of ‘/’ after the protocol).

Hello,

Believe you are using the incorrect method. LoadComponentFromUrl is for loading into a new or existing frame. See → XComponentLoader Interface Reference

If you want a command within basic, you can use the Shell command. See → Shell Function

As the example in the link, you specify the program to open and the file it is to open:

Sub ExampleShellForWin
    Shell("c:\windows\acrobat.exe  c:\windows\documents\my.pdf",2)
End Sub

My file path is in a macro variable. How can I pass a macro variable to the shell?

I tried constructing the entire shell command in a macro variable…

ShellCommand = "c:\windows\acrobat.exe e:\Gem_Invoices\49S803A.pdf"

Then I tried using that variable in the Shell statement…

Shell (shellCommand,2)

I got a file not found error
I tried hard coding the path in the Shell command as you suggested and still got a file not found error.

Hello,

The code presented was just an example. Is that the exe of your PDF reader. Should replace with correct exe.

On Ubuntu20.04, this code works:

Sub myShell
Shell ("atril /home/ANOTHER_DIRECTORY/ColumnTest.pdf", 2)
End Sub

as atril is the document viewer.

Just switched to Win 10 and installed a pdf viewer. This code worked:

Sub myShell
Shell ("C:\Program Files (x86)\PDF Reader\PDF Reader.exe C:\Users\MY_DIRECTORY\TESTpdf.pdf")
End Sub

Note the ,2 is not there was giving an error and eliminating resolved the error. That is supposed to determine where focus goes to.

And this is using the name of the program installed on my PC. Yours may certainly be different.

Also was incorrect about using incorrect method. On Ubuntu, using absolute path as stated by @mikekaganski and your LoadComponentFromUrl statement to open in LO, all worked fine.

My pdf document opened in Draw.

Thank you Mr. Ratsinger, that did the trick. I found the correct path to my Adobe Reader executable and removed the 2 from the parameters to the shell command and viola. I actually prefer this to the LoadCOmponentFromURL method for displaying PDF files.

Solved thanks to Mr. Ratslinger

Shell('C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe  E:\Gem_Invoices\RZV063.pdf')

The shell command was assembled in a macro variable ‘shellCommand’ and then the command Shell(shellCommand) worked fine