Difference between revisions of "Saving a document"
(Slight extension of warning) |
|||
(6 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
'''To save an existing document: ''' | '''To save an existing document: ''' | ||
− | < | + | <source lang="oobas"> |
thisComponent.store() | thisComponent.store() | ||
− | </ | + | </source> |
But, this maybe a bit too simplistic. So a more generic approach might be: | But, this maybe a bit too simplistic. So a more generic approach might be: | ||
− | < | + | <source lang="oobas"> |
function fnSave(oDoc) | function fnSave(oDoc) | ||
if oDoc.hasLocation AND Not oDoc.isReadOnly Then | if oDoc.hasLocation AND Not oDoc.isReadOnly Then | ||
Line 21: | Line 21: | ||
fnSave = false | fnSave = false | ||
end function | end function | ||
− | </ | + | </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]]. | ||
Line 27: | Line 27: | ||
To "save as": | To "save as": | ||
− | < | + | <source lang="oobas"> |
sub subSaveAs(oDoc, sFile, optional sType) | sub subSaveAs(oDoc, sFile, optional sType) | ||
sURL = convertToURL(sFile) | sURL = convertToURL(sFile) | ||
Line 40: | Line 40: | ||
end if | end if | ||
end sub | end sub | ||
− | </ | + | </source> |
+ | '''WARNING''' There is a bug in either the API documentation or the OO code (see [https://issues.apache.org/ooo/show_bug.cgi?id=121665 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): | To "export as" (leave the URL of the current document alone but save a copy of the file in the specified format): | ||
− | < | + | <source lang="oobas"> |
sub subExportAs(oDoc, sFile, optional sType) | sub subExportAs(oDoc, sFile, optional sType) | ||
sURL = convertToURL(sFile) | sURL = convertToURL(sFile) | ||
Line 56: | Line 58: | ||
end if | end if | ||
end sub | end sub | ||
− | </ | + | </source> |
Examples of calling these routines: | Examples of calling these routines: | ||
− | < | + | <source lang="oobas"> |
subSaveAs(thisComponent, "C:\tmp\NewName.sxw") 'Windows example | subSaveAs(thisComponent, "C:\tmp\NewName.sxw") 'Windows example | ||
subExportAs(thisComponent, "/home/ian/Example.txt", "Text") 'Linux example | subExportAs(thisComponent, "/home/ian/Example.txt", "Text") 'Linux example | ||
− | </ | + | </source> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
Line 74: | Line 72: | ||
The following routine, written by Danny Brewer (copied from [[http://www.oooforum.org/forum/viewtopic.phtml?t=3549 here]]) creates a new document and enters the current filters. | The following routine, written by Danny Brewer (copied from [[http://www.oooforum.org/forum/viewtopic.phtml?t=3549 here]]) creates a new document and enters the current filters. | ||
− | < | + | <source lang="oobas"> |
Sub Main | Sub Main | ||
oFF = createUnoService( "com.sun.star.document.FilterFactory" ) | oFF = createUnoService( "com.sun.star.document.FilterFactory" ) | ||
Line 88: | Line 86: | ||
For i = LBound( oFilterNames ) To UBound( oFilterNames ) | For i = LBound( oFilterNames ) To UBound( oFilterNames ) | ||
oText.insertString( oCursor, oFilterNames(i), False ) | oText.insertString( oCursor, oFilterNames(i), False ) | ||
− | + | oText.insertControlCharacter( oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False ) | |
Next | Next | ||
End Sub | End Sub | ||
− | </ | + | </source> |
+ | |||
+ | [[Category:Basic:Tutorials]] |
Revision as of 12:03, 2 February 2013
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.org 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