Difference between revisions of "Documentation/BASIC Guide/StarDesktop"

From Apache OpenOffice Wiki
Jump to: navigation, search
(The StarDesktop: add "ThisComponent" section)
m (Reverted edits by Demgeorge5 (Talk) to last revision by BMarcelly)
(48 intermediate revisions by 11 users not shown)
Line 5: Line 5:
 
|NextPage=Documentation/BASIC Guide/Templates
 
|NextPage=Documentation/BASIC Guide/Templates
 
|docs=block
 
|docs=block
}}<!-- {{DISPLAYTITLE:The StarDesktop -->
+
}}  
 +
{{DISPLAYTITLE:The StarDesktop}}
 +
__NOTOC__
 +
When working with documents, two services are used most frequently:
  
== The StarDesktop ==
+
* The <idl>com.sun.star.frame.Desktop</idl> service, which is similar to the core service of {{OOo}}. It provides the functions for the frame object of {{OOo}}, under which all document windows are classified. Documents can also be created, opened and imported using this service.
 +
* The basic functionality for the individual document objects is provided by the <idl>com.sun.star.document.OfficeDocument</idl> service. This provides the methods for saving, exporting and printing documents.
  
When working with documents, there are two services which are used most frequently:
+
The <idl>com.sun.star.frame.Desktop</idl> service is created automatically when {{OOo}} is started. This service can be addressed in {{OOo}} Basic using the global name <tt>StarDesktop</tt>.
  
* The <tt>com.sun.star.frame.Desktop</tt> service, which is similar to the core service of {{OOo}}. It provides the functions for the frame object of {{OOo}}, under which all document windows are classified. Documents can also be created, opened and imported using this service.
+
The most important interface of the <tt>StarDesktop</tt> is <idl>com.sun.star.frame.XComponentLoader</idl>. This basically covers the <tt>loadComponentFromURL</tt> method, which is responsible for creating, importing, and opening documents.
* The basic functionality for the individual document objects is provided by the <tt>com.sun.star.document.OfficeDocument</tt> service. This provides the methods for saving, exporting and printing documents.
+
  
The <tt>com.sun.star.frame.Desktop</tt> service opens automatically when {{OOo}} is started. To do this, {{OOo}} creates an object which can be reached by means of the global name <tt>StarDesktop</tt>.  
+
{{Documentation/SO5note|The name of the <tt>StarDesktop</tt> object dates back to StarOffice 5, in which all document windows were embedded in one common application called <tt>StarDesktop</tt>. In the present version of {{OOo}}, a visible <tt>StarDesktop</tt> is no longer used. The name <tt>StarDesktop</tt> was, however, retained for the frame object of {{OOo}} because it clearly indicates that this is a basic object for the entire application.  
  
The most important interface of the <tt>StarDesktop</tt> is <tt>com.sun.star.frame.XComponentLoader</tt>. This basically covers the <tt>loadComponentFromURL</tt> method, which is responsible for creating, importing, and opening documents.
+
The <tt>StarDesktop</tt> object replaces the <tt>Application</tt> object of StarOffice 5 which previously applied as a root object. However, unlike the old <tt>Application</tt> object, <tt>StarDesktop</tt> is primarily responsible for opening new documents. The functions resident in the old <tt>Application</tt> object for controlling the on-screen depiction of {{OOo}} (for example, <tt>FullScreen</tt>, <tt>FunctionBarVisible</tt>, <tt>Height</tt>, <tt>Width</tt>, <tt>Top</tt>, <tt>Visible</tt>) are no longer used.}}
  
The name of the <tt>StarDesktop</tt> object dates back to {{OOo}} 5, in which all document windows were embedded in one common application called <tt>StarDesktop</tt>. In the present version of {{OOo}}, a visible <tt>StarDesktop</tt> is no longer used. The name <tt>StarDesktop</tt> was, however, retained for the frame object of {{OOo}} because it clearly indicates that this is a basic object for the entire application.
+
{{Documentation/VBAnote|Whereas the active document in Word is accessed through <tt>Application.ActiveDocument</tt> and in Excel through <tt>Application.ActiveWorkbook,</tt> in {{OOo}}, the <tt>StarDesktop</tt> is responsible for this task. The active document object is accessed in {{OOo}} through the <tt>StarDesktop.CurrentComponent</tt> property, or through <tt>ThisComponent</tt>.}}
  
The <tt>StarDesktop</tt> object assumes the position of successor to the <tt>Application</tt> object of {{OOo}} 5 which previously applied as a root object. Unlike the old <tt>Application</tt> object however it is primarily responsible for opening new documents. The functions resident in the old <tt>Application</tt> object for controlling the on-screen depiction of {{OOo}} (for example, <tt>FullScreen</tt>, <tt>FunctionBarVisible</tt>, <tt>Height</tt>, <tt>Width</tt>, <tt>Top</tt>, <tt>Visible</tt>) are no longer used.
 
 
 
{{Documentation/Note|Whereas the active document in Word is accessed through <tt>Application.ActiveDocument</tt> and in Excel through <tt>Application.ActiveWorkbook,</tt> in {{OOo}}, the <tt>StarDesktop</tt> is responsible for this task. The active document object is accessed in {{OOo}} 2 through the <tt>StarDesktop.CurrentComponent</tt> property.}}
 
 
== ThisComponent ==
 
== ThisComponent ==
<tt>ThisComponent</tt> generally returns the same object as <tt>StarDesktop.CurrentComponent</tt>, with one significant advantage. If you are running from within the Basic IDE, debugging or exploring, then <tt>StarDesktop</tt> returns the Basic IDE itself. This is probably not what you want. <tt>ThisComponent</tt> returns the last previously active document.
+
The global name <tt>ThisComponent</tt> generally returns the same object as <tt>StarDesktop.CurrentComponent</tt>, with one significant advantage. If you are running from within the Basic IDE, debugging or exploring, then <tt>StarDesktop</tt> returns the Basic IDE itself. This is probably not what you want. <tt>ThisComponent</tt> returns the last previously active document.
  
 
== Basic Information about Documents in {{OOo}} ==
 
