Difference between revisions of "Saving a document"
From Apache OpenOffice Wiki
(remove from Category:Tutorials if in Category:Basic:Tutorials) |
|||
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> |
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 56: | ||
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 70: | Line 70: | ||
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 87: | Line 87: | ||
Next | Next | ||
End Sub | End Sub | ||
− | </ | + | </source> |
[[Category:Basic:Tutorials]] | [[Category:Basic:Tutorials]] |
Revision as of 17:42, 10 July 2008
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
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