| View previous topic :: View next topic |
| Author |
Message |
briant79 General User

Joined: 19 Sep 2006 Posts: 8 Location: Basel, Switzerland
|
Posted: Wed Sep 27, 2006 6:29 am Post subject: Creating a simple text file |
|
|
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 |
|
 |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1313
|
|
| Back to top |
|
 |
briant79 General User

Joined: 19 Sep 2006 Posts: 8 Location: Basel, Switzerland
|
Posted: Sun Oct 01, 2006 10:35 pm Post subject: |
|
|
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 |
|
 |
noranthon Super User

Joined: 07 Jul 2005 Posts: 3323
|
Posted: Sun Oct 01, 2006 11:32 pm Post subject: |
|
|
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 |
|
 |
briant79 General User

Joined: 19 Sep 2006 Posts: 8 Location: Basel, Switzerland
|
Posted: Mon Oct 16, 2006 4:36 am Post subject: creating a simple text file |
|
|
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 |
|
 |
|