OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Creating a simple text file

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
briant79
General User
General User


Joined: 19 Sep 2006
Posts: 8
Location: Basel, Switzerland

PostPosted: Wed Sep 27, 2006 6:29 am    Post subject: Creating a simple text file Reply with quote

Hello,

I am trying to create a text file out of a macro with the following code:

Code:

...
If Not oFileAccess.exists(strDateipfad) Then
   oFileContentProvider = createUnoService("com.sun.star.ucb.FileContentProvider")
   oFileContent = oFileContentProvider.queryContent(strFolderURL)
   oFolder = oFileContent.execute("open", -1, null)
   oFile = oFolder.createNewContent("application/vnd.sun.staroffice.fsys-file")
   aDokument(0).name = "Title"
   aDokument(0).value = "Test.txt"
   oFile.execute("insert" , -1, aDokument())
End If
...


As OpenOffice crashes already during the .queryContent() command, I guess there is potential for improvement of my code;)

Could anyone give me a hint, how to make this work?

Thanks in advance.
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1313

PostPosted: Sun Oct 01, 2006 12:38 pm    Post subject: Reply with quote

no idea whats wrong there - maybe your url is wrongly formatted ?

But why are you using this complicated approach ? The SimpleFileAccess routines ( http://api.openoffice.org/docs/common/ref/com/sun/star/ucb/XSimpleFileAccess.html ) are much more easy to use. There are numerous examples ...
Back to top
View user's profile Send private message
briant79
General User
General User


Joined: 19 Sep 2006
Posts: 8
Location: Basel, Switzerland

PostPosted: Sun Oct 01, 2006 10:35 pm    Post subject: Reply with quote

I checked the folder URL and it looks ok. Even with convertToUrl OpenOffice crashes.

The reason why I am trying to use this complicated approach is that I have not found another one, that enables me to create a NEW text file. I checked the simplefileaccess but was sure, that it supports only operations with existing files, but no method to create a new one.

I am certainly open to every easier approach. If there is a possibility with the simplefileaccess service (I have not found any on the web), I would be glad if you could post a short example.
Back to top
View user's profile Send private message
noranthon
Super User
Super User


Joined: 07 Jul 2005
Posts: 3323

PostPosted: Sun Oct 01, 2006 11:32 pm    Post subject: Reply with quote

Have you looked at Andrew Pitonyak's document?

I see these sections which may help:
5.37. Writing to a file
19.122. Shell Function
_________________
search forum by month
Back to top
View user's profile Send private message Visit poster's website
briant79
General User
General User


Joined: 19 Sep 2006
Posts: 8
Location: Basel, Switzerland

PostPosted: Mon Oct 16, 2006 4:36 am    Post subject: creating a simple text file Reply with quote

I have checked Andrew Pitonyak's document, but did not find anything that led me to a solution. After a while of experimentation I resolved my problem as follows:

Quote:
As OpenOffice crashes already during the .queryContent() command, I guess there is potential for improvement of my code;)


OpenOffice crashed, because I passed a file URL string, instead of a ContentId to the .queryContent method.

Quote:
I checked the simplefileaccess but was sure, that it supports only operations with existing files, but no method to create a new one.


The interface XSimpleFileAccess2 offers such a method. It is called writeFile. I can now create a new text file by simply passing the destination URL to the writeFile method.

Code:
oFileAccess = createUnoService("com.sun.star.ucb.SimpleFileAccess")
oFileAccess.writeFile(strFileURL, NULL)


Finally I could nevertheless figure out how to create a file/folder with the FileContentProvider. My code to create a sub folder into an existing one looks like this:

Code:
Sub createAdressbuchdatei

   Dim oFileContentProvider As Object
   Dim oFileContent As Object
   Dim oFileAccess As Object
   Dim oContentInfo As Object
   Dim oContentProp As Object
   Dim aOrdnerProps(0) As New com.sun.star.beans.PropertyValue
   Dim oAuftragsordner As Object
   Dim oCommand As Object
   Dim oInsertCommandArg As Object

   oFileAccess = createUnoService("com.sun.star.ucb.SimpleFileAccess")
   'Überprüfen, ob der Dokumentenordner verfügbar ist
   If oFileAccess.exists(strDokumentenordnerURL) Then
      'Dateisystemzugriff vorbereiten
      oFileContentProvider = createUnoService("com.sun.star.ucb.FileContentProvider")
      oContentInfo = createUnoStruct("com.sun.star.ucb.ContentInfo")
      oContentProp = createUnoStruct("com.sun.star.beans.Property")
      oCommand = createUnoStruct("com.sun.star.ucb.Command")
      oInsertCommandArg = createUnoStruct("com.sun.star.ucb.InsertCommandArgument")
      'Auftragsgrundordner referenzieren
      oFileContent = oFileContentProvider.queryContent( _
                oFileContentProvider.createContentIdentifier(strDokumentenordnerURL))
      'Neuen Auftragsordner erzeugen
      oContentProp.name = "Title"
      oContentProp.handle = -1
      oContentInfo.type = "application/vnd.sun.staroffice.fsys-folder"
      oContentInfo.attributes = com.sun.star.ucb.ContentInfoAttribute.KIND_FOLDER
      oContentInfo.properties = array(oContentProp)
      oAuftragsordner = oFileContent.createNewContent(oContentInfo)
      'Neuem Auftragsordner einen Namen zuweisen
      aOrdnerProps(0).name = "Title"
      aOrdnerProps(0).value = "A-" & txtAuftragsnr.text
      oCommand.name = "setPropertyValues"
      oCommand.handle = -1
      oCommand.argument = aOrdnerProps()
      oAuftragsordner.execute(oCommand, 0, NULL)
      'Neuen Auftragsordner ins Dateisystem einfügen
      oInsertCommandArg.replaceExisting = true
      oCommand.name = "insert"
      oCommand.argument = oInsertCommandArg   
      oAuftragsordner.execute(oCommand, 0, NULL)
      'Objekte schliessen
      oFileContentProvider = Nothing
   End If
   oFileAccess = Nothing

End Sub


I guess, that if I wanted to create a file instead of a folder, I simply had to change oContentInfo.type into "application/vnd.sun.staroffice.fsys-file" and oContentInfo.attribute to com.sun.star.ucb.ContentInfoAttribute.KIND_DOCUMENT.

I hope this will provide help to other people that struggle with the same problem.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group