== Basic Information about Documents in {{OOo}} ==
Line 33: Line 32:
 
=== File Names in URL Notation ===
 
=== File Names in URL Notation ===
  
Since {{OOo}} is a platform-independent application, it uses URL notation (which is independent of any operating system), as defined in the Internet Standard RFC 1738 for file names. Standard file names using this system begin with the prefix
+
Since {{OOo}} is a platform-independent application, it uses URL notation (which is independent of any operating system), as defined in the Internet Standard RFC 1738 for file names. Standard file names using this system begin with the prefix <tt>file:///</tt> followed by the local path. If the file name contains sub-directories, then these are separated by a single forward slash, not with a backslash usually used under Windows. The following path references the <tt>test.odt</tt> file in the doc directory on the C: drive.
 
+
<tt>file:///</tt>
+
 
+
followed by the local path. If the file name contains sub-directories, then these are separated by a single '''forward''' slash, not with a backslash usually used under Windows. The following path references the <tt>test.sxw</tt> file in the doc directory on the C: drive.
+
  
<tt>file:///C:/doc/test.sxw</tt>
+
file:///C:/doc/test.odt
  
To covert local file names into an URL, {{OOo}} provides the <tt>ConvertToUrl</tt> function. To convert a URL into a local file name, {{OOo}} provides the <tt>ConvertFromUrl</tt> function:  
+
To convert local file names into an URL, {{OOo}} provides the <tt>ConvertToUrl</tt> function. To convert a URL into a local file name, {{OOo}} provides the <tt>ConvertFromUrl</tt> function:  
  
MsgBox ConvertToUrl("C:\doc\test.sxw")  
+
<source lang="oobas">
  ' supplies file:///C:/doc/test.sxw
+
MsgBox ConvertToUrl("C:\doc\test.odt")  
MsgBox ConvertFromUrl("file:///C:/doc/test.sxw")     
+
  ' supplies file:///C:/doc/test.odt
  '  supplies (under Windows) c:\doc\test.sxw
+
MsgBox ConvertFromUrl("file:///C:/doc/test.odt")     
 +
  '  supplies (under Windows) c:\doc\test.odt
 +
</source>
  
 
The example converts a local file name into a URL and displays it in a message box. It then converts a URL into a local file name and also displays this.  
 
The example converts a local file name into a URL and displays it in a message box. It then converts a URL into a local file name and also displays this.  
  
The Internet Standard RFC 1738, upon which this is based, permits use of the <tt>0-9</tt>, <tt>a-z</tt>, and <tt>A-Z</tt> characters. All other characters are inserted as escape coding in the URLs. To do this, they are converted into their hexadecimal value in the ISO 8859-1 (ISO-Latin) set of characters and are preceded by a percent sign. A space in a local file name therefore, for example, becomes a <tt>%20</tt> in the URL.
+
The Internet Standard RFC 1738, upon which this is based, permits use of the <tt>0-9</tt>, <tt>a-z</tt>, and <tt>A-Z</tt> characters. All other characters are inserted as escape coding in the URLs. To do this, they are converted into their hexadecimal value in the UTF-8 set of characters and are preceded by a percent sign. A space in a local file name therefore, for example, becomes a <tt>%20</tt> in the URL.
  
 
=== XML File Format ===
 
=== XML File Format ===
  
Since Version 1, {{OOo}} provides an XML-based file format. Through the use of XML, the user has the option of also opening and editing files in other programs.  
+
{{OOo}} documents are based on the XML file format. XML-based files can be opened and edited with other programs.
  
 
=== Compression of Files ===
 
=== Compression of Files ===
  
Since XML is based on standard text files, the resultant files are usually very large. {{OOo}} therefore compresses the files and saves them as a ZIP file. By means of a <tt>storeAsURL</tt> method option, the user can save the original XML files directly. See [html/p18.html storeAsURL][html/p18.html  Method Options].
+
Since XML is based on standard text files, the resultant files are usually very large. {{OOo}} therefore compresses the files and saves them as a ZIP file. By means of a <tt>storeAsURL</tt> method option, the user can save the original XML files directly. See [[Documentation/BASIC Guide/StarDesktop#storeAsURL Method Options|storeAsURL Method Options]], below.
  
 
== Creating, Opening and Importing Documents ==
 
== Creating, Opening and Importing Documents ==
Line 64: Line 61:
 
Documents are opened, imported and created using the method  
 
Documents are opened, imported and created using the method  
  
StarDesktop.loadComponentFromURL(URL, Frame, SearchFlags, FileProperties)
+
<source lang="oobas">
 +
StarDesktop.loadComponentFromURL(URL, Frame, SearchFlags, FileProperties)
 +
</source>
  
 
The first parameter of <tt>loadComponentFromURL</tt> specifies the URL of the associated file.  
 
The first parameter of <tt>loadComponentFromURL</tt> specifies the URL of the associated file.  
  
As the second parameter, <tt>loadComponentFromURL</tt> expects a name for the frame object of the window that {{OOo}} creates internally for its administration. The predefined <tt>_blank</tt> name is usually specified here, and this ensures that {{OOo}} creates a new window. Alternatively, <tt>_hidden</tt> can also be specified, and this ensures that the corresponding document is loaded but remains invisible.  
+
As the second parameter, <tt>loadComponentFromURL</tt> expects a name for the frame object of the window that {{OOo}} creates internally for its administration. The predefined <tt>_blank</tt> name is usually specified here, and this ensures that {{OOo}} creates a new window.
  
 
Using these parameters, the user can open a {{OOo}} document, since place holders (dummy values) can be assigned to the last two parameters:
 
Using these parameters, the user can open a {{OOo}} document, since place holders (dummy values) can be assigned to the last two parameters:
  
Dim Doc As Object
+
<source lang="oobas">
Dim Url As String
+
Dim Doc As Object
Dim Dummy()  
+
Dim Url As String
+
Dim Dummy() 'An (empty) array of PropertyValues
Url = "file:///C:/test.sxw"
+
 
+
Url = "file:///C:/test.odt"
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy())
+
 
 +
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy)
 +
</source>
  
