[Solved] What is the filtername to open a text document ?

Creating a macro - Writing a Script - Using the API

[Solved] What is the filtername to open a text document ?

Postby taupe59 » Mon Jan 11, 2010 10:44 am

Hello,

I am a newbie in OO basic programming. I am triying to write a macro that open a text document, whether the extension is .txt or not.

I wrote the following code

sub ouverture
Dim oSpreadsheetDocument As Object
Dim oTextDocument As Object
dim sUrl as string
Dim mNoArgs()

oDesktop = createUnoService("com.sun.star.frame.Desktop")
sUrl =ConvertToURL("D:\Documents and Settings\XXXXX\Mes documents\macro open office\exemple.txt")
msgbox FileExists(sUrl)

oTextDocument = oDesktop.loadComponentFromURL(sUrl,"_blank",0,mNoArgs)
end sub


It works just fine to open .odt and .txt document, but would crash when trying to open a text document with other extension.

I figure out that I must use a specific filter to do it. But I couldn't find the list of filter name where I can find this information.

Any help would be welcome...
Last edited by taupe59 on Wed Jan 13, 2010 10:16 pm, edited 1 time in total.
OpenOffice 3.1 on XP sp3 & Mac OS10.4
taupe59
 
Posts: 18
Joined: Mon Jan 11, 2010 10:28 am
Location: France

Re: What is the right filtername to open a text document ?

Postby rudolfo » Mon Jan 11, 2010 3:09 pm

You might check the BASIC macro guide particularly the section about loadComponentFromURL Method Options.
Yes, there is a problem with the file TypeDetection.xml that should be used to figure out the allowed filter names: It doesn't exist in my OOo 3.1 installation, neither in my 2.4 ?!
But this is one of the rare cases where the macro recorder is useful: Save a .odt file as plain text (.txt) and check out the recorded dispatcher code for .Name = "FilterName".
The following code works for me:
Code: Select all   Expand viewCollapse view
Sub loadPlainTextFile
  Dim oDoc As Object
  Dim FileProperties(0) As New com.sun.star.beans.PropertyValue
  Dim sUrl As String

  sUrl = ConvertToURL("C:\addresses.ldif")
  FileProperties(0).Name = "FilterName"
  FileProperties(0).Value = "Text"
  oDoc = StarDesktop.loadComponentFromURL(sUrl, "_blank", 0, FileProperties())
End Sub
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.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: What is the right filtername to open a text document ?

Postby taupe59 » Mon Jan 11, 2010 6:28 pm

Thank you for your help.

I've modified my macro :
Good news : it doesn'et crash
Bad news : it does'nt actually open the text doc : the corresponding window does'nt appear in writer.

The strange thing is that it correctly opens any .odt or .txt in a new writer window,
Something must be missing.

Any idea ?
Thanks
OpenOffice 3.1 on XP sp3 & Mac OS10.4
taupe59
 
Posts: 18
Joined: Mon Jan 11, 2010 10:28 am
Location: France

Re: What is the right filtername to open a text document ?

Postby rudolfo » Tue Jan 12, 2010 3:38 am

Did you actually read the wiki with the BASIC tutorial?
You may have noticed that my macro doesn't use the createUnoService method, instead it uses the predefined variable StarDesktop. I copied it from the tutorial and changed it to make it more Writer centric while the original macro uses a Calc filter.

Though it would be a bit strange it seems like the createUnoService() creates a second instance of the OpenOffice Desktop core object. This second instance might be hidden "behind" the always existing permanent instance of the desktop object.
Please try with StarDesktop and see if this makes a difference.
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.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: What is the right filtername to open a text document ?

Postby taupe59 » Tue Jan 12, 2010 12:31 pm

Yes, I have noticed it ant my macro looks now like this :

Code: Select all   Expand viewCollapse view
sub ouverture
Dim oTextDocument As Object
Dim FileProperties(0) As New com.sun.star.beans.PropertyValue
dim sUrl as string

sUrl =ConvertToURL("D:\Documents and Settings\..\Mes documents\macro open office\exemple.ofx")
msgbox FileExists(sUrl)

FileProperties(0).Name = "FilterName"
FileProperties(0).Value = "Text"

oTextDocument = StarDesktop.loadComponentFromURL(sUrl,"_blank",0,FileProperties())
end sub


