Difference between revisions of "Saving a document"
(Slight extension of warning) |
|||
Line 23: | Line 23: | ||
</source> | </source> | ||
− | fnDispatch is defined in [[ The OpenOffice.org recorder and UNO dispatch calls]]. | + | fnDispatch is defined in [[The OpenOffice.org recorder and UNO dispatch calls|The OpenOffice recorder and UNO dispatch calls]]. |
To "save as": | To "save as": |
Revision as of 15:38, 28 January 2021
To save an existing document:
thisComponent.store()
But, this maybe a bit too simplistic. So a more generic approach might be:
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 recorder and UNO dispatch calls.
To "save as":
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
WARNING There is a bug in either the API documentation or the OO code (see Issue 121665 for the discussion). Unless the "Overwrite" property is set to FALSE in the MediaDescriptor, any existing file of the same name is overwritten without an error notification, which is probably not desired. This has only been tested with OO 3.4.1 under MS Windows - other versions and operating systems may vary.
To "export as" (leave the URL of the current document alone but save a copy of the file in the specified format):
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:
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.
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