The preceding call opens the <tt>text.sxw</tt> file and displays this in a new window.  
+
The preceding call opens the <tt>test.odt</tt> file and displays this in a new window.  
  
 
Any number of documents can be opened in this way in {{OOo}} Basic and then edited using the returned document objects.
 
Any number of documents can be opened in this way in {{OOo}} Basic and then edited using the returned document objects.
  
{{Documentation/Note|<tt>StarDesktop.loadComponentFromURL</tt> supersedes the <tt>Documents.Add</tt> and <tt>Documents.Open</tt> methods from the old {{OOo}} API.}}
+
{{Documentation/SO5note|<tt>StarDesktop.loadComponentFromURL</tt> supersedes the <tt>Documents.Add</tt> and <tt>Documents.Open</tt> methods from the old {{OOo}} API.}}
  
 
=== Replacing the Content of the Document Window ===
 
=== Replacing the Content of the Document Window ===
  
The named <tt>_blank</tt> and <tt>_hidden</tt> values for the <tt>Frame</tt> parameter ensure that {{OOo}} creates a new window for every call from <tt>loadComponentFromURL</tt>. In some situations, it is useful to replace the content of an existing window. In this case, the frame object of the window should contain an explicit name. Note that this name must not begin with an underscore. Furthermore, the <tt>SearchFlags</tt> parameter must be set so that the corresponding framework is created, if it does not already exist. The corresponding constant for <tt>SearchFlags</tt> is:
+
The named <tt>_blank</tt> value for the <tt>Frame</tt> parameter ensures that {{OOo}} creates a new window for every call from <tt>loadComponentFromURL</tt>. In some situations, it is useful to replace the content of an existing window. In this case, the frame object of the window should contain an explicit name. Note that this name must not begin with an underscore. Furthermore, the <tt>SearchFlags</tt> parameter must be set so that the corresponding framework is created, if it does not already exist. The corresponding constant for <tt>SearchFlags</tt> is:
  
SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + _
+
<source lang="oobas">
              com.sun.star.frame.FrameSearchFlag.ALL
+
SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + _
 +
              com.sun.star.frame.FrameSearchFlag.ALL
 +
</source>
  
 
The following example shows how the content of an opened window can be replaced with the help of the frame parameter and <tt>SearchFlags</tt>:  
 
The following example shows how the content of an opened window can be replaced with the help of the frame parameter and <tt>SearchFlags</tt>:  
  
Dim Doc As Object
+
<source lang="oobas">
Dim Dummy()  
+
Dim Doc As Object
Dim Url As String
+
Dim Dummy()  
Dim SearchFlags As Long
+
Dim Url As String
 
+
Dim SearchFlags As Long
SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + _
+
              com.sun.star.frame.FrameSearchFlag.ALL
+
Url = "file:///C:/test.sxw"
+
Doc = StarDesktop.loadComponentFromURL(Url, "MyFrame", SearchFlags, Dummy)
+
MsgBox "Press OK to display the second document."
+
 
   
 
   
Url = "file:///C:/test2.sxw"
+
SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + _
Doc = StarDesktop.loadComponentFromURL(Url, "MyFrame", _
+
              com.sun.star.frame.FrameSearchFlag.ALL
      SearchFlags, Dummy)
+
Url = "file:///C:/test.odt"
 +
Doc = StarDesktop.loadComponentFromURL(Url, "MyFrame", SearchFlags, Dummy)
 +
MsgBox "Press OK to display the second document."
  
The example first opens the <tt>test.sxw</tt> file in a new window with the frame name of <tt>MyFrame</tt>. Once the message box has been confirmed, it replaces the content of the window with the <tt>test2.sxw</tt> file.
+
Url = "file:///C:/test2.odt"
 +
Doc = StarDesktop.loadComponentFromURL(Url, "MyFrame", _
 +
      SearchFlags, Dummy)
 +
</source>
 +
 
 +
The example first opens the <tt>test.odt</tt> file in a new window with the frame name of <tt>MyFrame</tt>. Once the message box has been confirmed, it replaces the content of the window with the <tt>test2.odt</tt> file.
  
 
=== loadComponentFromURL Method Options ===
 
=== loadComponentFromURL Method Options ===
Line 121: Line 126:
 
;<tt>CharacterSet (String)</tt>:defines which set of characters a document is based on.
 
;<tt>CharacterSet (String)</tt>:defines which set of characters a document is based on.
 
;<tt>FilterName (String)</tt>:specifies a special filter for the <tt>loadComponentFromURL</tt> function. The filter names available are defined in the <tt>\share\config\registry\instance\org\openoffice\office\TypeDetection.xml</tt> file.
 
;<tt>FilterName (String)</tt>:specifies a special filter for the <tt>loadComponentFromURL</tt> function. The filter names available are defined in the <tt>\share\config\registry\instance\org\openoffice\office\TypeDetection.xml</tt> file.
;<tt>FilterOptions (String)</tt>:defines additional options for filters.
+
;<tt>FilterData (String)</tt>:defines additional options for filters.
 +
;<tt>FilterOptions (String)</tt>:defines additional options (used by old filters).
 +
;<tt>Hidden (Boolean)</tt>:value true loads the document in invisible mode.
 
;<tt>JumpMark (String)</tt>:once a document has been opened, jumps to the position defined in JumpMark.
 
;<tt>JumpMark (String)</tt>:once a document has been opened, jumps to the position defined in JumpMark.
 +
;<tt>MacroExecutionMode (Integer)</tt>:indicates if document macros may be executed. Values : see <idl>com.sun.star.document.MacroExecMode</idl>
 
;<tt>Password (String)</tt>:transfers a password for a protected file.
 
;<tt>Password (String)</tt>:transfers a password for a protected file.
;<tt>ReadOnly (Boolean)</tt>:loads a read-only document.
+
;<tt>ReadOnly (Boolean)</tt>:value true loads a document in read-only mode.
 +
;<tt>UpdateDocMode (Integer)</tt>:indicates how/if links will be updated. Values : see <idl>com.sun.star.document.UpdateDocMode</idl>
  
 
The following example shows how a text file separated by a comma in {{OOo}} Calc can be opened using the <tt>FilterName</tt> option.  
 
The following example shows how a text file separated by a comma in {{OOo}} Calc can be opened using the <tt>FilterName</tt> option.  
  
Dim Doc As Object
+
<source lang="oobas">
Dim FileProperties(0) As New com.sun.star.beans.PropertyValue
+
Dim Doc As Object
Dim Url As String
+
Dim FileProperties(1) As New com.sun.star.beans.PropertyValue
+
Dim Url As String
Url = "file:///C:/csv.doc"
+
FileProperties(0).Name = "FilterName"
+
FileProperties(0).Value ="scalc: Text - txt - csv ({{OOo}} Calc)"
+
+
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, FileProperties())
+
  
The <tt>FileProperties</tt> data field covers precisely one value because it records one option. The <tt>Filtername</tt> property defines whether {{OOo}} uses a {{OOo}} Calc text filter to open files.
+
Url = "file:///C:/doc.csv"
 +
FileProperties(0).Name = "FilterName"
 +
FileProperties(0).Value ="Text - txt - csv (StarCalc)"
 +
FileProperties(1).Name = "FilterOptions"
 +
FileProperties(1).value  = "44,34,0,1"
 +
 
 +
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, FileProperties())
 +
</source>
 +
 
 +
The <tt>FileProperties</tt> array has two elements, one for each option used. The <tt>Filtername</tt> property defines whether {{OOo}} uses a {{OOo}} Calc text filter to open files. The <tt>FilterOptions</tt> property contains the description of the syntax of the csv file.
  
 
=== Creating New Documents ===
 
=== Creating New Documents ===
Line 146: Line 159:
 
Alternatively, if only an empty document without any adaptation is needed, a <tt>private:factory</tt> URL can be specified:
 
Alternatively, if only an empty document without any adaptation is needed, a <tt>private:factory</tt> URL can be specified:
  
Dim Dummy()  
+
<source lang="oobas">
Dim Url As String
+
Dim Dummy()  
Dim Doc As Object
+
Dim Url As String
+
Dim Doc As Object
Url = "private:factory/swriter"
+
 
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy())
+
Url = "private:factory/swriter"
 +
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy())
 +
</source>
  
 
The call creates an empty {{OOo}} writer document.
 
The call creates an empty {{OOo}} writer document.
Line 157: Line 172:
 
== Document Objects ==
 
== Document Objects ==
  
The <tt>loadComponentFromURL</tt> function introduced in the previous section returns a document object. This supports the <tt>com.sun.star.document.OfficeDocument</tt> service, which in turn provides two central interfaces:  
+
The <tt>loadComponentFromURL</tt> function introduced in the previous section returns a document object. This supports the <idl>com.sun.star.document.OfficeDocument</idl> service, which in turn provides two central interfaces:  
 
+
* the <tt>com.sun.star.frame.XStorable</tt> interface, which is responsible for saving documents and
+
* the <tt>com.sun.star.view.XPrintable</tt> interface, which contains the methods for printing documents.
+
  
{{Documentation/Note|When changing over to {{OOo}} 2.x, you will find that the functional scope of the document objects has remained the same for the most part. The document objects, for example, still provide methods for saving and printing documents. The names and parameters of the methods have, however, changed.}}
+
* The <idl>com.sun.star.frame.XStorable</idl> interface, which is responsible for saving documents.
 +
* The <idl>com.sun.star.view.XPrintable</idl> interface, which contains the methods for printing documents.  
  
 
=== Saving and Exporting Documents ===
 
=== Saving and Exporting Documents ===
  
{{OOo}} documents are saved directly through the document object. The <tt>store</tt> method of the <tt>com.sun.star.frame.XStorable</tt> interface is available for this purpose:
+
{{OOo}} documents are saved directly through the document object. The <tt>store</tt> method of the <idl>com.sun.star.frame.XStorable</idl> interface is available for this purpose:
  
Doc.store()
+
<source lang="oobas">
 +
Doc.store()
 +
</source>
  
This call functions provided that the document has already been assigned a memory space. This is not the case for new documents. In this instance, the <tt>storeAsURL</tt> method is used. This method is also defined in <tt>com.sun.star.frame.XStorable</tt> and can be used to define the location of the document:  
+
This call functions provided that the document has already been assigned a memory space. This is not the case for new documents. In this instance, the <tt>storeAsURL</tt> method is used. This method is also defined in <idl>com.sun.star.frame.XStorable</idl> and can be used to define the location of the document:  
  
Dim URL As String
+
<source lang="oobas">
Dim Dummy()
+
Dim URL As String
 +
Dim Dummy()
  
Url = "file:///C:/test3.sxw"
+
Url = "file:///C:/test3.odt"
Doc.storeAsURL(URL, Dummy())
+
Doc.storeAsURL(URL, Dummy())
 +
</source>
  
In addition to the preceding methods, <tt>com.sun.star.frame.XStorable</tt> also provides some help methods which are useful when saving documents. These are:
+
In addition to the preceding methods, <idl>com.sun.star.frame.XStorable</idl> also provides some help methods which are useful when saving documents. These are:
  
 
;<tt>hasLocation()</tt>:specifies whether the document has already been assigned a URL.
 
;<tt>hasLocation()</tt>:specifies whether the document has already been assigned a URL.
Line 186: Line 203:
 
The code for saving a document can be extended by these options so that the document is only saved if the object has actually been modified and the file name is only queried if it is actually needed:
 
The code for saving a document can be extended by these options so that the document is only saved if the object has actually been modified and the file name is only queried if it is actually needed:
  
If (Doc.isModified) Then
+
<source lang="oobas">
  If (Doc.hasLocation And (Not Doc.isReadOnly)) Then
+
If (Doc.isModified) Then
    Doc.store()
+
  If (Doc.hasLocation And (Not Doc.isReadOnly)) Then
  Else
+
    Doc.store()
    Doc.storeAsURL(URL, Dummy())
+
  Else
  End If
+
    Doc.storeAsURL(URL, Dummy())
End If
+
  End If
 +
End If
 +
</source>
  
 
The example first checks whether the relevant document has been modified since it was last saved. It only continues with the saving process if this is the case. If the document has already been assigned a URL and is not a read-only document, it is saved under the existing URL. If it does not have a URL or was opened in its read-only status, it is saved under a new URL.
 
The example first checks whether the relevant document has been modified since it was last saved. It only continues with the saving process if this is the case. If the document has already been assigned a URL and is not a read-only document, it is saved under the existing URL. If it does not have a URL or was opened in its read-only status, it is saved under a new URL.
Line 202: Line 221:
 
;<tt>CharacterSet (String)</tt>:defines which set of characters a document is based on.
 
;<tt>CharacterSet (String)</tt>:defines which set of characters a document is based on.
 
;<tt>FilterName (String)</tt>:specifies a special filter for the <tt>loadComponentFromURL</tt> function. The filter names available are defined in the <tt>\share\config\registry\instance\org\openoffice\office\TypeDetection.xml</tt> file.
 
;<tt>FilterName (String)</tt>:specifies a special filter for the <tt>loadComponentFromURL</tt> function. The filter names available are defined in the <tt>\share\config\registry\instance\org\openoffice\office\TypeDetection.xml</tt> file.
;<tt>FilterOptions (String)</tt>:defines additional options for filters.
+
;<tt>FilterData (String)</tt>:defines additional options for filters.
 +
;<tt>FilterOptions (String)</tt>:defines additional options (used by old filters).
 
;<tt>Overwrite (Boolean)</tt>:allows a file which already exists to be overwritten without a query.
 
;<tt>Overwrite (Boolean)</tt>:allows a file which already exists to be overwritten without a query.
 
;<tt>Password (String)</tt>:transfers the password for a protected file.
 
;<tt>Password (String)</tt>:transfers the password for a protected file.
 
;<tt>Unpacked (Boolean)</tt>:saves the document (not compressed) in sub-directories.
 
;<tt>Unpacked (Boolean)</tt>:saves the document (not compressed) in sub-directories.
 +
:The possibility to store documents in unpacked way is not currently supported, the "Unpacked" property is just ignored, see {{Bug|64364}}.
  
 
The following example shows how the <tt>Overwrite</tt> option can be used in conjunction with <tt>storeAsURL</tt>:
 
The following example shows how the <tt>Overwrite</tt> option can be used in conjunction with <tt>storeAsURL</tt>:
  
Dim Doc As Object
+
<source lang="oobas">
Dim FileProperties(0) As New com.sun.star.beans.PropertyValue
+
Dim Doc As Object
Dim Url As String
+
Dim FileProperties(0) As New com.sun.star.beans.PropertyValue
' ... Initialize Doc  
+
Dim Url As String
 +
' ... Initialize Doc  
  
Url = "file:///c:/test3.sxw"
+
Url = "file:///c:/test3.odt"
FileProperties(0).Name = "Overwrite"
+
FileProperties(0).Name = "Overwrite"
FileProperties(0).Value = True
+
FileProperties(0).Value = True
Doc.storeAsURL(sUrl, mFileProperties())
+
Doc.storeAsURL(Url, FileProperties())
 +
</source>
  
 
The example then saves <tt>Doc</tt> under the specified file name if a file already exists under the name.
 
The example then saves <tt>Doc</tt> under the specified file name if a file already exists under the name.
Line 223: Line 246:
 
=== Printing Documents ===
 
=== Printing Documents ===
  
Similar to saving, documents are printed out directly by means of the document object. The <tt>Print</tt> method of the <tt>com.sun.star.view.Xprintable</tt> interface is provided for this purpose. In its simplest form, the print call is:
+
Similar to saving, documents are printed out directly by means of the document object. The <tt>Print</tt> method of the <idl>com.sun.star.view.Xprintable</idl> interface is provided for this purpose. In its simplest form, the print call is:
  
Dim Dummy()
+
<source lang="oobas">
 +
Dim Dummy()
  
Doc.print(Dummy())
+
Doc.print(Dummy())
 +
</source>
  
 
As in the case of the <tt>loadComponentFromURL</tt> method, the Dummy parameter is a <tt>PropertyValue</tt> data field through which {{OOo}} can specify several options for printing.
 
As in the case of the <tt>loadComponentFromURL</tt> method, the Dummy parameter is a <tt>PropertyValue</tt> data field through which {{OOo}} can specify several options for printing.
Line 240: Line 265:
 
;<tt>Sort (Boolean)</tt>:sorts the pages when printing out several copies (<tt>CopyCount</tt> > 1).
 
;<tt>Sort (Boolean)</tt>:sorts the pages when printing out several copies (<tt>CopyCount</tt> > 1).
 
;<tt>Pages (String)</tt>:contains the list of the pages to be printed (syntax as specified in print dialog).
 
;<tt>Pages (String)</tt>:contains the list of the pages to be printed (syntax as specified in print dialog).
 +
;<tt>Wait (Boolean)</tt>:if set to True the print method will return ''after'' the job is stored on the waiting list for the printer. Use this option if you want to close the document after print.
  
 
The following example shows how several pages of a document can be printed out using the <tt>Pages</tt> option:  
 
The following example shows how several pages of a document can be printed out using the <tt>Pages</tt> option:  
  
Dim Doc As Object
+
<source lang="oobas">
Dim PrintProperties(0) As New com.sun.star.beans.PropertyValue
+
Dim Doc As Object
 +
Dim PrintProperties(1) As New com.sun.star.beans.PropertyValue
  
PrintProperties(0).Name="Pages"
+
PrintProperties(0).Name="Pages"
PrintProperties(0).Value="1-3; 7; 9"
+
PrintProperties(0).Value="1-3; 7; 9"
Doc.print(PrintProperties())
+
PrintProperties(1).Name="Wait"
 +
PrintProperties(1).Value=True
 +
Doc.print(PrintProperties())
 +
</source>
  
 
=== Printer selection and settings ===
 
=== Printer selection and settings ===
  
The <tt>com.sun.star.view.XPrintable</tt> interface provides the <tt>Printer</tt> property, which selects the printer. This property receives a <tt>PropertyValue</tt> data field with the following settings:  
+
The <idl>com.sun.star.view.XPrintable</idl> interface provides the <tt>Printer</tt> property, which selects the printer. This property receives a <tt>PropertyValue</tt> data field with the following settings:  
  
 
;<tt>Name (String)</tt>:specifies the name of printer.
 