Is there any function to hide/unhide documents (like Show, in VisualBasic ?)

I'd a look at the tuto butdi not find any answer about this question.
OpenOffice 3.1 on XP sp3 & Mac OS10.4
taupe59
 
Posts: 18
Joined: Mon Jan 11, 2010 10:28 am
Location: France

Re: What is the right filtername to open a text document ?

Postby rudolfo » Tue Jan 12, 2010 1:53 pm

That's strange. I tried several starting points to run this macro and in all cases I saw the Writer window with the plain text document activated as foreground window. My different starting points are:
  • Started from the Macro Editor IDE (as the only OOo window that's open)
  • Started from Writer with an empty "Untitled 1" document (ldif Document is opened in another window, usually when opening an existing document manually through menu commands the "Untitled 1" will vanish)
  • Started from Writer with an existing .odt document (same as above with the "Untitled 1")
  • Started the macro from the Welcome Frame (the one that is new with 3.x): Again the plain text document is opened in the foreground, but the welcome window stays open (The welcome window should normally vanish)
What operating system are you using? I use Windows 2000. Maybe someone with a different OS can help us?!

 Edit: Just another thought: Can you see the Message Box with the File exists information? Is the behaviour different if you remove this MsgBox line from your macro code? 
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.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: What is the right filtername to open a text document ?

Postby taupe59 » Tue Jan 12, 2010 5:28 pm

I think I have found part of the solution of the problem.

My file is an ASCII file. When I manually open it, it opens a dialog box "ASCII filter options", where you have to choose caracters set, police and language.

If I create another text file wiht Notepad, call example2.ofx, no problem : the macro opens the file and display it.

So I think that the filter "Text" is not enought : there must be some other parameters to set, but how ?

Concerning the dialog box, yes it works (just to make sure that the file actually exists !)

I am using Windows XP family edition SP3
OpenOffice 3.1 on XP sp3 & Mac OS10.4
taupe59
 
Posts: 18
Joined: Mon Jan 11, 2010 10:28 am
Location: France

Re: What is the right filtername to open a text document ?

Postby rudolfo » Tue Jan 12, 2010 7:11 pm

I know this kind of filter dialog only when reading the file into Calc, but Writer usually "eats" anything without confirmation. Maybe you can upload a screenshot of this dialog.

As you were talking about character sets I gave it a go with a UTF-8 encoded file with some german umlauts:
  • the original file in Notepad: Einführungskurs
  • after "imported" into Writer: Einführungskurs if the original file doesn't have the initial byte order mark ef bb bf
  • with the BOM ef bb bf after import in Writer: Einführungskurs
This is typically what you get when you have a file with some double byte utf-8 characters but the editing application (OOo Writer in our case) reads it as a 7 bit ascii or an iso-8859-1 file, because the file is lacking the BOM byte sequence.

In both cases (with BOM or without) the file is directly loaded and displayed in Writer (as Preformated Text with a fixed width font) without bothering me with a conversion dialog. Something with your file must be really strange. I can't recall that the extension .ofx is something special, so OOo would not treat it in an extraordinary way.
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.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: What is the right filtername to open a text document ?

Postby taupe59 » Tue Jan 12, 2010 11:19 pm

.ofx is some kind of financial data interchange file. I use it to download account statement from the bank.

The file looks like this :

Code: Select all   Expand viewCollapse view
OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE

<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<DTSERVER>20100109174906
<LANGUAGE>FRA
</SONRS>
</SIGNONMSGSRSV1>
<BANKMSGSRSV1>
<STMTTRNRS>
<TRNUID>51211
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<STMTRS>
<CURDEF>EUR
<BANKACCTFROM>
....



I have attached the copy of the dialog box.

When I open the .ofx file in Notepad, I get the same content, but without the LF/CR at the end of the line.

You must be right, there is something in the OFX file that makes the macro behave in a strangely manner.
Attachments
dialog box.PNG
OpenOffice 3.1 on XP sp3 & Mac OS10.4
taupe59
 
Posts: 18
Joined: Mon Jan 11, 2010 10:28 am
Location: France

Re: What is the right filtername to open a text document ?

Postby taupe59 » Tue Jan 12, 2010 11:43 pm

I have tried something else :

I have recorded a macro to import the ofx file into an empty writer document.
(While importing manually, the same ASCII filter dialog box opened)

The code generated is as follow :


Code: Select all   Expand viewCollapse view
sub import
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:InsertDoc", "", 0, Array())

rem ----------------------------------------------------------------------
dim args2(1) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Name"
args2(0).Value = "file:///D:/Documents%20and%20Settings/xxx/Mes%20documents/macro%20open%20office/exemple.ofx"
args2(1).Name = "Filter"
args2(1).Value = "Text (encoded)"

dispatcher.executeDispatch(document, ".uno:InsertDoc", "", 0, args2())


end sub


This time, it worked...
OpenOffice 3.1 on XP sp3 & Mac OS10.4
taupe59
 
Posts: 18
Joined: Mon Jan 11, 2010 10:28 am
Location: France

Re: What is the right filtername to open a text document ?

Postby rudolfo » Wed Jan 13, 2010 3:05 pm

taupe59 wrote:args2(1).Name = "Filter"
args2(1).Value = "Text (encoded)"


Did you realise that the macro used Filter for the parameter name and not Filtername?
In the Basic documentation wiki as referenced above I find only Filtername, FilterData and FilterOptions, but barely Filter seems to be undocumented or was added in later versions of OOo that are not yet covered by that macro documentation.

When I used your code I could reproduce the behaviour that you described (but for me there is still no character set dialog popping up!):
Code: Select all   Expand viewCollapse view
Sub loadPlainTextFile
  Dim oDoc As Object
  Dim FileProperties(0) As New com.sun.star.beans.PropertyValue
  Dim sUrl As String

  sUrl = ConvertToURL("C:\home\run\calendar_1262007213789.ics")
  FileProperties(0).Name = "Filter"
  FileProperties(0).Value ="Text (encoded)"

  oDoc = StarDesktop.loadComponentFromURL(sUrl, "_blank", 0, FileProperties())
  Xray oDoc
End Sub

The .ics file is the one from my last post (UTF-8, but without BOM) and Xray (an object inspector tool -- very useful, just google for it and you will find where to download it) tells me that the object variable oDoc is NULL.
That means it is not the document that is hidden, but loading the document with loadComponentFromURL failed!

Can't really say it from the .ofx content that you cut and pasted here, but what you tell about notepad not showing the linebreaks looks like the content has Unix linebreaks (LF only) and maybe at the last line a CR/LF. A single LF is used during the export to encoded text for soft line breaks (start a new line in WYSIWYG, but not a new paragraph) while a full DOS CR/LF completes a paragraph and starts a new one in Writer WYSIWYG.

So I think if you are asked for ascii filter options it is not for the characterset, but for the linebreak because OOo finds two types of linebreaks in the file content.
Hm, if I try to import a file with CR/LF and LF at the same time it directly shows the open document, no options dialog!

Maybe someone else knows more than me about this undocumented "Filter" parameter?!
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.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: What is the right filtername to open a text document ?

Postby taupe59 » Wed Jan 13, 2010 10:00 pm

Did you realise that the macro used Filter for the parameter name and not Filtername?
In the Basic documentation wiki as referenced above I find only Filtername, FilterData and FilterOptions, but barely Filter seems to be undocumented or was added in later versions of OOo that are not yet covered by that macro documentation.


No, I did'nt : I just recorded the macro, and this is how it came out !
It's my first attempt to make an OO macro, and truly speaking, I am not yet used to the semantic of OOBasic. On top of that, finding the documentation is a headache...

Back to the problem, importing the OFX document in a blank frame seems to work. So I will develop this way.

Thank you for your help.
OpenOffice 3.1 on XP sp3 & Mac OS10.4
taupe59
 
Posts: 18
Joined: Mon Jan 11, 2010 10:28 am
Location: France

Re: [Solved] What is the filtername to open a text document

Postby pansmanse » Sat Mar 11, 2017 5:43 pm

OK, this is an old topic, but thanks to Rudolfo for an excellent tip in answering a related question: use the macro recorder to find the answer.
Pansmanser
LO3.5.7.2/Ubuntu12.04
pansmanse
 
Posts: 26
Joined: Wed Nov 11, 2009 4:30 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests