Difference between revisions of "Saving a document"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
'''To save an existing document: '''
 
'''To save an existing document: '''
  
<code>[oobas]
+
<syntaxhighlight lang="oobas">
 
thisComponent.store()
 
thisComponent.store()
</code>
+
</syntaxhighlight>
 
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:  
  
<code>[oobas]
+
<syntaxhighlight 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
</code>
+
</syntaxhighlight>
  
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":  
  
<code>[oobas]
+
<syntaxhighlight 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
</code>
+
</syntaxhighlight>
 +
 
 +
'''WARNING:''' There is a bug in either the API documentation or the {{AOo}} code (see {{Bug|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 OpenOffice.org 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):  
  
<code>[oobas]
+
<syntaxhighlight lang="oobas">
 
sub subExportAs(oDoc, sFile, optional sType)
 
sub subExportAs(oDoc, sFile, optional sType)
 
sURL = convertToURL(sFile)
 
sURL = convertToURL(sFile)
Line 56: Line 59:
 
end if
 
end if
 
end sub
 
end sub
</code>
+
</syntaxhighlight>
  
 
Examples of calling these routines:  
 
Examples of calling these routines:  
  
<code>[oobas]
+
<syntaxhighlight 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
</code>
+
</syntaxhighlight>
  
  
Line 70: Line 73:
 
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.  
  
<code>[oobas]
+
<syntaxhighlight lang="oobas">
 
Sub Main
 
Sub Main
 
oFF = createUnoService( "com.sun.star.document.FilterFactory" )
 
oFF = createUnoService( "com.sun.star.document.FilterFactory" )
Line 84: Line 87:
 
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 )
+
  oText.insertControlCharacter( oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False )
 
Next
 
Next
 
End Sub
 
End Sub
</code>
+
</syntaxhighlight>
  
 
[[Category:Basic:Tutorials]]
 
[[Category:Basic:Tutorials]]
[[Category:Tutorials]]
 

Latest revision as of 14:43, 24 August 2022

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 Apache OpenOffice 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 OpenOffice.org 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
Personal tools