;<tt>Name (String)</tt>:specifies the name of printer.
;<tt>PaperOrientation (Enum)</tt>:specifies the paper orientation (<tt>com.sun.star.view.PaperOrientation.PORTRAIT</tt> value for portrait format, <tt>com.sun.star.view.PaperOrientation.LANDSCAPE</tt> for landscape format).
+
;<tt>PaperOrientation (Enum)</tt>:specifies the paper orientation (<idl>com.sun.star.view.PaperOrientation.PORTRAIT</idl> value for portrait format, <idl>com.sun.star.view.PaperOrientation.LANDSCAPE</idl> for landscape format).
;<tt>PaperFormat (Enum)</tt>:specifies the paper format (for example, <tt>com.sun.star.view.PaperFormat.A4</tt> for DIN A4 or <tt>com.sun.star.view.PaperFormat.Letter</tt> for US letters).
+
;<tt>PaperFormat (Enum)</tt>:specifies the paper format (for example, <idl>com.sun.star.view.PaperFormat.A4</idl> for DIN A4 or <idl>com.sun.star.view.PaperFormat.Letter</idl> for US letters).
 
;<tt>PaperSize (Size)</tt>:specifies the paper size in hundredths of a millimeter.
 
;<tt>PaperSize (Size)</tt>:specifies the paper size in hundredths of a millimeter.
  
 
The following example shows how a printer can be changed and the paper size set with the help of the <tt>Printer</tt> property.  
 
The following example shows how a printer can be changed and the paper size set with the help of the <tt>Printer</tt> property.  
  
Dim Doc As Object
+
<source lang="oobas">
Dim PrinterProperties(1) As New com.sun.star.beans.PropertyValue
+
Dim Doc As Object
Dim PaperSize As New com.sun.star.awt.Size
+
Dim PrinterProperties(1) As New com.sun.star.beans.PropertyValue
+
Dim PaperSize As New com.sun.star.awt.Size
PaperSize.Width = 20000  ' corresponds to 20 cm
+
PaperSize.Height = 20000  ' corresponds to 20 cm
+
PrinterProperties (0).Name="Name"
+
PrinterProperties (0).Value="My HP Laserjet"
+
PrinterProperties (1).Name="PaperSize"
+
PrinterProperties (1).Value=PaperSize
+
Doc.Printer = PrinterProperties()
+
  
The example defines an object named <tt>PaperSize</tt> with the <tt>com.sun.star.awt.Size</tt> type. This is needed to specify the paper size. Furthermore, it creates a data field for two <tt>PropertyValue</tt> entries named <tt>PrinterProperties</tt>. This data field is then initialized with the values to be set and assigned the <tt>Printer</tt> property. From the standpoint of UNO, the printer is not a real property but an imitated one.
+
PaperSize.Width = 20000  ' corresponds to 20 cm
 +
PaperSize.Height = 20000  ' corresponds to 20 cm
 +
PrinterProperties (0).Name="Name"
 +
PrinterProperties (0).Value="My HP Laserjet"
 +
PrinterProperties (1).Name="PaperSize"
 +
PrinterProperties (1).Value=PaperSize
 +
Doc.Printer = PrinterProperties()
 +
</source>
  
 +
The example defines an object named <tt>PaperSize</tt> with the <idl>com.sun.star.awt.Size</idl> type. This is needed to specify the paper size. Furthermore, it creates a data field for two <tt>PropertyValue</tt> entries named <tt>PrinterProperties</tt>. This data field is then initialized with the values to be set and assigned the <tt>Printer</tt> property. From the standpoint of UNO, the printer is not a real property but an imitated one.
 +
 +
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/StarDesktop}}
 
{{PDL1}}
 
{{PDL1}}

Revision as of 08:42, 13 September 2010


When working with documents, two services are used most frequently:

  • The com.sun.star.frame.Desktop service, which is similar to the core service of Apache OpenOffice. It provides the functions for the frame object of Apache OpenOffice, under which all document windows are classified. Documents can also be created, opened and imported using this service.
  • The basic functionality for the individual document objects is provided by the com.sun.star.document.OfficeDocument service. This provides the methods for saving, exporting and printing documents.

The com.sun.star.frame.Desktop service is created automatically when Apache OpenOffice is started. This service can be addressed in Apache OpenOffice Basic using the global name StarDesktop.

The most important interface of the StarDesktop is com.sun.star.frame.XComponentLoader. This basically covers the loadComponentFromURL method, which is responsible for creating, importing, and opening documents.

Documentation note.png StarOffice 5 : The name of the StarDesktop object dates back to StarOffice 5, in which all document windows were embedded in one common application called StarDesktop. In the present version of Apache OpenOffice, a visible StarDesktop is no longer used. The name StarDesktop was, however, retained for the frame object of Apache OpenOffice because it clearly indicates that this is a basic object for the entire application.

The StarDesktop object replaces the Application object of StarOffice 5 which previously applied as a root object. However, unlike the old Application object, StarDesktop is primarily responsible for opening new documents. The functions resident in the old Application object for controlling the on-screen depiction of Apache OpenOffice (for example, FullScreen, FunctionBarVisible, Height, Width, Top, Visible) are no longer used.


Documentation note.png VBA : Whereas the active document in Word is accessed through Application.ActiveDocument and in Excel through Application.ActiveWorkbook, in Apache OpenOffice, the StarDesktop is responsible for this task. The active document object is accessed in Apache OpenOffice through the StarDesktop.CurrentComponent property, or through ThisComponent.


ThisComponent

The global name ThisComponent generally returns the same object as StarDesktop.CurrentComponent, with one significant advantage. If you are running from within the Basic IDE, debugging or exploring, then StarDesktop returns the Basic IDE itself. This is probably not what you want. ThisComponent returns the last previously active document.

Basic Information about Documents in Apache OpenOffice

When working with Apache OpenOffice documents, it is useful to deal with some of the basic issues of document administration in Apache OpenOffice. This includes the way in which file names are structured for Apache OpenOffice documents, as well as the format in which files are saved.

