Difference between revisions of "Saving a document"
| Line 64: | Line 64: | ||
subExportAs(thisComponent, "/home/ian/Example.txt", "Text") 'Linux example | subExportAs(thisComponent, "/home/ian/Example.txt", "Text") 'Linux example | ||
</code> | </code> | ||
| − | |||
| − | |||
| − | |||
| − | |||
Revision as of 00:12, 2 May 2006
To save an existing document:
[oobas]
thisComponent.store()
But, this maybe a bit too simplistic. So a more generic approach might be:
[oobas]
function fnSave(oDoc)
if oDoc.hasLocation AND Not oDoc.isReadOnly Then
fnSave = true
on local error goto notSaved
oDoc.store()
on error goto 0
else
fnSave = fnDispatch("SaveAs")
end If
exit function
notSaved:
fnSave = false
end function
fnDispatch is defined in The OpenOffice.org recorder and UNO dispatch calls.
To "save as":
[oobas]
sub subSaveAs(oDoc, sFile, optional sType)
sURL = convertToURL(sFile)
if isMissing(sType) then
oDoc.storeAsURL(sURL, array())
else
dim mFileType(0)
mFileType(0) = createUnoStruct("com.sun.star.beans.PropertyValue")
mFileType(0).Name = "FilterName"
mFileType(0).Value = sType
oDoc.storeAsURL(sURL, mFileType())
end if
end sub
To "export as" (leave the URL of the current document alone but save a copy of the file in the specified format):
[oobas]
sub subExportAs(oDoc, sFile, optional sType)
sURL = convertToURL(sFile)
if isMissing(sType) then
oDoc.storeToURL(sURL, array())
else
dim mFileType(0)
mFileType(0) = createUnoStruct("com.sun.star.beans.PropertyValue")
mFileType(0).Name = "FilterName"
mFileType(0).Value = sType
oDoc.storeToURL(sURL, mFileType())
end if
end sub
Examples of calling these routines:
[oobas]
subSaveAs(thisComponent, "C:\tmp\NewName.sxw") 'Windows example
subExportAs(thisComponent, "/home/ian/Example.txt", "Text") 'Linux example
File Types
The following routine, written by Danny Brewer (copied from [here]) creates a new document and enters the current filters.
[oobas]
Sub Main
oFF = createUnoService( "com.sun.star.document.FilterFactory" )
oFilterNames = oFF.getElementNames()
' Create a Writer doc and save the filter names to it.
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
oText = oDoc.getText()
oCursor = oText.createTextCursor()
oCursor.gotoEnd( False )
' Print the filter names into a Writer document.
For i = LBound( oFilterNames ) To UBound( oFilterNames )
oText.insertString( oCursor, oFilterNames(i), False )
oText.insertControlCharacter( oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False )
Next
End Sub