Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / wizards / source / sfdocuments / SF_Document.xba
blob2233aeb5c6500c998a9b02cc2f2dc8480764b327
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_Document" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
4 REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
5 REM === The SFDocuments library is one of the associated libraries. ===
6 REM === Full documentation is available on https://help.libreoffice.org/ ===
7 REM =======================================================================================================================
9 Option Compatible
10 Option ClassModule
12 Option Explicit
14 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
15 &apos;&apos;&apos; SF_Document
16 &apos;&apos;&apos; ===========
17 &apos;&apos;&apos;
18 &apos;&apos;&apos; The SFDocuments library gathers a number of methods and properties making easy
19 &apos;&apos;&apos; managing and manipulating LibreOffice documents
20 &apos;&apos;&apos;
21 &apos;&apos;&apos; Some methods are generic for all types of documents: they are combined in the
22 &apos;&apos;&apos; current SF_Document module
23 &apos;&apos;&apos; - saving, closing documents
24 &apos;&apos;&apos; - accessing their standard or custom properties
25 &apos;&apos;&apos; Specific properties and methods are implemented in the concerned subclass(es) SF_Calc, SF_Base, ...
26 &apos;&apos;&apos;
27 &apos;&apos;&apos; Documents might contain forms. The current service gives access to the &quot;SFDocuments.Form&quot; service
28 &apos;&apos;&apos;
29 &apos;&apos;&apos; To workaround the absence of class inheritance in LibreOffice Basic, some redundancy is necessary
30 &apos;&apos;&apos; Each subclass MUST implement also the generic methods and properties, even if they only call
31 &apos;&apos;&apos; the parent methods and properties implemented below
32 &apos;&apos;&apos; They should also duplicate some generic private members as a subset of their own set of members
33 &apos;&apos;&apos;
34 &apos;&apos;&apos; The current module is closely related to the &quot;UI&quot; and &quot;FileSystem&quot; services
35 &apos;&apos;&apos; of the ScriptForge library
36 &apos;&apos;&apos;
37 &apos;&apos;&apos; Service invocation examples:
38 &apos;&apos;&apos; 1) From the UI service
39 &apos;&apos;&apos; Dim ui As Object, oDoc As Object
40 &apos;&apos;&apos; Set ui = CreateScriptService(&quot;UI&quot;)
41 &apos;&apos;&apos; Set oDoc = ui.GetDocument(&quot;Untitled 1&quot;)
42 &apos;&apos;&apos; &apos; or Set oDoc = ui.CreateDocument(&quot;Calc&quot;, ...)
43 &apos;&apos;&apos; &apos; or Set oDoc = ui.OpenDocument(&quot;C:\Me\MyFile.odt&quot;)
44 &apos;&apos;&apos; 2) Directly if the document is already opened
45 &apos;&apos;&apos; Dim oDoc As Object
46 &apos;&apos;&apos; Set oDoc = CreateScriptService(&quot;SFDocuments.Document&quot;, &quot;Untitled 1&quot;) &apos; Default = ActiveWindow
47 &apos;&apos;&apos; &apos; The substring &quot;SFDocuments.&quot; in the service name is optional
48 &apos;&apos;&apos;
49 &apos;&apos;&apos; Detailed user documentation:
50 &apos;&apos;&apos; https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03/sf_document.html?DbPAR=BASIC
51 &apos;&apos;&apos;
52 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
54 REM ================================================================== EXCEPTIONS
56 Private Const DOCUMENTDEADERROR = &quot;DOCUMENTDEADERROR&quot;
57 Private Const DOCUMENTSAVEERROR = &quot;DOCUMENTSAVEERROR&quot;
58 Private Const DOCUMENTSAVEASERROR = &quot;DOCUMENTSAVEASERROR&quot;
59 Private Const DOCUMENTREADONLYERROR = &quot;DOCUMENTREADONLYERROR&quot;
61 Private Const FORMDEADERROR = &quot;FORMDEADERROR&quot;
63 REM ============================================================= PRIVATE MEMBERS
65 Private [Me] As Object
66 Private [_Parent] As Object
67 Private [_SubClass] As Object &apos; Subclass instance
68 Private ObjectType As String &apos; Must be DOCUMENT
69 Private ServiceName As String
71 &apos; Window description
72 Private _Component As Object &apos; com.sun.star.lang.XComponent
73 Private _Frame As Object &apos; com.sun.star.comp.framework.Frame
74 Private _WindowName As String &apos; Object Name
75 Private _WindowTitle As String &apos; Only mean to identify new documents
76 Private _WindowFileName As String &apos; URL of file name
77 Private _DocumentType As String &apos; Writer, Calc, ...
79 &apos; Properties (work variables - real properties could have been set manually by user)
80 Private _DocumentProperties As Object &apos; Dictionary of document properties
81 Private _CustomProperties As Object &apos; Dictionary of custom properties
83 &apos; Cache for static toolbar descriptions
84 Private _Toolbars As Object &apos; SF_Dictionary instance to hold toolbars stored in application or in document
86 REM ============================================================ MODULE CONSTANTS
88 Const ISDOCFORM = 1 &apos; Form is stored in a Writer document
90 REM ====================================================== CONSTRUCTOR/DESTRUCTOR
92 REM -----------------------------------------------------------------------------
93 Private Sub Class_Initialize()
94 Set [Me] = Nothing
95 Set [_Parent] = Nothing
96 Set [_SubClass] = Nothing
97 ObjectType = &quot;DOCUMENT&quot;
98 ServiceName = &quot;SFDocuments.Document&quot;
99 Set _Component = Nothing
100 Set _Frame = Nothing
101 _WindowName = &quot;&quot;
102 _WindowTitle = &quot;&quot;
103 _WindowFileName = &quot;&quot;
104 _DocumentType = &quot;&quot;
105 Set _DocumentProperties = Nothing
106 Set _CustomProperties = Nothing
107 Set _Toolbars = Nothing
108 End Sub &apos; SFDocuments.SF_Document Constructor
110 REM -----------------------------------------------------------------------------
111 Private Sub Class_Terminate()
112 Call Class_Initialize()
113 End Sub &apos; SFDocuments.SF_Document Destructor
115 REM -----------------------------------------------------------------------------
116 Public Function Dispose() As Variant
117 Call Class_Terminate()
118 Set Dispose = Nothing
119 End Function &apos; SFDocuments.SF_Document Explicit Destructor
121 REM ================================================================== PROPERTIES
123 REM -----------------------------------------------------------------------------
124 Property Get CustomProperties() As Variant
125 &apos;&apos;&apos; Returns a dictionary of all custom properties of the document
126 CustomProperties = _PropertyGet(&quot;CustomProperties&quot;)
127 End Property &apos; SFDocuments.SF_Document.CustomProperties
129 REM -----------------------------------------------------------------------------
130 Property Let CustomProperties(Optional ByVal pvCustomProperties As Variant)
131 &apos;&apos;&apos; Sets the updatable custom properties
132 &apos;&apos;&apos; The argument is a dictionary
134 Dim vPropertyValues As Variant &apos; Array of com.sun.star.beans.PropertyValue
135 Dim vCustomProperties As Variant &apos; Alias of argument
136 Dim oUserdefinedProperties As Object &apos; Custom properties object
137 Dim vOldPropertyValues As Variant &apos; Array of (to remove) existing user defined properties
138 Dim oProperty As Object &apos; Single com.sun.star.beans.PropertyValues
139 Dim sProperty As String &apos; Property name
140 Dim vKeys As Variant &apos; Array of dictionary keys
141 Dim vItems As Variant &apos; Array of dictionary items
142 Dim vValue As Variant &apos; Value to store in property
143 Dim iAttribute As Integer &apos; com.sun.star.beans.PropertyAttribute.REMOVEABLE
144 Dim i As Long
145 Const cstThisSub = &quot;SFDocuments.Document.setCustomProperties&quot;
146 Const cstSubArgs = &quot;CustomProperties&quot;
148 On Local Error GoTo Catch
150 Check:
151 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
152 If Not _IsStillAlive(True) Then GoTo Finally
153 If Not ScriptForge.SF_Utils._Validate(pvCustomProperties, &quot;CustomProperties&quot;, ScriptForge.V_OBJECT, , , &quot;DICTIONARY&quot;) Then GoTo Finally
154 End If
156 Try:
157 Set oUserDefinedProperties = _Component.getDocumentProperties().UserDefinedProperties
159 Set vCustomProperties = pvCustomProperties &apos; To avoid &quot;Object variable not set&quot; error
160 With vCustomProperties
162 &apos; All existing custom properties must first be removed to avoid type conflicts
163 vOldPropertyValues = oUserDefinedProperties.getPropertyValues
164 For Each oProperty In vOldPropertyValues
165 sProperty = oProperty.Name
166 oUserDefinedProperties.removeProperty(sProperty)
167 Next oProperty
169 &apos; Insert new properties one by one after type adjustment (dates, arrays, numbers)
170 vKeys = .Keys
171 vItems = .Items
172 iAttribute = com.sun.star.beans.PropertyAttribute.REMOVEABLE
173 For i = 0 To UBound(vKeys)
174 If VarType(vItems(i)) = V_DATE Then
175 vValue = ScriptForge.SF_Utils._CDateToUnoDate(vItems(i))
176 ElseIf IsArray(vItems(i)) Then
177 vValue = Null
178 ElseIf ScriptForge.SF_Utils._VarTypeExt(vItems(i)) = ScriptForge.V_NUMERIC Then
179 vValue = CreateUnoValue(&quot;double&quot;, vItems(i))
180 Else
181 vValue = vItems(i)
182 End If
183 oUserDefinedProperties.addProperty(vKeys(i), iAttribute, vValue)
184 Next i
186 &apos; Declare the document as changed
187 _Component.setModified(True)
188 End With
190 &apos; Reload custom properties in current object instance
191 _PropertyGet(&quot;CustomProperties&quot;)
193 Finally:
194 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
195 Exit Property
196 Catch:
197 GoTo Finally
198 End Property &apos; SFDocuments.SF_Document.CustomProperties
200 REM -----------------------------------------------------------------------------
201 Property Get Description() As Variant
202 &apos;&apos;&apos; Returns the updatable document property Description
203 Description = _PropertyGet(&quot;Description&quot;)
204 End Property &apos; SFDocuments.SF_Document.Description
206 REM -----------------------------------------------------------------------------
207 Property Let Description(Optional ByVal pvDescription As Variant)
208 &apos;&apos;&apos; Sets the updatable document property Description
209 &apos;&apos;&apos; If multilined, separate lines by &quot;\n&quot; escape sequence or by hard breaks
211 Dim sDescription As String &apos; Alias of pvDescription
212 Const cstThisSub = &quot;SFDocuments.Document.setDescription&quot;
213 Const cstSubArgs = &quot;Description&quot;
215 Check:
216 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
217 If Not _IsStillAlive(True) Then GoTo Finally
218 If Not ScriptForge.SF_Utils._Validate(pvDescription, &quot;Description&quot;, V_STRING) Then GoTo Finally
219 End If
221 Try:
222 &apos; Update in UNO component object and in current instance
223 sDescription = Replace(pvDescription, &quot;\n&quot;, ScriptForge.SF_String.sfNEWLINE)
224 _Component.DocumentProperties.Description = sDescription
225 If Not IsNull(_DocumentProperties) Then _DocumentProperties.ReplaceItem(&quot;Description&quot;, sdescription)
227 Finally:
228 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
229 Exit Property
230 End Property &apos; SFDocuments.SF_Document.Description
232 REM -----------------------------------------------------------------------------
233 Property Get DocumentProperties() As Variant
234 &apos;&apos;&apos; Returns a dictionary of all standard document properties, custom properties are excluded
235 DocumentProperties = _PropertyGet(&quot;DocumentProperties&quot;)
236 End Property &apos; SFDocuments.SF_Document.DocumentProperties
238 REM -----------------------------------------------------------------------------
239 Property Get DocumentType() As String
240 &apos;&apos;&apos; Returns &quot;Base&quot;, &quot;Calc&quot;, &quot;Draw&quot;, ... or &quot;Writer&quot;
241 DocumentType = _PropertyGet(&quot;DocumentType&quot;)
242 End Property &apos; SFDocuments.SF_Document.DocumentType
244 REM -----------------------------------------------------------------------------
245 Property Get ExportFilters() As Variant
246 &apos;&apos;&apos; Returns the list of the export filter names applicable to the current document
247 &apos;&apos;&apos; as a zero-based array of strings
248 &apos;&apos;&apos; Import/Export filters are included
249 ExportFilters = _PropertyGet(&quot;ExportFilters&quot;)
250 End Property &apos; SFDocuments.SF_Document.ExportFilters
252 REM -----------------------------------------------------------------------------
253 Property Get ImportFilters() As Variant
254 &apos;&apos;&apos; Returns the list of the import filter names applicable to the current document
255 &apos;&apos;&apos; as a zero-based array of strings
256 &apos;&apos;&apos; Import/Export filters are included
257 ImportFilters = _PropertyGet(&quot;ImportFilters&quot;)
258 End Property &apos; SFDocuments.SF_Document.ImportFilters
260 REM -----------------------------------------------------------------------------
261 Property Get IsBase() As Boolean
262 IsBase = _PropertyGet(&quot;IsBase&quot;)
263 End Property &apos; SFDocuments.SF_Document.IsBase
265 REM -----------------------------------------------------------------------------
266 Property Get IsCalc() As Boolean
267 IsCalc = _PropertyGet(&quot;IsCalc&quot;)
268 End Property &apos; SFDocuments.SF_Document.IsCalc
270 REM -----------------------------------------------------------------------------
271 Property Get IsDraw() As Boolean
272 IsDraw = _PropertyGet(&quot;IsDraw&quot;)
273 End Property &apos; SFDocuments.SF_Document.IsDraw
275 REM -----------------------------------------------------------------------------
276 Property Get IsFormDocument() As Boolean
277 IsFormDocument = _PropertyGet(&quot;IsFormDocument&quot;)
278 End Property &apos; SFDocuments.SF_Document.IsFormDocument
280 REM -----------------------------------------------------------------------------
281 Property Get IsImpress() As Boolean
282 IsImpress = _PropertyGet(&quot;IsImpress&quot;)
283 End Property &apos; SFDocuments.SF_Document.IsImpress
285 REM -----------------------------------------------------------------------------
286 Property Get IsMath() As Boolean
287 IsMath = _PropertyGet(&quot;IsMath&quot;)
288 End Property &apos; SFDocuments.SF_Document.IsMath
290 REM -----------------------------------------------------------------------------
291 Property Get IsWriter() As Boolean
292 IsWriter = _PropertyGet(&quot;IsWriter&quot;)
293 End Property &apos; SFDocuments.SF_Document.IsWriter
295 REM -----------------------------------------------------------------------------
296 Property Get Keywords() As Variant
297 &apos;&apos;&apos; Returns the updatable document property Keywords
298 Keywords = _PropertyGet(&quot;Keywords&quot;)
299 End Property &apos; SFDocuments.SF_Document.Keywords
301 REM -----------------------------------------------------------------------------
302 Property Let Keywords(Optional ByVal pvKeywords As Variant)
303 &apos;&apos;&apos; Sets the updatable document property Keywords
305 Dim vKeywords As Variant &apos; Alias of pvKeywords
306 Const cstThisSub = &quot;SFDocuments.Document.setKeywords&quot;
307 Const cstSubArgs = &quot;Keywords&quot;
309 Check:
310 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
311 If Not _IsStillAlive(True) Then GoTo Finally
312 If Not ScriptForge.SF_Utils._Validate(pvKeywords, &quot;Keywords&quot;, V_STRING) Then GoTo Finally
313 End If
315 Try:
316 &apos; Update in UNO component object and in current instance
317 vKeywords = ScriptForge.SF_Array.TrimArray(Split(pvKeywords, &quot;,&quot;))
318 _Component.DocumentProperties.Keywords = vKeywords
319 If Not IsNull(_DocumentProperties) Then _DocumentProperties.ReplaceItem(&quot;Keywords&quot;, Join(vKeywords, &quot;, &quot;))
321 Finally:
322 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
323 Exit Property
324 End Property &apos; SFDocuments.SF_Document.Keywords
326 REM -----------------------------------------------------------------------------
327 Property Get Readonly() As Boolean
328 &apos;&apos;&apos; Returns True if the document must not be modified
329 Readonly = _PropertyGet(&quot;Readonly&quot;)
330 End Property &apos; SFDocuments.SF_Document.Readonly
332 REM -----------------------------------------------------------------------------
333 Property Get Subject() As Variant
334 &apos;&apos;&apos; Returns the updatable document property Subject
335 Subject = _PropertyGet(&quot;Subject&quot;)
336 End Property &apos; SFDocuments.SF_Document.Subject
338 REM -----------------------------------------------------------------------------
339 Property Let Subject(Optional ByVal pvSubject As Variant)
340 &apos;&apos;&apos; Sets the updatable document property Subject
342 Const cstThisSub = &quot;SFDocuments.Document.setSubject&quot;
343 Const cstSubArgs = &quot;Subject&quot;
345 Check:
346 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
347 If Not _IsStillAlive(True) Then GoTo Finally
348 If Not ScriptForge.SF_Utils._Validate(pvSubject, &quot;Subject&quot;, V_STRING) Then GoTo Finally
349 End If
351 Try:
352 &apos; Update in UNO component object and in current instance
353 _Component.DocumentProperties.Subject = pvSubject
354 If Not IsNull(_DocumentProperties) Then _DocumentProperties.ReplaceItem(&quot;Subject&quot;, pvSubject)
356 Finally:
357 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
358 Exit Property
359 End Property &apos; SFDocuments.SF_Document.Subject
361 REM -----------------------------------------------------------------------------
362 Property Get Title() As Variant
363 &apos;&apos;&apos; Returns the updatable document property Title
364 Title = _PropertyGet(&quot;Title&quot;)
365 End Property &apos; SFDocuments.SF_Document.Title
367 REM -----------------------------------------------------------------------------
368 Property Let Title(Optional ByVal pvTitle As Variant)
369 &apos;&apos;&apos; Sets the updatable document property Title
371 Const cstThisSub = &quot;SFDocuments.Document.setTitle&quot;
372 Const cstSubArgs = &quot;Title&quot;
374 Check:
375 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
376 If Not _IsStillAlive(True) Then GoTo Finally
377 If Not ScriptForge.SF_Utils._Validate(pvTitle, &quot;Title&quot;, V_STRING) Then GoTo Finally
378 End If
380 Try:
381 &apos; Update in UNO component object and in current instance
382 _Component.DocumentProperties.Title = pvTitle
383 If Not IsNull(_DocumentProperties) Then _DocumentProperties.ReplaceItem(&quot;Title&quot;, pvTitle)
385 Finally:
386 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
387 Exit Property
388 End Property &apos; SFDocuments.SF_Document.Title
390 REM -----------------------------------------------------------------------------
391 Property Get XComponent() As Variant
392 &apos;&apos;&apos; Returns the com.sun.star.lang.XComponent UNO object representing the document
393 XComponent = _PropertyGet(&quot;XComponent&quot;)
394 End Property &apos; SFDocuments.SF_Document.XComponent
396 REM ===================================================================== METHODS
398 REM -----------------------------------------------------------------------------
399 Public Function Activate() As Boolean
400 &apos;&apos;&apos; Make the current document active
401 &apos;&apos;&apos; Args:
402 &apos;&apos;&apos; Returns:
403 &apos;&apos;&apos; True if the document could be activated
404 &apos;&apos;&apos; Otherwise, there is no change in the actual user interface
405 &apos;&apos;&apos; Examples:
406 &apos;&apos;&apos; oDoc.Activate()
408 Dim bActivate As Boolean &apos; Return value
409 Dim oContainer As Object &apos; com.sun.star.awt.XWindow
410 Const cstThisSub = &quot;SFDocuments.Document.Activate&quot;
411 Const cstSubArgs = &quot;&quot;
413 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
414 bActivate = False
416 Check:
417 ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
418 If Not _IsStillAlive() Then GoTo Finally
420 Try:
421 Set oContainer = _Frame.ContainerWindow
422 With oContainer
423 If .isVisible() = False Then .setVisible(True)
424 If .IsMinimized Then .IsMinimized = False
425 .setFocus()
426 .toFront() &apos; Force window change in Linux
427 Wait 1 &apos; Bypass desynchro issue in Linux
428 End With
429 bActivate = True
431 Finally:
432 Activate = bActivate
433 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
434 Exit Function
435 Catch:
436 GoTo Finally
437 End Function &apos; SFDocuments.SF_Document.Activate
439 REM -----------------------------------------------------------------------------
440 Public Function CloseDocument(Optional ByVal SaveAsk As Variant) As Boolean
441 &apos;&apos;&apos; Close the document. Does nothing if the document is already closed
442 &apos;&apos;&apos; regardless of how the document was closed, manually or by program
443 &apos;&apos;&apos; Args:
444 &apos;&apos;&apos; SaveAsk: If True (default), the user is invited to confirm or not the writing of the changes on disk
445 &apos;&apos;&apos; No effect if the document was not modified
446 &apos;&apos;&apos; Returns:
447 &apos;&apos;&apos; False if the user declined to close
448 &apos;&apos;&apos; Examples:
449 &apos;&apos;&apos; If oDoc.CloseDocument() Then
450 &apos;&apos;&apos; &apos; ...
452 Dim bClosed As Boolean &apos; return value
453 Dim oDispatch &apos; com.sun.star.frame.DispatchHelper
454 Const cstThisSub = &quot;SFDocuments.Document.CloseDocument&quot;
455 Const cstSubArgs = &quot;[SaveAsk=True]&quot;
457 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
458 bClosed = False
460 Check:
461 If IsMissing(SaveAsk) Or IsEmpty(SaveAsk) Then SaveAsk = True
462 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
463 If Not _IsStillAlive() Then GoTo Finally
464 If Not ScriptForge.SF_Utils._Validate(SaveAsk, &quot;SaveAsk&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
465 End If
467 Try:
468 If SaveAsk And _Component.IsModified Then &apos; Execute closure with the File/Close menu command
469 Activate()
470 RunCommand(&quot;CloseDoc&quot;)
471 bClosed = Not _IsStillAlive(, False) &apos; Do not raise error
472 Else
473 _Frame.close(True)
474 _Frame.dispose()
475 bClosed = True
476 End If
478 Finally:
479 If bClosed Then Dispose()
480 CloseDocument = bClosed
481 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
482 Exit Function
483 Catch:
484 On Local Error GoTo 0
485 GoTo Finally
486 End Function &apos; SFDocuments.SF_Document.CloseDocument
488 REM -----------------------------------------------------------------------------
489 Public Function CreateMenu(Optional ByVal MenuHeader As Variant _
490 , Optional ByVal Before As Variant _
491 , Optional ByVal SubmenuChar As Variant _
492 , Optional ByRef _Document As Variant _
493 ) As Object
494 &apos;&apos;&apos; Create a new menu entry in the document&apos;s menubar
495 &apos;&apos;&apos; The menu is not intended to be saved neither in the LibreOffice global environment, nor in the document
496 &apos;&apos;&apos; The method returns a SFWidgets.Menu instance. Its methods let define the menu further.
497 &apos;&apos;&apos; Args:
498 &apos;&apos;&apos; MenuHeader: the name/header of the menu
499 &apos;&apos;&apos; Before: the place where to put the new menu on the menubar (string or number &gt;= 1)
500 &apos;&apos;&apos; When not found =&gt; last position
501 &apos;&apos;&apos; SubmenuChar: the delimiter used in menu trees. Default = &quot;&gt;&quot;
502 &apos;&apos;&apos; _Document: undocumented argument to designate the document where the menu will be located
503 &apos;&apos;&apos; Returns:
504 &apos;&apos;&apos; A SFWidgets.Menu instance or Nothing
505 &apos;&apos;&apos; Examples:
506 &apos;&apos;&apos; Dim oMenu As Object
507 &apos;&apos;&apos; Set oMenu = oDoc.CreateMenu(&quot;My menu&quot;, Before := &quot;Styles&quot;)
508 &apos;&apos;&apos; With oMenu
509 &apos;&apos;&apos; .AddItem(&quot;Item 1&quot;, Command := &quot;About&quot;)
510 &apos;&apos;&apos; &apos;...
511 &apos;&apos;&apos; .Dispose() &apos; When definition is complete, the menu instance may be disposed
512 &apos;&apos;&apos; End With
513 &apos;&apos;&apos; &apos; ...
515 Dim oMenu As Object &apos; return value
516 Const cstThisSub = &quot;SFDocuments.Document.CreateMenu&quot;
517 Const cstSubArgs = &quot;MenuHeader, [Before=&quot;&quot;&quot;&quot;], [SubmenuChar=&quot;&quot;&gt;&quot;&quot;]&quot;
519 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
520 Set oMenu = Nothing
522 Check:
523 If IsMissing(Before) Or IsEmpty(Before) Then Before = &quot;&quot;
524 If IsMissing(SubmenuChar) Or IsEmpty(SubmenuChar) Then SubmenuChar = &quot;&quot;
525 If IsMissing(_Document) Or IsEmpty(_Document) Or IsNull(_Document) Then Set _Document = _Component
527 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
528 If Not _IsStillAlive() Then GoTo Finally
529 If Not ScriptForge.SF_Utils._Validate(MenuHeader, &quot;MenuHeader&quot;, V_STRING) Then GoTo Finally
530 If Not ScriptForge.SF_Utils._Validate(Before, &quot;Before&quot;, V_STRING) Then GoTo Finally
531 If Not ScriptForge.SF_Utils._Validate(SubmenuChar, &quot;SubmenuChar&quot;, V_STRING) Then GoTo Finally
532 End If
534 Try:
535 Set oMenu = ScriptForge.SF_Services.CreateScriptService(&quot;SFWidgets.Menu&quot;, _Document, MenuHeader, Before, SubmenuChar)
537 Finally:
538 Set CreateMenu = oMenu
539 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
540 Exit Function
541 Catch:
542 GoTo Finally
543 End Function &apos; SFDocuments.SF_Document.CreateMenu
545 REM -----------------------------------------------------------------------------
546 Public Sub Echo(Optional ByVal EchoOn As Variant _
547 , Optional ByVal Hourglass As Variant _
549 &apos;&apos;&apos; While a script is executed any display update resulting from that execution
550 &apos;&apos;&apos; is done immediately.
551 &apos;&apos;&apos; For performance reasons it might be an advantage to differ the display updates
552 &apos;&apos;&apos; up to the end of the script.
553 &apos;&apos;&apos; This is where pairs of Echo() methods to set and reset the removal of the
554 &apos;&apos;&apos; immediate updates may be beneficial.
555 &apos;&apos;&apos; Optionally the actual mouse pointer can be modified to the image of an hourglass.
556 &apos;&apos;&apos; Args:
557 &apos;&apos;&apos; EchoOn: when False, the display updates are suspended. Default = True.
558 &apos;&apos;&apos; Multiple calls with EchoOn = False are harmless.
559 &apos;&apos;&apos; Hourglass: when True, the mouse pointer is changed to an hourglass. Default = False.
560 &apos;&apos;&apos; The mouse pointer needs to be inside the actual document&apos;s window.
561 &apos;&apos;&apos; Note that it is very likely that at the least manual movement of the mouse,
562 &apos;&apos;&apos; the operating system or the LibreOffice process will take back the control
563 &apos;&apos;&apos; of the mouse icon and its usual behaviour.
564 &apos;&apos;&apos; Returns:
565 &apos;&apos;&apos; Examples:
566 &apos;&apos;&apos; oDoc.Echo(False, Hourglass := True)
567 &apos;&apos;&apos; &apos; ... &quot;long-lasting&quot; script ...
568 &apos;&apos;&apos; oDoc.Echo() &apos; Reset to normal
570 Dim oContainer As Object &apos; com.sun.star.awt.XWindow
571 Dim lPointer As Long &apos; com.sun.star.awt.SystemPointer constant
572 Dim oPointer As Object &apos; com.sun.star.awt.Pointer
573 Const cstThisSub = &quot;SFDocuments.Document.Echo&quot;
574 Const cstSubArgs = &quot;[EchoOn=True], [Hourglass=False]&quot;
576 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
578 Check:
579 If IsMissing(EchoOn) Or IsEmpty(EchoOn) Then EchoOn = True
580 If IsMissing(Hourglass) Or IsEmpty(Hourglass) Then Hourglass = False
581 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
582 If Not _IsStillAlive() Then GoTo Finally
583 If Not SF_Utils._Validate(EchoOn, &quot;EchoOn&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
584 If Not SF_Utils._Validate(Hourglass, &quot;Hourglass&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
585 End If
587 Try:
588 With _Component
590 Set oContainer = .CurrentController.Frame.GetContainerWindow()
591 Set oPointer = CreateUnoService(&quot;com.sun.star.awt.Pointer&quot;)
592 With com.sun.star.awt.SystemPointer
593 If Hourglass Then lPointer = .WAIT Else lPointer = .ARROW
594 End With
595 oPointer.setType(lPointer)
597 &apos; Mouse icon is set when controller is unlocked
598 If Not EchoOn Then
599 oContainer.setPointer(oPointer)
600 .lockControllers()
601 Else &apos; EchoOn = True
602 Do While .hasControllersLocked()
603 .unlockControllers()
604 Loop
605 oContainer.setPointer(oPointer)
606 End If
608 End With
610 Finally:
611 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
612 Exit Sub
613 Catch:
614 GoTo Finally
615 End Sub &apos; SFDocuments.SF_Document.Echo
617 REM -----------------------------------------------------------------------------
618 Public Function ExportAsPDF(Optional ByVal FileName As Variant _
619 , Optional ByVal Overwrite As Variant _
620 , Optional ByVal Pages As Variant _
621 , Optional ByVal Password As Variant _
622 , Optional ByVal Watermark As Variant _
623 ) As Boolean
624 &apos;&apos;&apos; Store the document to the given file location in PDF format
625 &apos;&apos;&apos; Args:
626 &apos;&apos;&apos; FileName: Identifies the file where to save. It must follow the SF_FileSystem.FileNaming notation
627 &apos;&apos;&apos; Overwrite: True if the destination file may be overwritten (default = False)
628 &apos;&apos;&apos; Pages: the pages to print as a string, like in the user interface. Example: &quot;1-4;10;15-18&quot;. Default = all pages
629 &apos;&apos;&apos; Password: password to open the document
630 &apos;&apos;&apos; Watermark: the text for a watermark to be drawn on every page of the exported PDF file
631 &apos;&apos;&apos; Returns:
632 &apos;&apos;&apos; False if the document could not be saved
633 &apos;&apos;&apos; Exceptions:
634 &apos;&apos;&apos; DOCUMENTSAVEASERROR The destination has its readonly attribute set or overwriting rejected
635 &apos;&apos;&apos; Examples:
636 &apos;&apos;&apos; oDoc.ExportAsPDF(&quot;C:\Me\myDoc.pdf&quot;, Overwrite := True)
638 Dim bSaved As Boolean &apos; return value
639 Dim oSfa As Object &apos; com.sun.star.ucb.SimpleFileAccess
640 Dim sFile As String &apos; Alias of FileName
641 Dim sFilter As String &apos; One of the pdf filter names
642 Dim vFilterData As Variant &apos; Array of com.sun.star.beans.PropertyValue
643 Dim vProperties As Variant &apos; Array of com.sun.star.beans.PropertyValue
644 Dim FSO As Object &apos; SF_FileSystem
645 Const cstThisSub = &quot;SFDocuments.Document.ExportAsPDF&quot;
646 Const cstSubArgs = &quot;FileName, [Overwrite=False], [Pages=&quot;&quot;&quot;&quot;], [Password=&quot;&quot;&quot;&quot;], [Watermark=&quot;&quot;&quot;&quot;]&quot;
648 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo CatchError
649 bSaved = False
651 Check:
652 If IsMissing(Overwrite) Or IsEmpty(Overwrite) Then Overwrite = False
653 If IsMissing(Pages) Or IsEmpty(Pages) Then Pages = &quot;&quot;
654 If IsMissing(Password) Or IsEmpty(Password) Then Password = &quot;&quot;
655 If IsMissing(Watermark) Or IsEmpty(Watermark) Then Watermark = &quot;&quot;
657 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
658 If Not _IsStillAlive() Then GoTo Finally
659 If Not SF_Utils._ValidateFile(FileName, &quot;FileName&quot;) Then GoTo Finally
660 If Not SF_Utils._Validate(Overwrite, &quot;Overwrite&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
661 If Not SF_Utils._Validate(Pages, &quot;Pages&quot;, V_STRING) Then GoTo Finally
662 If Not SF_Utils._Validate(Password, &quot;Password&quot;, V_STRING) Then GoTo Finally
663 If Not SF_Utils._Validate(Watermark, &quot;Watermark&quot;, V_STRING) Then GoTo Finally
664 End If
666 &apos; Check destination file overwriting
667 Set FSO = CreateScriptService(&quot;FileSystem&quot;)
668 sFile = FSO._ConvertToUrl(FileName)
669 If FSO.FileExists(FileName) Then
670 If Overwrite = False Then GoTo CatchError
671 Set oSfa = ScriptForge.SF_Utils._GetUNOService(&quot;FileAccess&quot;)
672 If oSfa.isReadonly(sFile) Then GoTo CatchError
673 End If
675 Try:
676 &apos; Setup arguments
677 Select Case _DocumentType &apos; Disguise form documents as a Writer document
678 Case &quot;FormDocument&quot; : sFilter = &quot;Writer_pdf_Export&quot;
679 Case Else : sFilter = LCase(_DocumentType) &amp; &quot;_pdf_Export&quot;
680 End Select
681 &apos; FilterData parameters are added only if they are meaningful
682 vFilterData = Array()
683 If Len(Pages) &gt; 0 Then
684 vFilterData = ScriptForge.SF_Array.Append(vFilterData _
685 , ScriptForge.SF_Utils._MakePropertyValue(&quot;PageRange&quot;, Pages))
686 End If
687 If Len(Password) &gt; 0 Then
688 vFilterData = ScriptForge.SF_Array.Append(vFilterData _
689 , ScriptForge.SF_Utils._MakePropertyValue(&quot;EncryptFile&quot;, True) _
690 , ScriptForge.SF_Utils._MakePropertyValue(&quot;DocumentOpenPassword&quot;, Password))
691 End If
692 If Len(Watermark) &gt; 0 Then
693 vFilterData = ScriptForge.SF_Array.Append(vFilterData _
694 , ScriptForge.SF_Utils._MakePropertyValue(&quot;Watermark&quot;, Watermark))
695 End If
697 &apos; Finalize properties and export
698 vProperties = Array( _
699 ScriptForge.SF_Utils._MakePropertyValue(&quot;FilterName&quot;, sFilter) _
700 , ScriptForge.SF_Utils._MakePropertyValue(&quot;FilterData&quot;, vFilterData))
701 _Component.StoreToURL(sFile, vProperties)
702 bSaved = True
704 Finally:
705 ExportAsPDF = bSaved
706 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
707 Exit Function
708 Catch:
709 GoTo Finally
710 CatchError:
711 ScriptForge.SF_Exception.RaiseFatal(DOCUMENTSAVEASERROR, &quot;FileName&quot;, FileName, &quot;Overwrite&quot;, Overwrite _
712 , &quot;FilterName&quot;, &quot;PDF Export&quot;)
713 GoTo Finally
714 End Function &apos; SFDocuments.SF_Document.ExportAsPDF
716 REM -----------------------------------------------------------------------------
717 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
718 &apos;&apos;&apos; Return the actual value of the given property
719 &apos;&apos;&apos; Args:
720 &apos;&apos;&apos; PropertyName: the name of the property as a string
721 &apos;&apos;&apos; Returns:
722 &apos;&apos;&apos; The actual value of the property
723 &apos;&apos;&apos; If the property does not exist, returns Null
724 &apos;&apos;&apos; Exceptions:
725 &apos;&apos;&apos; see the exceptions of the individual properties
726 &apos;&apos;&apos; Examples:
727 &apos;&apos;&apos; myModel.GetProperty(&quot;MyProperty&quot;)
729 Const cstThisSub = &quot;SFDocuments.Document.GetProperty&quot;
730 Const cstSubArgs = &quot;&quot;
732 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
733 GetProperty = Null
735 Check:
736 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
737 If Not ScriptForge.SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
738 End If
740 Try:
741 GetProperty = _PropertyGet(PropertyName)
743 Finally:
744 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
745 Exit Function
746 Catch:
747 GoTo Finally
748 End Function &apos; SFDocuments.SF_Document.GetProperty
750 REM -----------------------------------------------------------------------------
751 Public Function Methods() As Variant
752 &apos;&apos;&apos; Return the list of public methods of the Document service as an array
754 Methods = Array( _
755 &quot;Activate&quot; _
756 , &quot;CloseDocument&quot; _
757 , &quot;CreateMenu&quot; _
758 , &quot;Echo&quot; _
759 , &quot;ExportAsPDF&quot; _
760 , &quot;PrintOut&quot; _
761 , &quot;RemoveMenu&quot; _
762 , &quot;RunCommand&quot; _
763 , &quot;Save&quot; _
764 , &quot;SaveAs&quot; _
765 , &quot;SaveCopyAs&quot; _
766 , &quot;SetPrinter&quot; _
769 End Function &apos; SFDocuments.SF_Document.Methods
771 REM -----------------------------------------------------------------------------
772 Public Function PrintOut(Optional ByVal Pages As Variant _
773 , Optional ByVal Copies As Variant _
774 , Optional ByRef _Document As Variant _
775 ) As Boolean
776 &apos;&apos;&apos; Send the content of the document to the printer.
777 &apos;&apos;&apos; The printer might be defined previously by default, by the user or by the SetPrinter() method
778 &apos;&apos;&apos; Args:
779 &apos;&apos;&apos; Pages: the pages to print as a string, like in the user interface. Example: &quot;1-4;10;15-18&quot;. Default = all pages
780 &apos;&apos;&apos; Copies: the number of copies
781 &apos;&apos;&apos; _Document: undocumented argument to designate the document to print when called from a subclass
782 &apos;&apos;&apos; Returns:
783 &apos;&apos;&apos; True when successful
784 &apos;&apos;&apos; Examples:
785 &apos;&apos;&apos; oDoc.PrintOut(&quot;1-4;10;15-18&quot;, Copies := 2)
787 Dim bPrint As Boolean &apos; Return value
788 Dim vPrintGoal As Variant &apos; Array of property values
790 Const cstThisSub = &quot;SFDocuments.Document.PrintOut&quot;
791 Const cstSubArgs = &quot;[Pages=&quot;&quot;&quot;&quot;], [Copies=1]&quot;
793 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
794 bPrint = False
796 Check:
797 If IsMissing(Pages) Or IsEmpty(Pages) Then Pages = &quot;&quot;
798 If IsMissing(Copies) Or IsEmpty(Copies) Then Copies = 1
799 If IsMissing(_Document) Or IsEmpty(_Document) Or IsNull(_Document) Then Set _Document = _Component
801 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
802 If Not _IsStillAlive() Then GoTo Finally
803 If Not ScriptForge.SF_Utils._Validate(Pages, &quot;Pages&quot;, V_STRING) Then GoTo Finally
804 If Not ScriptForge.SF_Utils._Validate(Copies, &quot;Copies&quot;, ScriptForge.V_NUMERIC) Then GoTo Finally
805 End If
807 Try:
808 vPrintGoal = Array( _
809 ScriptForge.SF_Utils._MakePropertyValue(&quot;CopyCount&quot;, CInt(Copies)) _
810 , ScriptForge.SF_Utils._MakePropertyValue(&quot;Collate&quot;, True) _
811 , ScriptForge.SF_Utils._MakePropertyValue(&quot;Pages&quot;, Pages) _
812 , ScriptForge.SF_Utils._MakePropertyValue(&quot;Wait&quot;, False) _
815 _Document.Print(vPrintGoal)
816 bPrint = True
818 Finally:
819 PrintOut = bPrint
820 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
821 Exit Function
822 Catch:
823 GoTo Finally
824 End Function &apos; SFDocuments.SF_Document.PrintOut
826 REM -----------------------------------------------------------------------------
827 Public Function Properties() As Variant
828 &apos;&apos;&apos; Return the list or properties of the Document class as an array
830 Properties = Array( _
831 &quot;CustomProperties&quot; _
832 , &quot;Description&quot; _
833 , &quot;DocumentProperties&quot; _
834 , &quot;DocumentType&quot; _
835 , &quot;ExportFilters&quot; _
836 , &quot;ImportFilters&quot; _
837 , &quot;IsBase&quot; _
838 , &quot;IsCalc&quot; _
839 , &quot;IsDraw&quot; _
840 , &quot;IsFormDocument&quot; _
841 , &quot;IsImpress&quot; _
842 , &quot;IsMath&quot; _
843 , &quot;IsWriter&quot; _
844 , &quot;Keywords&quot; _
845 , &quot;Readonly&quot; _
846 , &quot;Subject&quot; _
847 , &quot;Title&quot; _
848 , &quot;XComponent&quot; _
851 End Function &apos; SFDocuments.SF_Document.Properties
853 REM -----------------------------------------------------------------------------
854 Public Function RemoveMenu(Optional ByVal MenuHeader As Variant _
855 , Optional ByRef _Document As Variant _
856 ) As Boolean
857 &apos;&apos;&apos; Remove a menu entry in the document&apos;s menubar
858 &apos;&apos;&apos; The removal is not intended to be saved neither in the LibreOffice global environment, nor in the document
859 &apos;&apos;&apos; Args:
860 &apos;&apos;&apos; MenuHeader: the name/header of the menu, without tilde &quot;~&quot;, as a case-sensitive string
861 &apos;&apos;&apos; _Document: undocumented argument to designate the document where the menu is located
862 &apos;&apos;&apos; Returns:
863 &apos;&apos;&apos; True when successful
864 &apos;&apos;&apos; Examples:
865 &apos;&apos;&apos; oDoc.RemoveMenu(&quot;File&quot;)
866 &apos;&apos;&apos; &apos; ...
868 Dim bRemove As Boolean &apos; Return value
869 Dim oLayout As Object &apos; com.sun.star.comp.framework.LayoutManager
870 Dim oMenuBar As Object &apos; com.sun.star.awt.XMenuBar or stardiv.Toolkit.VCLXMenuBar
871 Dim sName As String &apos; Menu name
872 Dim iMenuId As Integer &apos; Menu identifier
873 Dim iMenuPosition As Integer &apos; Menu position &gt;= 0
874 Dim i As Integer
875 Const cstTilde = &quot;~&quot;
877 Const cstThisSub = &quot;SFDocuments.Document.RemoveMenu&quot;
878 Const cstSubArgs = &quot;MenuHeader&quot;
880 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
881 bRemove = False
883 Check:
884 If IsMissing(_Document) Or IsEmpty(_Document) Or IsNull(_Document) Then Set _Document = _Component
885 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
886 If Not _IsStillAlive() Then GoTo Finally
887 If Not ScriptForge.SF_Utils._Validate(MenuHeader, &quot;MenuHeader&quot;, V_STRING) Then GoTo Finally
888 End If
890 Try:
891 Set oLayout = _Document.CurrentController.Frame.LayoutManager
892 Set oMenuBar = oLayout.getElement(&quot;private:resource/menubar/menubar&quot;).XMenuBar
894 &apos; Search the menu identifier to remove by its name, Mark its position
895 With oMenuBar
896 iMenuPosition = -1
897 For i = 0 To .ItemCount - 1
898 iMenuId = .getItemId(i)
899 sName = Replace(.getItemText(iMenuId), cstTilde, &quot;&quot;)
900 If MenuHeader= sName Then
901 iMenuPosition = i
902 Exit For
903 End If
904 Next i
905 &apos; Remove the found menu item
906 If iMenuPosition &gt;= 0 Then
907 .removeItem(iMenuPosition, 1)
908 bRemove = True
909 End If
910 End With
912 Finally:
913 RemoveMenu = bRemove
914 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
915 Exit Function
916 Catch:
917 GoTo Finally
918 End Function &apos; SFDocuments.SF_Document.RemoveMenu
920 REM -----------------------------------------------------------------------------
921 Public Sub RunCommand(Optional ByVal Command As Variant _
922 , ParamArray Args As Variant _
924 &apos;&apos;&apos; Run on the current document window the given menu command. The command is executed with or without arguments
925 &apos;&apos;&apos; A few typical commands:
926 &apos;&apos;&apos; Save, SaveAs, ExportToPDF, SetDocumentProperties, Undo, Copy, Paste, ...
927 &apos;&apos;&apos; Dozens can be found on next page: https://wiki.documentfoundation.org/Development/DispatchCommands
928 &apos;&apos;&apos; Args:
929 &apos;&apos;&apos; Command: Case-sensitive. The command itself is not checked.
930 &apos;&apos;&apos; If the command does not contain the &quot;.uno:&quot; prefix, it is added.
931 &apos;&apos;&apos; If nothing happens, then the command is probably wrong
932 &apos;&apos;&apos; Args: Pairs of arguments name (string), value (any)
933 &apos;&apos;&apos; Returns:
934 &apos;&apos;&apos; Examples:
935 &apos;&apos;&apos; oDoc.RunCommand(&quot;EditDoc&quot;, &quot;Editable&quot;, False) &apos; Toggle edit mode
937 Dim vArgs As Variant &apos; Alias of Args
938 Dim oDispatch &apos; com.sun.star.frame.DispatchHelper
939 Dim vProps As Variant &apos; Array of PropertyValues
940 Dim vValue As Variant &apos; A single value argument
941 Dim sCommand As String &apos; Alias of Command
942 Dim i As Long
943 Const cstPrefix = &quot;.uno:&quot;
945 Const cstThisSub = &quot;SFDocuments.Document.RunCommand&quot;
946 Const cstSubArgs = &quot;Command, [arg0Name, arg0Value], [arg1Name, arg1Value], ...&quot;
948 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
950 Check:
951 &apos; When called from a subclass (Calc, Writer, ..) the arguments are gathered into one single array item
952 vArgs = Args
953 If IsArray(Args) Then
954 If UBound(Args) &gt;= 0 Then
955 If IsArray(Args(0)) Then vArgs = Args(0)
956 End If
957 End If
958 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
959 If Not _IsStillAlive() Then GoTo Finally
960 If Not ScriptForge.SF_Utils._Validate(Command, &quot;Command&quot;, V_STRING) Then GoTo Finally
961 If Not ScriptForge.SF_Utils._ValidateArray(vArgs, &quot;Args&quot;, 1) Then GoTo Finally
962 For i = 0 To UBound(vArgs) - 1 Step 2
963 If Not ScriptForge.SF_Utils._Validate(vArgs(i), &quot;Arg&quot; &amp; CStr(i/2) &amp; &quot;Name&quot;, V_STRING) Then GoTo Finally
964 Next i
965 End If
967 Try:
968 &apos; Build array of property values
969 vProps = Array()
970 For i = 0 To UBound(vArgs) - 1 Step 2
971 If IsEmpty(vArgs(i + 1)) Then vValue = Null Else vValue = vArgs(i + 1)
972 vProps = ScriptForge.SF_Array.Append(vProps, ScriptForge.SF_Utils._MakePropertyValue(vArgs(i), vValue))
973 Next i
974 Set oDispatch = ScriptForge.SF_Utils._GetUNOService(&quot;DispatchHelper&quot;)
975 If ScriptForge.SF_String.StartsWith(Command, cstPrefix) Then sCommand = Command Else sCommand = cstPrefix &amp; Command
976 oDispatch.executeDispatch(_Frame, sCommand, &quot;&quot;, 0, vProps)
978 Finally:
979 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
980 Exit Sub
981 Catch:
982 GoTo Finally
983 End Sub &apos; SFDocuments.SF_Document.RunCommand
985 REM -----------------------------------------------------------------------------
986 Public Function Save() As Boolean
987 &apos;&apos;&apos; Store the document to the file location from which it was loaded
988 &apos;&apos;&apos; Ignored if the document was not modified
989 &apos;&apos;&apos; Args:
990 &apos;&apos;&apos; Returns:
991 &apos;&apos;&apos; False if the document could not be saved
992 &apos;&apos;&apos; Exceptions:
993 &apos;&apos;&apos; DOCUMENTSAVEERROR The file has been opened readonly or was opened as new and was not yet saved
994 &apos;&apos;&apos; Examples:
995 &apos;&apos;&apos; If Not oDoc.Save() Then
996 &apos;&apos;&apos; &apos; ...
998 Dim bSaved As Boolean &apos; return value
999 Const cstThisSub = &quot;SFDocuments.Document.Save&quot;
1000 Const cstSubArgs = &quot;&quot;
1002 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1003 bSaved = False
1005 Check:
1006 ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
1007 If Not _IsStillAlive() Then GoTo Finally
1008 bSaved = False
1010 Try:
1011 With _Component
1012 If .isReadonly() Or Not .hasLocation() Then GoTo CatchReadonly
1013 If .IsModified() Then
1014 .store()
1015 bSaved = True
1016 End If
1017 End With
1019 Finally:
1020 Save = bSaved
1021 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
1022 Exit Function
1023 Catch:
1024 GoTo Finally
1025 CatchReadonly:
1026 ScriptForge.SF_Exception.RaiseFatal(DOCUMENTSAVEERROR, &quot;FileName&quot;, _FileIdent())
1027 GoTo Finally
1028 End Function &apos; SFDocuments.SF_Document.Save
1030 REM -----------------------------------------------------------------------------
1031 Public Function SaveAs(Optional ByVal FileName As Variant _
1032 , Optional ByVal Overwrite As Variant _
1033 , Optional ByVal Password As Variant _
1034 , Optional ByVal FilterName As Variant _
1035 , Optional ByVal FilterOptions As Variant _
1036 ) As Boolean
1037 &apos;&apos;&apos; Store the document to the given file location
1038 &apos;&apos;&apos; The new location becomes the new file name on which simple Save method calls will be applied
1039 &apos;&apos;&apos; Args:
1040 &apos;&apos;&apos; FileName: Identifies the file where to save. It must follow the SF_FileSystem.FileNaming notation
1041 &apos;&apos;&apos; Overwrite: True if the destination file may be overwritten (default = False)
1042 &apos;&apos;&apos; Password: Use to protect the document
1043 &apos;&apos;&apos; FilterName: the name of a filter that should be used for saving the document
1044 &apos;&apos;&apos; If present, the filter must exist
1045 &apos;&apos;&apos; FilterOptions: an optional string of options associated with the filter
1046 &apos;&apos;&apos; Returns:
1047 &apos;&apos;&apos; False if the document could not be saved
1048 &apos;&apos;&apos; Exceptions:
1049 &apos;&apos;&apos; DOCUMENTSAVEASERROR The destination has its readonly attribute set or overwriting rejected
1050 &apos;&apos;&apos; Examples:
1051 &apos;&apos;&apos; oDoc.SaveAs(&quot;C:\Me\Copy2.odt&quot;, Overwrite := True)
1053 Dim bSaved As Boolean &apos; return value
1054 Dim oFilterFactory As Object &apos; com.sun.star.document.FilterFactory
1055 Dim oSfa As Object &apos; com.sun.star.ucb.SimpleFileAccess
1056 Dim sFile As String &apos; Alias of FileName
1057 Dim vProperties As Variant &apos; Array of com.sun.star.beans.PropertyValue
1058 Dim FSO As Object &apos; SF_FileSystem
1059 Const cstThisSub = &quot;SFDocuments.Document.SaveAs&quot;
1060 Const cstSubArgs = &quot;FileName, [Overwrite=False], [Password=&quot;&quot;&quot;&quot;], [FilterName=&quot;&quot;&quot;&quot;], [FilterOptions=&quot;&quot;&quot;&quot;]&quot;
1062 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo CatchError
1063 bSaved = False
1065 Check:
1066 If IsMissing(Overwrite) Or IsEmpty(Overwrite) Then Overwrite = False
1067 If IsMissing(Password) Or IsEmpty(Password) Then Password = &quot;&quot;
1068 If IsMissing(FilterName) Or IsEmpty(FilterName) Then FilterName = &quot;&quot;
1069 If IsMissing(FilterOptions) Or IsEmpty(FilterOptions) Then FilterOptions = &quot;&quot;
1071 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1072 If Not _IsStillAlive() Then GoTo Finally
1073 If Not SF_Utils._ValidateFile(FileName, &quot;FileName&quot;) Then GoTo Finally
1074 If Not SF_Utils._Validate(Overwrite, &quot;Overwrite&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
1075 If Not SF_Utils._Validate(Password, &quot;Password&quot;, V_STRING) Then GoTo Finally
1076 If Not SF_Utils._Validate(FilterName, &quot;FilterName&quot;, V_STRING) Then GoTo Finally
1077 If Not SF_Utils._Validate(FilterOptions, &quot;FilterOptions&quot;, V_STRING) Then GoTo Finally
1078 End If
1080 &apos; Check that the filter exists
1081 If Len(FilterName) &gt; 0 Then
1082 Set oFilterFactory = ScriptForge.SF_Utils._GetUNOService(&quot;FilterFactory&quot;)
1083 If Not oFilterFactory.hasByName(FilterName) Then GoTo CatchError
1084 End If
1086 &apos; Check destination file overwriting
1087 Set FSO = CreateScriptService(&quot;FileSystem&quot;)
1088 sFile = FSO._ConvertToUrl(FileName)
1089 If FSO.FileExists(FileName) Then
1090 If Overwrite = False Then GoTo CatchError
1091 Set oSfa = ScriptForge.SF_Utils._GetUNOService(&quot;FileAccess&quot;)
1092 If oSfa.isReadonly(sFile) Then GoTo CatchError
1093 End If
1095 Try:
1096 &apos; Setup arguments
1097 If Len(Password) + Len(FilterName) = 0 Then
1098 vProperties = Array()
1099 Else
1100 vProperties = Array( _
1101 ScriptForge.SF_Utils._MakePropertyValue(&quot;FilterName&quot;, FilterName) _
1102 , ScriptForge.SF_Utils._MakePropertyValue(&quot;FilterOptions&quot;, FilterOptions) _
1104 If Len(Password) &gt; 0 Then &apos; Password is to add only if &lt;&gt; &quot;&quot; !?
1105 vProperties = ScriptForge.SF_Array.Append(vProperties _
1106 , ScriptForge.SF_Utils._MakePropertyValue(&quot;Password&quot;, Password))
1107 End If
1108 End If
1110 _Component.StoreAsURL(sFile, vProperties)
1112 &apos; Remind the new file name
1113 _WindowFileName = sFile
1114 _WindowName = FSO.GetName(FileName)
1115 bSaved = True
1117 Finally:
1118 SaveAs = bSaved
1119 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
1120 Exit Function
1121 Catch:
1122 GoTo Finally
1123 CatchError:
1124 ScriptForge.SF_Exception.RaiseFatal(DOCUMENTSAVEASERROR, &quot;FileName&quot;, FileName, &quot;Overwrite&quot;, Overwrite _
1125 , &quot;FilterName&quot;, FilterName)
1126 GoTo Finally
1127 End Function &apos; SFDocuments.SF_Document.SaveAs
1129 REM -----------------------------------------------------------------------------
1130 Public Function SaveCopyAs(Optional ByVal FileName As Variant _
1131 , Optional ByVal Overwrite As Variant _
1132 , Optional ByVal Password As Variant _
1133 , Optional ByVal FilterName As Variant _
1134 , Optional ByVal FilterOptions As Variant _
1135 ) As Boolean
1136 &apos;&apos;&apos; Store a copy or export the document to the given file location
1137 &apos;&apos;&apos; The actual location is unchanged
1138 &apos;&apos;&apos; Args:
1139 &apos;&apos;&apos; FileName: Identifies the file where to save. It must follow the SF_FileSystem.FileNaming notation
1140 &apos;&apos;&apos; Overwrite: True if the destination file may be overwritten (default = False)
1141 &apos;&apos;&apos; Password: Use to protect the document
1142 &apos;&apos;&apos; FilterName: the name of a filter that should be used for saving the document
1143 &apos;&apos;&apos; If present, the filter must exist
1144 &apos;&apos;&apos; FilterOptions: an optional string of options associated with the filter
1145 &apos;&apos;&apos; Returns:
1146 &apos;&apos;&apos; False if the document could not be saved
1147 &apos;&apos;&apos; Exceptions:
1148 &apos;&apos;&apos; DOCUMENTSAVEASERROR The destination has its readonly attribute set or overwriting rejected
1149 &apos;&apos;&apos; Examples:
1150 &apos;&apos;&apos; oDoc.SaveCopyAs(&quot;C:\Me\Copy2.odt&quot;, Overwrite := True)
1152 Dim bSaved As Boolean &apos; return value
1153 Dim oFilterFactory As Object &apos; com.sun.star.document.FilterFactory
1154 Dim oSfa As Object &apos; com.sun.star.ucb.SimpleFileAccess
1155 Dim sFile As String &apos; Alias of FileName
1156 Dim vProperties As Variant &apos; Array of com.sun.star.beans.PropertyValue
1157 Dim FSO As Object &apos; SF_FileSystem
1158 Const cstThisSub = &quot;SFDocuments.Document.SaveCopyAs&quot;
1159 Const cstSubArgs = &quot;FileName, [Overwrite=False], [Password=&quot;&quot;&quot;&quot;], [FilterName=&quot;&quot;&quot;&quot;], [FilterOptions=&quot;&quot;&quot;&quot;]&quot;
1161 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo CatchError
1162 bSaved = False
1164 Check:
1165 If IsMissing(Overwrite) Or IsEmpty(Overwrite) Then Overwrite = False
1166 If IsMissing(Password) Or IsEmpty(Password) Then Password = &quot;&quot;
1167 If IsMissing(FilterName) Or IsEmpty(FilterName) Then FilterName = &quot;&quot;
1168 If IsMissing(FilterOptions) Or IsEmpty(FilterOptions) Then FilterOptions = &quot;&quot;
1170 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1171 If Not _IsStillAlive() Then GoTo Finally
1172 If Not SF_Utils._ValidateFile(FileName, &quot;FileName&quot;) Then GoTo Finally
1173 If Not SF_Utils._Validate(Overwrite, &quot;Overwrite&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
1174 If Not SF_Utils._Validate(Password, &quot;Password&quot;, V_STRING) Then GoTo Finally
1175 If Not SF_Utils._Validate(FilterName, &quot;FilterName&quot;, V_STRING) Then GoTo Finally
1176 If Not SF_Utils._Validate(FilterOptions, &quot;FilterOptions&quot;, V_STRING) Then GoTo Finally
1177 End If
1179 &apos; Check that the filter exists
1180 If Len(FilterName) &gt; 0 Then
1181 Set oFilterFactory = ScriptForge.SF_Utils._GetUNOService(&quot;FilterFactory&quot;)
1182 If Not oFilterFactory.hasByName(FilterName) Then GoTo CatchError
1183 End If
1185 &apos; Check destination file overwriting
1186 Set FSO = CreateScriptService(&quot;FileSystem&quot;)
1187 sFile = FSO._ConvertToUrl(FileName)
1188 If FSO.FileExists(FileName) Then
1189 If Overwrite = False Then GoTo CatchError
1190 Set oSfa = ScriptForge.SF_Utils._GetUNOService(&quot;FileAccess&quot;)
1191 If oSfa.isReadonly(sFile) Then GoTo CatchError
1192 End If
1194 Try:
1195 &apos; Setup arguments
1196 If Len(Password) + Len(FilterName) = 0 Then
1197 vProperties = Array()
1198 Else
1199 vProperties = Array( _
1200 ScriptForge.SF_Utils._MakePropertyValue(&quot;FilterName&quot;, FilterName) _
1201 , ScriptForge.SF_Utils._MakePropertyValue(&quot;FilterOptions&quot;, FilterOptions) _
1203 If Len(Password) &gt; 0 Then &apos; Password is to add only if &lt;&gt; &quot;&quot; !?
1204 vProperties = ScriptForge.SF_Array.Append(vProperties _
1205 , ScriptForge.SF_Utils._MakePropertyValue(&quot;Password&quot;, Password))
1206 End If
1207 End If
1209 _Component.StoreToURL(sFile, vProperties)
1210 bSaved = True
1212 Finally:
1213 SaveCopyAs = bSaved
1214 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
1215 Exit Function
1216 Catch:
1217 GoTo Finally
1218 CatchError:
1219 ScriptForge.SF_Exception.RaiseFatal(DOCUMENTSAVEASERROR, &quot;FileName&quot;, FileName, &quot;Overwrite&quot;, Overwrite _
1220 , &quot;FilterName&quot;, FilterName)
1221 GoTo Finally
1222 End Function &apos; SFDocuments.SF_Document.SaveCopyAs
1224 REM -----------------------------------------------------------------------------
1225 Public Function SetPrinter(Optional ByVal Printer As Variant _
1226 , Optional ByVal Orientation As Variant _
1227 , Optional ByVal PaperFormat As Variant _
1228 , Optional ByRef _PrintComponent As Variant _
1229 ) As Boolean
1230 &apos;&apos;&apos; Define the printer options for the document
1231 &apos;&apos;&apos; Args:
1232 &apos;&apos;&apos; Printer: the name of the printer queue where to print to
1233 &apos;&apos;&apos; When absent or space, the default printer is set
1234 &apos;&apos;&apos; Orientation: either &quot;PORTRAIT&quot; or &quot;LANDSCAPE&quot;. Left unchanged when absent
1235 &apos;&apos;&apos; PaperFormat: one of next values
1236 &apos;&apos;&apos; &quot;A3&quot;, &quot;A4&quot;, &quot;A5&quot;, &quot;B4&quot;, &quot;B5&quot;, &quot;LETTER&quot;, &quot;LEGAL&quot;, &quot;TABLOID&quot;
1237 &apos;&apos;&apos; Left unchanged when absent
1238 &apos;&apos;&apos; _PrintComponent: undocumented argument to determine the component
1239 &apos;&apos;&apos; Useful typically to apply printer settings on a Base form document
1240 &apos;&apos;&apos; Returns:
1241 &apos;&apos;&apos; True when successful
1242 &apos;&apos;&apos; Examples:
1243 &apos;&apos;&apos; oDoc.SetPrinter(Orientation := &quot;PORTRAIT&quot;)
1245 Dim bPrinter As Boolean &apos; Return value
1246 Dim vPrinters As Variant &apos; Array of known printers
1247 Dim vOrientations As Variant &apos; Array of allowed paper orientations
1248 Dim vPaperFormats As Variant &apos; Array of allowed formats
1249 Dim vPrinterSettings As Variant &apos; Array of property values
1250 Dim oPropertyValue As New com.sun.star.beans.PropertyValue
1251 &apos; A single property value item
1252 Const cstThisSub = &quot;SFDocuments.Document.SetPrinter&quot;
1253 Const cstSubArgs = &quot;[Printer=&quot;&quot;&quot;&quot;], [Orientation=&quot;&quot;PORTRAIT&quot;&quot;|&quot;&quot;LANDSCAPE&quot;&quot;]&quot; _
1254 &amp; &quot;, [PaperFormat=&quot;&quot;A3&quot;&quot;|&quot;&quot;A4&quot;&quot;|&quot;&quot;A5&quot;&quot;|&quot;&quot;B4&quot;&quot;|&quot;&quot;B5&quot;&quot;|&quot;&quot;LETTER&quot;&quot;|&quot;&quot;LEGAL&quot;&quot;|&quot;&quot;TABLOID&quot;&quot;&quot;
1256 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1257 bPrinter = False
1259 Check:
1260 If IsMissing(Printer) Or IsEmpty(Printer) Then Printer = &quot;&quot;
1261 If IsMissing(Orientation) Or IsEmpty(Orientation) Then Orientation = &quot;&quot;
1262 If IsMissing(PaperFormat) Or IsEmpty(PaperFormat) Then PaperFormat = &quot;&quot;
1263 If IsMissing(_PrintComponent) Or IsEmpty(_PrintComponent) Then Set _PrintComponent = _Component
1265 ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) &apos; Unconditional validation
1266 If Not _IsStillAlive() Then GoTo Finally
1267 If VarType(Printer) = V_STRING Then
1268 vPrinters = ScriptForge.SF_Platform.Printers
1269 If Len(Printer) &gt; 0 Then
1270 If Not ScriptForge.SF_Utils._Validate(Printer, &quot;Printer&quot;, V_STRING, vPrinters) Then GoTo Finally
1271 End If
1272 Else
1273 If Not ScriptForge.SF_Utils._Validate(Printer, &quot;Printer&quot;, V_STRING) Then GoTo Finally &apos; Manage here the VarType error
1274 End If
1275 If VarType(Orientation) = V_STRING Then
1276 vOrientations = Array(&quot;PORTRAIT&quot;, &quot;LANDSCAPE&quot;)
1277 If Len(Orientation) &gt; 0 Then
1278 If Not ScriptForge.SF_Utils._Validate(Orientation, &quot;Orientation&quot;, V_STRING, vOrientations) Then GoTo Finally
1279 End If
1280 Else
1281 If Not ScriptForge.SF_Utils._Validate(Orientation, &quot;Orientation&quot;, V_STRING) Then GoTo Finally
1282 End If
1283 If VarType(PaperFormat) = V_STRING Then
1284 vPaperFormats = Array(&quot;A3&quot;, &quot;A4&quot;, &quot;A5&quot;, &quot;B4&quot;, &quot;B5&quot;, &quot;LETTER&quot;, &quot;LEGAL&quot;, &quot;TABLOID&quot;)
1285 If Len(PaperFormat) &gt; 0 Then
1286 If Not ScriptForge.SF_Utils._Validate(PaperFormat, &quot;PaperFormat&quot;, V_STRING, vPaperFormats) Then GoTo Finally
1287 End If
1288 Else
1289 If Not ScriptForge.SF_Utils._Validate(PaperFormat, &quot;PaperFormat&quot;, V_STRING) Then GoTo Finally
1290 End If
1292 Try:
1293 With _PrintComponent
1294 Set oPropertyValue = ScriptForge.SF_Utils._MakePropertyValue(&quot;Name&quot;, Iif(Len(Printer) &gt; 0, Printer, vPrinters(0)))
1295 vPrinterSettings = Array(oPropertyValue)
1296 If Len(Orientation) &gt; 0 Then
1297 vPrinterSettings = ScriptForge.SF_Utils._SetPropertyValue(vPrinterSettings, &quot;PaperOrientation&quot; _
1298 , ScriptForge.SF_Array.IndexOf(vOrientations, Orientation, CaseSensitive := False))
1299 End If
1300 If Len(PaperFormat) &gt; 0 Then
1301 vPrinterSettings = ScriptForge.SF_Utils._SetPropertyValue(vPrinterSettings, &quot;PaperFormat&quot; _
1302 , ScriptForge.SF_Array.IndexOf(vPaperFormats, PaperFormat, CaseSensitive := False))
1303 End If
1304 .setPrinter(vPrinterSettings)
1305 End With
1306 bPrinter = True
1308 Finally:
1309 SetPrinter = bPrinter
1310 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
1311 Exit Function
1312 Catch:
1313 GoTo Finally
1314 End Function &apos; SFDocuments.SF_Document.SetPrinter
1316 REM -----------------------------------------------------------------------------
1317 Private Function SetProperty(Optional ByVal psProperty As String _
1318 , Optional ByVal pvValue As Variant _
1319 ) As Boolean
1320 &apos;&apos;&apos; Set the new value of the named property
1321 &apos;&apos;&apos; Args:
1322 &apos;&apos;&apos; psProperty: the name of the property
1323 &apos;&apos;&apos; pvValue: the new value of the given property
1324 &apos;&apos;&apos; Returns:
1325 &apos;&apos;&apos; True if successful
1327 Dim bSet As Boolean &apos; Return value
1328 Static oSession As Object &apos; Alias of SF_Session
1329 Dim cstThisSub As String
1330 Const cstSubArgs = &quot;Value&quot;
1332 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1333 bSet = False
1335 cstThisSub = &quot;SFDocuments.Document.set&quot; &amp; psProperty
1336 If IsMissing(pvValue) Then pvValue = Empty
1337 &apos;ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) &apos; Validation done in Property Lets
1339 If IsNull(oSession) Then Set oSession = ScriptForge.SF_Services.CreateScriptService(&quot;Session&quot;)
1340 bSet = True
1341 Select Case UCase(psProperty)
1342 Case UCase(&quot;CustomProperties&quot;)
1343 CustomProperties = pvValue
1344 Case UCase(&quot;Description&quot;)
1345 Description = pvValue
1346 Case UCase(&quot;Keywords&quot;)
1347 Keywords = pvValue
1348 Case UCase(&quot;Subject&quot;)
1349 Subject = pvValue
1350 Case UCase(&quot;Title&quot;)
1351 Title = pvValue
1352 Case Else
1353 bSet = False
1354 End Select
1356 Finally:
1357 SetProperty = bSet
1358 &apos;ScriptForge.SF_Utils._ExitFunction(cstThisSub)
1359 Exit Function
1360 Catch:
1361 GoTo Finally
1362 End Function &apos; SFDocuments.SF_Document.SetProperty
1364 REM -----------------------------------------------------------------------------
1365 Public Function Toolbars(Optional ByVal ToolbarName As Variant) As Variant
1366 &apos;&apos;&apos; Returns either a list of the available toolbar names in the actual document
1367 &apos;&apos;&apos; or a Toolbar object instance.
1368 &apos;&apos;&apos; Args:
1369 &apos;&apos;&apos; ToolbarName: the usual name of one of the available toolbars
1370 &apos;&apos;&apos; Returns:
1371 &apos;&apos;&apos; A zero-based array of toolbar names when the argument is absent,
1372 &apos;&apos;&apos; or a new Toolbar object instance from the SF_Widgets library.
1374 Const cstThisSub = &quot;SFDocuments.Document.Toolbars&quot;
1375 Const cstSubArgs = &quot;[ToolbarName=&quot;&quot;&quot;&quot;]&quot;
1377 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
1379 Check:
1380 If IsMissing(ToolbarName) Or IsEmpty(ToolbarName) Then ToolbarName = &quot;&quot;
1381 If IsNull(_Toolbars) Then _Toolbars = ScriptForge.SF_UI._ListToolbars(_Component)
1382 If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
1383 If Not _IsStillAlive() Then GoTo Finally
1384 If VarType(ToolbarName) = V_STRING Then
1385 If Len(ToolbarName) &gt; 0 Then
1386 If Not ScriptForge.SF_Utils._Validate(ToolbarName, &quot;ToolbarName&quot;, V_STRING, _Toolbars.Keys()) Then GoTo Finally
1387 End If
1388 Else
1389 If Not ScriptForge.SF_Utils._Validate(ToolbarName, &quot;ToolbarName&quot;, V_STRING) Then GoTo Finally &apos; Manage here the VarType error
1390 End If
1391 End If
1393 Try:
1394 If Len(ToolbarName) = 0 Then
1395 Toolbars = _Toolbars.Keys()
1396 Else
1397 Toolbars = CreateScriptService(&quot;SFWidgets.Toolbar&quot;, _Toolbars.Item(ToolbarName))
1398 End If
1400 Finally:
1401 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
1402 Exit Function
1403 Catch:
1404 GoTo Finally
1405 End Function &apos; SFDocuments.SF_Document.Toolbars
1407 REM =========================================================== PRIVATE FUNCTIONS
1409 REM -----------------------------------------------------------------------------
1410 Private Function _FileIdent() As String
1411 &apos;&apos;&apos; Returns a file identification from the information that is currently available
1412 &apos;&apos;&apos; Useful e.g. for display in error messages
1414 &apos; OS notation is used to avoid presence of &quot;%nn&quot; in error messages and wrong parameter substitutions
1415 _FileIdent = Iif(Len(_WindowFileName) &gt; 0, ConvertFromUrl(_WindowFileName), _WindowTitle)
1417 End Function &apos; SFDocuments.SF_Document._FileIdent
1419 REM -----------------------------------------------------------------------------
1420 Private Function _GetFilterNames(ByVal pbExport As Boolean) As Variant
1421 &apos;&apos;&apos; Returns the list of export (pbExport = True) or import filters
1422 &apos;&apos;&apos; applicable to the current document
1423 &apos;&apos;&apos; Args:
1424 &apos;&apos;&apos; pbExport: True for export, False for import
1425 &apos;&apos;&apos; Returns:
1426 &apos;&apos;&apos; A zero-based array of strings
1428 Dim vFilters As Variant &apos; Return value
1429 Dim sIdentifier As String &apos; Document service, like com.sun.star.text.TextDocument
1430 Dim oFilterFactory As Object &apos; com.sun.star.document.FilterFactory
1431 Dim vAllFilters As Variant &apos; The full list of installed filters
1432 Dim sFilter As String &apos; A single filter name
1433 Dim iCount As Integer &apos; Filters counter
1434 Dim vFilter As Variant &apos; A filter descriptor as an array of Name/Value pairs
1435 Dim sType As String &apos; The filter type to be compared with the document service
1436 Dim lFlags As Long &apos; Read https://wiki.documentfoundation.org/Documentation/DevGuide/Office_Development#Properties_of_a_Filter
1437 Dim bExport As Boolean &apos; Filter valid for export when True
1438 Dim bImport As Boolean &apos; Filter valid for import when True
1439 Dim bImportExport As Boolean &apos; Filter valid both for import and export when True
1441 vFilters = Array()
1442 On Local Error GoTo Finally &apos; Return empty or partial list if error
1444 Try:
1445 sIdentifier = _Component.Identifier
1446 Set oFilterFactory = ScriptForge.SF_Utils._GetUNOService(&quot;FilterFactory&quot;)
1447 vAllFilters = oFilterFactory.getElementNames()
1448 ReDim vFilters(0 To UBound(vAllFilters))
1449 iCount = -1
1451 For Each sFilter In vAllFilters
1452 vFilter = oFilterFactory.getByName(sFilter)
1453 sType = ScriptForge.SF_Utils._GetPropertyValue(vFilter, &quot;DocumentService&quot;)
1454 If sType = sIdentifier Then
1455 lFlags = ScriptForge.SF_Utils._GetPropertyValue(vFilter, &quot;Flags&quot;)
1456 &apos; export: flag is even
1457 &apos; import: flag is odd and flag/2 is even
1458 &apos; import/export: flag is odd and flag/2 is odd
1459 bExport = ( lFlags Mod 2 = 0 )
1460 bImport = ( (lFlags Mod 2 = 1) And ((lFlags \ 2) Mod 2 = 0) )
1461 bImportExport = ( (lFlags Mod 2 = 1) And ((lFlags \ 2) Mod 2 = 1) )
1462 &apos; Select filter ?
1463 If bImportExport _
1464 Or (pbExport And bExport) _
1465 Or (Not pbExport And bImport) Then
1466 iCount = iCount + 1
1467 vFilters(iCount) = sFilter
1468 End If
1469 End If
1470 Next sFilter
1472 If iCount &gt; -1 Then
1473 ReDim Preserve vFilters(0 To iCount)
1474 End If
1476 Finally:
1477 _GetFilterNames = vFilters
1478 Exit Function
1479 End Function &apos; SFDocuments.SF_Document._GetFilterNames
1481 REM -----------------------------------------------------------------------------
1482 Private Function _IsStillAlive(Optional ByVal pbForUpdate As Boolean _
1483 , Optional ByVal pbError As Boolean _
1484 ) As Boolean
1485 &apos;&apos;&apos; Returns True if the document has not been closed manually or incidentally since the last use
1486 &apos;&apos;&apos; If dead the actual instance is disposed. The execution is cancelled when pbError = True (default)
1487 &apos;&apos;&apos; Args:
1488 &apos;&apos;&apos; pbForUpdate: if True (default = False), check additionally if document is open for editing
1489 &apos;&apos;&apos; pbError: if True (default), raise a fatal error
1491 Dim bAlive As Boolean &apos; Return value
1492 Dim sFileName As String &apos; File identification used to display error message
1494 On Local Error GoTo Catch &apos; Anticipate DisposedException errors or alike
1495 If IsMissing(pbForUpdate) Then pbForUpdate = False
1496 If IsMissing(pbError) Then pbError = True
1498 Try:
1499 &apos; Check existence of document
1500 bAlive = Not IsNull(_Frame)
1501 If bAlive Then bAlive = Not IsNull(_Component)
1502 If bAlive Then bAlive = Not IsNull(_Component.CurrentController)
1504 &apos; Check document is not read only
1505 If bAlive And pbForUpdate Then
1506 If _Component.isreadonly() Then GoTo CatchReadonly
1507 End If
1509 Finally:
1510 _IsStillAlive = bAlive
1511 Exit Function
1512 Catch:
1513 bAlive = False
1514 On Error GoTo 0
1515 sFileName = _FileIdent()
1516 Dispose()
1517 If pbError Then ScriptForge.SF_Exception.RaiseFatal(DOCUMENTDEADERROR, sFileName)
1518 GoTo Finally
1519 CatchReadonly:
1520 bAlive = False
1521 If pbError Then ScriptForge.SF_Exception.RaiseFatal(DOCUMENTREADONLYERROR, &quot;Document&quot;, _FileIdent())
1522 GoTo Finally
1523 End Function &apos; SFDocuments.SF_Document._IsStillAlive
1525 REM -----------------------------------------------------------------------------
1526 Private Sub _LoadDocumentProperties()
1527 &apos;&apos;&apos; Create dictionary with document properties as entries/ Custom properties are excluded
1528 &apos;&apos;&apos; Document is presumed still alive
1529 &apos;&apos;&apos; Special values:
1530 &apos;&apos;&apos; Only valid dates are taken
1531 &apos;&apos;&apos; Statistics are exploded in subitems. Subitems are specific to document type
1532 &apos;&apos;&apos; Keywords are joined
1533 &apos;&apos;&apos; Language is aligned on L10N convention la-CO
1535 Dim oProperties As Object &apos; Document properties
1536 Dim vNamedValue As Variant &apos; com.sun.star.beans.NamedValue
1538 If IsNull(_DocumentProperties) Then
1539 Set oProperties = _Component.getDocumentProperties
1540 Set _DocumentProperties = CreateScriptService(&quot;Dictionary&quot;)
1541 With _DocumentProperties
1542 .Add(&quot;Author&quot;, oProperties.Author)
1543 .Add(&quot;AutoloadSecs&quot;, oProperties.AutoloadSecs)
1544 .Add(&quot;AutoloadURL&quot;, oProperties.AutoloadURL)
1545 If oProperties.CreationDate.Year &gt; 0 Then .Add(&quot;CreationDate&quot;, CDateFromUnoDateTime(oProperties.CreationDate))
1546 .Add(&quot;DefaultTarget&quot;, oProperties.DefaultTarget)
1547 .Add(&quot;Description&quot;, oProperties.Description) &apos; The description can be multiline
1548 &apos; DocumentStatistics : number and names of statistics depend on document type
1549 For Each vNamedValue In oProperties.DocumentStatistics
1550 .Add(vNamedValue.Name, vNamedValue.Value)
1551 Next vNamedValue
1552 .Add(&quot;EditingDuration&quot;, oProperties.EditingDuration)
1553 .Add(&quot;Generator&quot;, oProperties.Generator)
1554 .Add(&quot;Keywords&quot;, Join(oProperties.Keywords, &quot;, &quot;))
1555 .Add(&quot;Language&quot;, oProperties.Language.Language &amp; Iif(Len(oProperties.Language.Country) &gt; 0, &quot;-&quot; &amp; oProperties.Language.Country, &quot;&quot;))
1556 If oProperties.ModificationDate.Year &gt; 0 Then .Add(&quot;ModificationDate&quot;, CDateFromUnoDateTime(oProperties.ModificationDate))
1557 If oProperties.PrintDate.Year &gt; 0 Then .Add(&quot;PrintDate&quot;, CDateFromUnoDateTime(oProperties.PrintDate))
1558 .Add(&quot;PrintedBy&quot;, oProperties.PrintedBy)
1559 .Add(&quot;Subject&quot;, oProperties.Subject)
1560 If oProperties.TemplateDate.Year &gt; 0 Then .Add(&quot;TemplateDate&quot;, CDateFromUnoDateTime(oProperties.TemplateDate))
1561 .Add(&quot;TemplateName&quot;, oProperties.TemplateName)
1562 .Add(&quot;TemplateURL&quot;, oProperties.TemplateURL)
1563 .Add(&quot;Title&quot;, oProperties.Title)
1564 End With
1565 End If
1567 End Sub &apos; SFDocuments.SF_Document._LoadDocumentProperties
1569 REM -----------------------------------------------------------------------------
1570 Private Function _PropertyGet(Optional ByVal psProperty As String) As Variant
1571 &apos;&apos;&apos; Return the value of the named property
1572 &apos;&apos;&apos; Args:
1573 &apos;&apos;&apos; psProperty: the name of the property
1575 Dim oProperties As Object &apos; Document or Custom properties
1576 Dim cstThisSub As String
1577 Const cstSubArgs = &quot;&quot;
1579 _PropertyGet = False
1581 Select Case _DocumentType
1582 Case &quot;Calc&quot; : cstThisSub = &quot;SFDocuments.SF_&quot; &amp; _DocumentType &amp; &quot;.get&quot; &amp; psProperty
1583 Case Else : cstThisSub = &quot;SFDocuments.SF_Document.get&quot; &amp; psProperty
1584 End Select
1585 ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
1586 If Not _IsStillAlive() Then GoTo Finally
1588 Select Case psProperty
1589 Case &quot;CustomProperties&quot;
1590 _CustomProperties = CreateScriptService(&quot;Dictionary&quot;) &apos; Always reload as updates could have been done manually by user
1591 _CustomProperties.ImportFromPropertyValues(_Component.getDocumentProperties().UserDefinedProperties.getPropertyValues)
1592 _PropertyGet = _CustomProperties
1593 Case &quot;Description&quot;
1594 _PropertyGet = _Component.DocumentProperties.Description
1595 Case &quot;DocumentProperties&quot;
1596 _LoadDocumentProperties() &apos; Always reload as updates could have been done manually by user
1597 Set _PropertyGet = _DocumentProperties
1598 Case &quot;DocumentType&quot;
1599 _PropertyGet = _DocumentType
1600 Case &quot;ExportFilters&quot;
1601 _PropertyGet = _GetFilterNames(True)
1602 Case &quot;ImportFilters&quot;
1603 _PropertyGet = _GetFilterNames(False)
1604 Case &quot;IsBase&quot;, &quot;IsCalc&quot;, &quot;IsDraw&quot;, &quot;IsFormDocument&quot;, &quot;IsImpress&quot;, &quot;IsMath&quot;, &quot;IsWriter&quot;
1605 _PropertyGet = ( Mid(psProperty, 3) = _DocumentType )
1606 Case &quot;Keywords&quot;
1607 _PropertyGet = Join(_Component.DocumentProperties.Keywords, &quot;, &quot;)
1608 Case &quot;Readonly&quot;
1609 _PropertyGet = _Component.isReadonly()
1610 Case &quot;Subject&quot;
1611 _PropertyGet = _Component.DocumentProperties.Subject
1612 Case &quot;Title&quot;
1613 _PropertyGet = _Component.DocumentProperties.Title
1614 Case &quot;XComponent&quot;
1615 Set _PropertyGet = _Component
1616 Case Else
1617 _PropertyGet = Null
1618 End Select
1620 Finally:
1621 ScriptForge.SF_Utils._ExitFunction(cstThisSub)
1622 Exit Function
1623 End Function &apos; SFDocuments.SF_Document._PropertyGet
1625 REM -----------------------------------------------------------------------------
1626 Private Function _Repr() As String
1627 &apos;&apos;&apos; Convert the SF_Document instance to a readable string, typically for debugging purposes (DebugPrint ...)
1628 &apos;&apos;&apos; Args:
1629 &apos;&apos;&apos; Return:
1630 &apos;&apos;&apos; &quot;[DOCUMENT]: Type - File&quot;
1632 _Repr = &quot;[Document]: &quot; &amp; _DocumentType &amp; &quot; - &quot; &amp; _FileIdent()
1634 End Function &apos; SFDocuments.SF_Document._Repr
1636 REM ============================================ END OF SFDOCUMENTS.SF_DOCUMENT
1637 </script:module>