File Names in URL Notation

Since Apache OpenOffice is a platform-independent application, it uses URL notation (which is independent of any operating system), as defined in the Internet Standard RFC 1738 for file names. Standard file names using this system begin with the prefix file:/// followed by the local path. If the file name contains sub-directories, then these are separated by a single forward slash, not with a backslash usually used under Windows. The following path references the test.odt file in the doc directory on the C: drive.

file:///C:/doc/test.odt

To convert local file names into an URL, Apache OpenOffice provides the ConvertToUrl function. To convert a URL into a local file name, Apache OpenOffice provides the ConvertFromUrl function:

MsgBox ConvertToUrl("C:\doc\test.odt") 
  ' supplies file:///C:/doc/test.odt
MsgBox ConvertFromUrl("file:///C:/doc/test.odt")    
  '  supplies (under Windows) c:\doc\test.odt

The example converts a local file name into a URL and displays it in a message box. It then converts a URL into a local file name and also displays this.

The Internet Standard RFC 1738, upon which this is based, permits use of the 0-9, a-z, and A-Z characters. All other characters are inserted as escape coding in the URLs. To do this, they are converted into their hexadecimal value in the UTF-8 set of characters and are preceded by a percent sign. A space in a local file name therefore, for example, becomes a %20 in the URL.

XML File Format

Apache OpenOffice documents are based on the XML file format. XML-based files can be opened and edited with other programs.

Compression of Files

Since XML is based on standard text files, the resultant files are usually very large. Apache OpenOffice therefore compresses the files and saves them as a ZIP file. By means of a storeAsURL method option, the user can save the original XML files directly. See storeAsURL Method Options, below.

Creating, Opening and Importing Documents

Documents are opened, imported and created using the method

StarDesktop.loadComponentFromURL(URL, Frame, SearchFlags, FileProperties)

The first parameter of loadComponentFromURL specifies the URL of the associated file.

As the second parameter, loadComponentFromURL expects a name for the frame object of the window that Apache OpenOffice creates internally for its administration. The predefined _blank name is usually specified here, and this ensures that Apache OpenOffice creates a new window.

Using these parameters, the user can open a Apache OpenOffice document, since place holders (dummy values) can be assigned to the last two parameters:

Dim Doc As Object
Dim Url As String
Dim Dummy() 'An (empty) array of PropertyValues
 
Url = "file:///C:/test.odt"
 
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy)

The preceding call opens the test.odt file and displays this in a new window.

Any number of documents can be opened in this way in Apache OpenOffice Basic and then edited using the returned document objects.

Documentation note.png StarOffice 5 : StarDesktop.loadComponentFromURL supersedes the Documents.Add and Documents.Open methods from the old Apache OpenOffice API.


Replacing the Content of the Document Window

The named _blank value for the Frame parameter ensures that Apache OpenOffice creates a new window for every call from loadComponentFromURL. In some situations, it is useful to replace the content of an existing window. In this case, the frame object of the window should contain an explicit name. Note that this name must not begin with an underscore. Furthermore, the SearchFlags parameter must be set so that the corresponding framework is created, if it does not already exist. The corresponding constant for SearchFlags is:

SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + _
              com.sun.star.frame.FrameSearchFlag.ALL

The following example shows how the content of an opened window can be replaced with the help of the frame parameter and SearchFlags:

Dim Doc As Object
Dim Dummy() 
Dim Url As String
Dim SearchFlags As Long
 
SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + _
              com.sun.star.frame.FrameSearchFlag.ALL
Url = "file:///C:/test.odt"
Doc = StarDesktop.loadComponentFromURL(Url, "MyFrame", SearchFlags, Dummy)
MsgBox "Press OK to display the second document."
 
Url = "file:///C:/test2.odt"
Doc = StarDesktop.loadComponentFromURL(Url, "MyFrame", _
      SearchFlags, Dummy)

The example first opens the test.odt file in a new window with the frame name of MyFrame. Once the message box has been confirmed, it replaces the content of the window with the test2.odt file.

loadComponentFromURL Method Options

The fourth parameter of the loadComponentFromURL function is a PropertyValue data field. which provides Apache OpenOffice with various options for opening and creating documents. The data field must provide a PropertyValue structure for each option in which the name of the option is saved as a string as well as the associated value.

loadComponentFromURL supports the following options:

AsTemplate (Boolean)
if true, loads a new, untitled document from the given URL. If is false, template files are loaded for editing.
CharacterSet (String)
defines which set of characters a document is based on.
FilterName (String)
specifies a special filter for the loadComponentFromURL function. The filter names available are defined in the \share\config\registry\instance\org\openoffice\office\TypeDetection.xml file.
FilterData (String)
defines additional options for filters.
FilterOptions (String)
defines additional options (used by old filters).
Hidden (Boolean)
value true loads the document in invisible mode.
JumpMark (String)
once a document has been opened, jumps to the position defined in JumpMark.
MacroExecutionMode (Integer)
indicates if document macros may be executed. Values : see com.sun.star.document.MacroExecMode
Password (String)
transfers a password for a protected file.
ReadOnly (Boolean)
value true loads a document in read-only mode.
UpdateDocMode (Integer)
indicates how/if links will be updated. Values : see com.sun.star.document.UpdateDocMode

The following example shows how a text file separated by a comma in Apache OpenOffice Calc can be opened using the FilterName option.

Dim Doc As Object
Dim FileProperties(1) As New com.sun.star.beans.PropertyValue
Dim Url As String
 
Url = "file:///C:/doc.csv"
FileProperties(0).Name = "FilterName"
FileProperties(0).Value ="Text - txt - csv (StarCalc)"
FileProperties(1).Name = "FilterOptions"
FileProperties(1).value  = "44,34,0,1"
 
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, FileProperties())

The FileProperties array has two elements, one for each option used. The Filtername property defines whether Apache OpenOffice uses a Apache OpenOffice Calc text filter to open files. The FilterOptions property contains the description of the syntax of the csv file.

Creating New Documents

Apache OpenOffice automatically creates a new document if the document specified in the URL is a template.

