[Solved] Open .rtf file in Calc

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Alex12345678910
Posts: 3
Joined: Fri Feb 25, 2011 7:18 am

[Solved] Open .rtf file in Calc

Post by Alex12345678910 »

Hi. I googled tons of sites trying to understand how to open file in specific application to be exact "Calc". I've got js code something like this:

Code: Select all

 Arr=new Array()
 ServiceManager=new ActiveXObject('com.sun.star.ServiceManager')
 Desktop=ServiceManager.createInstance('com.sun.star.frame.Desktop')
 Document=Desktop.LoadComponentFromURL(RTFFile,'_blank',0,Arr)
This code works but opens Calc or Writer depending on the content of opening file.
As a usual windows programmer logically i would use variable "Document" as object with the method "Open":

Code: Select all

 Arr=new Array()
 ServiceManager=new ActiveXObject('com.sun.star.ServiceManager')
 Desktop=ServiceManager.createInstance('com.sun.star.frame.Desktop')
 Document=Desktop.LoadComponentFromURL('private:factory/scalc',_blank',0,Arr)
 Document.Open(RTFFile)
But such concept does not work and there is no help in OO to find out OO's object model.
It looks like simple task, can somebody help with a string of needed code, please?
Last edited by Hagar Delest on Sat Feb 26, 2011 4:49 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.2.1 on Windows 7
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Open .rtf file in calc

Post by rudolfo »

Code: Select all

 Document=Desktop.LoadComponentFromURL('private:factory/scalc','_blank',0,Arr)
opens an empty Calc sheet. No need for an additional Document.Open() after that. And that's why this method doesn't exist. There is a sticky thread about the UNO object hierachy Need thorough documentation of OOo BASIC objects with quite a lot of links to helpful resources. Start with the wiki Basic Tutorial

Regarding the RTF file you won't be able to open it in Calc. It is a text document typ and will hence always be opened in Writer (with the help of an import module that has several limitations). In other words before automating what you want to do, I would do a manual open and click test with different rtf documents to figure out if OpenOffice can really do for you want you want to achieve.
Quite frustrating if you need 2 days of macro programming only to find out in the end that the RTF filter of OOo is not good enough for you.
 Edit: Added the missing quote before _blank. That's how it was in the code section of the initial post 
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
Alex12345678910
Posts: 3
Joined: Fri Feb 25, 2011 7:18 am

Re: Open .rtf file in calc

Post by Alex12345678910 »

Regarding the RTF file you won't be able to open it in Calc. It is a text document typ and will hence always be opened in Writer (with the help of an import module that has several limitations). In other words before automating what you want to do, I would do a manual open and click test with different rtf documents to figure out if OpenOffice can really do for you want you want to achieve.
Of course I tried to open file in calc. It is perfectly (!) performed with the use of menu item "Open as". Thanks a lot for links and your answer but I don't need to achieve a goal to be programmer in OO (which programming concept is "unusual") I just need a simple task to be done. So the question is still open and I look forward to help of gurus.
OpenOffice 3.2.1 on Windows 7
FJCC
Moderator
Posts: 9279
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Open .rtf file in Calc

Post by FJCC »

This Basic code opens an RTF file in Calc.

Code: Select all

Dim Propval(0) as New com.sun.star.beans.PropertyValue
Propval(0).Name = "FilterName"
Propval(0).Value = "Rich Text Format (StarCalc)"
Addr = convertToURL("C:\Import.rtf")
oRTF = StarDesktop.loadComponentFromURL(Addr, "_blank", 0, Propval())
The available filters for importing and exporting files are listed here
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Alex12345678910
Posts: 3
Joined: Fri Feb 25, 2011 7:18 am

Re: Open .rtf file in Calc

Post by Alex12345678910 »

It works! I love you FJCC :) . I tried the "filter" property with other variants found in some local file (don't remember which exactly) and that file did not contain "Rich Text Format (StarCalc)". Thank you a lot!
OpenOffice 3.2.1 on Windows 7
Post Reply