Alternatively, if only an empty document without any adaptation is needed, a private:factory URL can be specified:

Dim Dummy() 
Dim Url As String
Dim Doc As Object
 
Url = "private:factory/swriter"
Doc = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy())

The call creates an empty Apache OpenOffice writer document.

Document Objects

The loadComponentFromURL function introduced in the previous section returns a document object. This supports the com.sun.star.document.OfficeDocument service, which in turn provides two central interfaces:

Saving and Exporting Documents

Apache OpenOffice documents are saved directly through the document object. The store method of the com.sun.star.frame.XStorable interface is available for this purpose:

Doc.store()

This call functions provided that the document has already been assigned a memory space. This is not the case for new documents. In this instance, the storeAsURL method is used. This method is also defined in com.sun.star.frame.XStorable and can be used to define the location of the document:

Dim URL As String
Dim Dummy()
 
Url = "file:///C:/test3.odt"
Doc.storeAsURL(URL, Dummy())

In addition to the preceding methods, com.sun.star.frame.XStorable also provides some help methods which are useful when saving documents. These are:

hasLocation()
specifies whether the document has already been assigned a URL.
isReadonly()
specifies whether a document has read-only protection.
isModified()
specifies whether a document has been modified since it was last saved.

The code for saving a document can be extended by these options so that the document is only saved if the object has actually been modified and the file name is only queried if it is actually needed:

If (Doc.isModified) Then
  If (Doc.hasLocation And (Not Doc.isReadOnly)) Then
    Doc.store()
  Else
    Doc.storeAsURL(URL, Dummy())
  End If
End If

The example first checks whether the relevant document has been modified since it was last saved. It only continues with the saving process if this is the case. If the document has already been assigned a URL and is not a read-only document, it is saved under the existing URL. If it does not have a URL or was opened in its read-only status, it is saved under a new URL.

storeAsURL Method Options

As with the loadComponentFromURL method, some options can also be specified in the form of a PropertyValue data field using the storeAsURL method. These determine the procedure Apache OpenOffice uses when saving a document. storeAsURL provides the following options:

CharacterSet (String)
defines which set of characters a document is based on.
FilterName (String)
specifies a special filter for the loadComponentFromURL function. The filter names available are defined in the \share\config\registry\instance\org\openoffice\office\TypeDetection.xml file.
FilterData (String)
defines additional options for filters.
FilterOptions (String)
defines additional options (used by old filters).
Overwrite (Boolean)
allows a file which already exists to be overwritten without a query.
Password (String)
transfers the password for a protected file.
Unpacked (Boolean)
saves the document (not compressed) in sub-directories.
The possibility to store documents in unpacked way is not currently supported, the "Unpacked" property is just ignored, see Issue 64364 .

The following example shows how the Overwrite option can be used in conjunction with storeAsURL:

Dim Doc As Object
Dim FileProperties(0) As New com.sun.star.beans.PropertyValue
Dim Url As String
' ... Initialize Doc 
 
Url = "file:///c:/test3.odt"
FileProperties(0).Name = "Overwrite"
FileProperties(0).Value = True
Doc.storeAsURL(Url, FileProperties())

The example then saves Doc under the specified file name if a file already exists under the name.

Printing Documents

Similar to saving, documents are printed out directly by means of the document object. The Print method of the com.sun.star.view.Xprintable interface is provided for this purpose. In its simplest form, the print call is:

Dim Dummy()
 
Doc.print(Dummy())

As in the case of the loadComponentFromURL method, the Dummy parameter is a PropertyValue data field through which Apache OpenOffice can specify several options for printing.

The options of the print method

The print method expects a PropertyValue data field as a parameter, which reflects the settings of the print dialog of Apache OpenOffice:

CopyCount (Integer)
specifies the number of copies to be printed.
FileName (String)
prints the document in the specified file.
Collate (Boolean)
advises the printer to collate the pages of the copies.
Sort (Boolean)
sorts the pages when printing out several copies (CopyCount > 1).
Pages (String)
contains the list of the pages to be printed (syntax as specified in print dialog).
Wait (Boolean)
if set to True the print method will return after the job is stored on the waiting list for the printer. Use this option if you want to close the document after print.

The following example shows how several pages of a document can be printed out using the Pages option:

Dim Doc As Object
Dim PrintProperties(1) As New com.sun.star.beans.PropertyValue
 
PrintProperties(0).Name="Pages"
PrintProperties(0).Value="1-3; 7; 9"
PrintProperties(1).Name="Wait"
PrintProperties(1).Value=True
Doc.print(PrintProperties())

Printer selection and settings

The com.sun.star.view.XPrintable interface provides the Printer property, which selects the printer. This property receives a PropertyValue data field with the following settings:

Name (String)
specifies the name of printer.
PaperOrientation (Enum)
specifies the paper orientation (com.sun.star.view.PaperOrientation.PORTRAIT value for portrait format, com.sun.star.view.PaperOrientation.LANDSCAPE for landscape format).
PaperFormat (Enum)
specifies the paper format (for example, com.sun.star.view.PaperFormat.A4 for DIN A4 or com.sun.star.view.PaperFormat.Letter for US letters).
PaperSize (Size)
specifies the paper size in hundredths of a millimeter.

The following example shows how a printer can be changed and the paper size set with the help of the Printer property.

Dim Doc As Object
Dim PrinterProperties(1) As New com.sun.star.beans.PropertyValue
Dim PaperSize As New com.sun.star.awt.Size
 
PaperSize.Width = 20000   ' corresponds to 20 cm
PaperSize.Height = 20000   ' corresponds to 20 cm
PrinterProperties (0).Name="Name"
PrinterProperties (0).Value="My HP Laserjet"
PrinterProperties (1).Name="PaperSize"
PrinterProperties (1).Value=PaperSize
Doc.Printer = PrinterProperties()

The example defines an object named PaperSize with the com.sun.star.awt.Size type. This is needed to specify the paper size. Furthermore, it creates a data field for two PropertyValue entries named PrinterProperties. This data field is then initialized with the values to be set and assigned the Printer property. From the standpoint of UNO, the printer is not a real property but an imitated one.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools