Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / wizards / source / gimmicks / GetTexts.xba
blob452ea776e4137cc150e3bf2aef1f5807f8bb1561
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <!--
4 * This file is part of the LibreOffice project.
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 * This file incorporates work covered by the following license notice:
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed
14 * with this work for additional information regarding copyright
15 * ownership. The ASF licenses this file to you under the Apache
16 * License, Version 2.0 (the "License"); you may not use this file
17 * except in compliance with the License. You may obtain a copy of
18 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 -->
20 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="GetTexts" script:language="StarBasic">Option Explicit
21 &apos; Macro-Description:
22 &apos; This Macro extracts the strings out of the currently activated document and inserts them into a log document.
23 &apos; The aim of the macro is to provide the programmer an insight into the StarOffice API
24 &apos; It focusses on how document-Objects are accessed.
25 &apos; Therefore not only texts of the document-body are retrieved but also Texts of general
26 &apos; document Objects like, Annotations, charts and general Document Information
28 Public oLogDocument, oLogText, oLogCursor, oLogHeaderStyle, oLogBodyTextStyle as Object
29 Public oDocument as Object
30 Public LogArray(1000) as String
31 Public LogIndex as Integer
32 Public oLocHeaderStyle as Object
34 Sub Main
35 Dim sDocType as String
36 Dim oHyperCursor as Object
37 Dim oCharStyles as Object
38 BasicLibraries.LoadLibrary(&quot;Tools&quot;)
39 On Local Error GoTo NODOCUMENT
40 oDocument = StarDesktop.ActiveFrame.Controller.Model
41 sDocType = GetDocumentType(oDocument)
42 NODOCUMENT:
43 If Err &lt;&gt; 0 Then
44 Msgbox(&quot;This macro extracts all data from the active Writer, Calc or Draw document.&quot; &amp; chr(13) &amp;_
45 &quot;To start this macro you have to activate a document first.&quot; , 16, GetProductName)
46 Exit Sub
47 End If
48 On Local Error Goto 0
50 &apos; Open a new document where all the texts are inserted
51 oLogDocument = CreateNewDocument(&quot;swriter&quot;)
52 If Not IsNull(oLogDocument) Then
53 oLogText = oLogDocument.Text
55 &apos; create and define the character styles of the Log-document
56 oCharStyles = oLogDocument.StyleFamilies.GetByName(&quot;CharacterStyles&quot;)
57 oLogHeaderStyle = oLogDocument.createInstance(&quot;com.sun.star.style.CharacterStyle&quot;)
58 oCharStyles.InsertbyName(&quot;Log Header&quot;, oLogHeaderStyle)
60 oLogHeaderStyle.charWeight = com.sun.star.awt.FontWeight.BOLD
61 oLogBodyTextStyle = oLogDocument.createInstance(&quot;com.sun.star.style.CharacterStyle&quot;)
62 oCharStyles.InsertbyName(&quot;Log Body&quot;, oLogBodyTextStyle)
64 &apos; Insert the title of the activated document as a hyperlink
65 oHyperCursor = oLogText.createTextCursor()
66 oHyperCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
67 oHyperCursor.gotoStart(False)
68 oHyperCursor.HyperLinkURL = oDocument.URL
69 oHyperCursor.HyperLinkTarget = oDocument.URL
70 If oDocument.DocumentProperties.Title &lt;&gt; &quot;&quot; Then
71 oHyperCursor.HyperlinkName = oDocument.DocumentProperties.Title
72 End If
73 oLogText.insertString(oHyperCursor, oDocument.DocumentProperties.Title, False)
74 oLogText.insertControlCharacter(oHyperCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
76 oLogCursor = oLogText.createTextCursor()
77 oLogCursor.GotoEnd(False)
78 &apos; &quot;Switch off&quot; the Hyperlink - Properties
79 oLogCursor.SetPropertyToDefault(&quot;HyperLinkURL&quot;)
80 oLogCursor.SetPropertyToDefault(&quot;HyperLinkTarget&quot;)
81 oLogCursor.SetPropertyToDefault(&quot;HyperLinkName&quot;)
82 LogIndex = 0
84 &apos; Get the Properties of the document
85 GetDocumentProps()
87 Select Case sDocType
88 Case &quot;swriter&quot;
89 GetWriterStrings()
90 Case &quot;scalc&quot;
91 GetCalcStrings()
92 Case &quot;sdraw&quot;, &quot;simpress&quot;
93 GetDrawStrings()
94 Case Else
95 Msgbox(&quot;This macro only works with a Writer, Calc or Draw/Impress document.&quot;, 16, GetProductName())
96 End Select
97 End If
98 End Sub
101 &apos; ***********************************************Calc-Documents**************************************************
103 Sub GetCalcStrings()
104 Dim i, n as integer
105 Dim oSheet as Object
106 Dim SheetName as String
107 Dim oSheets as Object
108 &apos; Create a sequence of all sheets within the document
109 oSheets = oDocument.Sheets
111 For i = 0 to osheets.Count - 1
112 oSheet = osheets.GetbyIndex(i)
113 SheetName = oSheet.Name
114 MakeLogHeadLine(&quot;Sheet No. &quot; &amp; i &amp; &quot;(&quot; &amp; SheetName &amp; &quot;)&quot; )
116 &apos; Check the &quot;body&quot; of the sheet
117 GetCellTexts(oSheet)
119 If oSheet.IsScenario then
120 MakeLogHeadLine(&quot;Scenario Comments from &quot; &amp; SheetName &amp; &quot;&apos;&quot;)
121 WriteStringtoLogFile(osheet.ScenarioComment)
122 End if
124 GetAnnotations(oSheet, &quot;Annotations from &apos;&quot; &amp; SheetName &amp; &quot;&apos;&quot;)
126 GetChartStrings(oSheet, &quot;Charts from &apos;&quot; &amp; SheetName &amp; &quot;&apos;&quot;)
128 GetControlStrings(oSheet.DrawPage, &quot;Controls from &apos;&quot; &amp; SheetName &amp; &quot;&apos;&quot;)
129 Next
131 &apos; Pictures
132 GetCalcGraphicNames()
134 GetNamedRanges()
135 End Sub
138 Sub GetCellTexts(oSheet as Object)
139 Dim BigRange, BigEnum, oCell as Object
140 BigRange = oDocument.CreateInstance(&quot;com.sun.star.sheet.SheetCellRanges&quot;)
141 BigRange.InsertbyName(&quot;&quot;,oSheet)
142 BigEnum = BigRange.GetCells.CreateEnumeration
143 While BigEnum.hasmoreElements
144 oCell = BigEnum.NextElement
145 If oCell.String &lt;&gt; &quot;&quot; And Val(oCell.String) = 0then
146 WriteStringtoLogFile(oCell.String)
147 End If
148 Wend
149 End Sub
152 Sub GetAnnotations(oSheet as Object, HeaderLine as String)
153 Dim oNotes as Object
154 Dim n as Integer
155 oNotes = oSheet.getAnnotations
156 If oNotes.hasElements() then
157 MakeLogHeadLine(HeaderLine)
158 For n = 0 to oNotes.Count-1
159 WriteStringtoLogFile(oNotes.GetbyIndex(n).String)
160 Next
161 End if
162 End Sub
165 Sub GetNamedRanges()
166 Dim i as integer
167 MakeLogHeadLine(&quot;Named Ranges&quot;)
168 For i = 0 To oDocument.NamedRanges.Count - 1
169 WriteStringtoLogFile(oDocument.NamedRanges.GetbyIndex(i).Name)
170 Next
171 End Sub
174 Sub GetCalcGraphicNames()
175 Dim n,m as integer
176 MakeLogHeadLine(&quot;Graphics&quot;)
177 For n = 0 To oDocument.Drawpages.count-1
178 For m = 0 To oDocument.Drawpages.GetbyIndex(n).Count - 1
179 WriteStringtoLogFile(oDocument.DrawPages.GetbyIndex(n).GetbyIndex(m).Text.String)
180 Next m
181 Next n
182 End Sub
185 &apos; ***********************************************Writer-Documents**************************************************
187 Sub GetParagraphTexts(oParaObject as Object, HeadLine as String)
188 Dim ParaEnum as Object
189 Dim oPara as Object
190 Dim oTextPortEnum as Object
191 Dim oTextPortion as Object
192 Dim i as integer
193 Dim oCellNames()
194 Dim oCell as Object
196 MakeLogHeadLine(HeadLine)
197 ParaEnum = oParaObject.Text.CreateEnumeration
199 While ParaEnum.HasMoreElements
200 oPara = ParaEnum.NextElement
202 &apos; Note: The Enumeration ParaEnum lists all tables and Paragraphs.
203 &apos; Therefore we have to find out what kind of object &quot;oPara&quot; actually is
204 If oPara.supportsService(&quot;com.sun.star.text.Paragraph&quot;) Then
205 &apos; &quot;oPara&quot; is a Paragraph
206 oTextPortEnum = oPara.createEnumeration
207 While oTextPortEnum.hasmoreElements
208 oTextPortion = oTextPortEnum.nextElement()
209 WriteStringToLogFile(oTextPortion.String)
210 Wend
211 Else
212 &apos; &quot;oPara&quot; is a table
213 oCellNames = oPara.CellNames
214 For i = 0 To Ubound(oCellNames())
215 If oCellNames(i) &lt;&gt; &quot;&quot; Then
216 oCell = oPara.getCellByName(oCellNames(i))
217 WriteStringToLogFile(oCell.String)
218 End If
219 Next
220 End If
221 Wend
222 End Sub
226 Sub GetChartStrings(oSheet as Object, HeaderLine as String)
227 Dim i as Integer
228 Dim aChartObject as Object
229 Dim aChartDiagram as Object
231 MakeLogHeadLine(HeaderLine)
233 For i = 0 to oSheet.Charts.Count-1
234 aChartObject = oSheet.Charts.GetByIndex(i).EmbeddedObject
235 If aChartObject.HasSubTitle then
236 WriteStringToLogFile(aChartObject.SubTitle.String)
237 End If
239 If aChartObject.HasMainTitle then
240 WriteStringToLogFile(aChartObject.Title.String)
241 End If
243 aChartDiagram = aChartObject.Diagram
245 If aChartDiagram.hasXAxisTitle Then
246 WriteStringToLogFile(aChartDiagram.XAxisTitle)
247 End If
249 If aChartDiagram.hasYAxisTitle Then
250 WriteStringToLogFile(aChartDiagram.YAxisTitle)
251 End If
253 If aChartDiagram.hasZAxisTitle Then
254 WriteStringToLogFile(aChartDiagram.ZAxisTitle)
255 End If
256 Next i
257 End Sub
261 Sub GetFrameTexts()
262 Dim i as integer
263 Dim oTextFrame as object
264 Dim oFrameEnum as Object
265 Dim oFramePort as Object
266 Dim oFrameTextEnum as Object
267 Dim oFrameTextPort as Object
269 MakeLogHeadLine(&quot;Text Frames&quot;)
270 For i = 0 to oDocument.TextFrames.Count-1
271 oTextFrame = oDocument.TextFrames.GetbyIndex(i)
272 WriteStringToLogFile(oTextFrame.Name)
274 &apos; Is the frame bound to the Page
275 If oTextFrame.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE Then
276 GetParagraphTexts(oTextFrame, &quot;Text Frame Contents&quot;)
277 End If
279 oFrameEnum = oTextFrame.CreateEnumeration
280 While oFrameEnum.HasMoreElements
281 oFramePort = oFrameEnum.NextElement
282 If oFramePort.supportsService(&quot;com.sun.star.text.Paragraph&quot;) then
283 oFrameTextEnum = oFramePort.createEnumeration
284 While oFrameTextEnum.HasMoreElements
285 oFrameTextPort = oFrameTextEnum.NextElement
286 If oFrameTextPort.SupportsService(&quot;com.sun.star.text.TextFrame&quot;) Then
287 WriteStringtoLogFile(oFrameTextPort.String)
288 End If
289 Wend
290 Else
291 WriteStringtoLogFile(oFramePort.Name)
292 End if
293 Wend
294 Next
295 End Sub
298 Sub GetTextFieldStrings()
299 Dim aTextField as Object
300 Dim i as integer
301 Dim CurElement as Object
302 MakeLogHeadLine(&quot;Text Fields&quot;)
303 aTextfield = oDocument.getTextfields.CreateEnumeration
304 While aTextField.hasmoreElements
305 CurElement = aTextField.NextElement
306 If CurElement.PropertySetInfo.hasPropertybyName(&quot;Content&quot;) Then
307 WriteStringtoLogFile(CurElement.Content)
308 ElseIf CurElement.PropertySetInfo.hasPropertybyName(&quot;PlaceHolder&quot;) Then
309 WriteStringtoLogFile(CurElement.PlaceHolder)
310 WriteStringtoLogFile(CurElement.Hint)
311 ElseIf Curelement.TextFieldMaster.PropertySetInfo.HasPropertybyName(&quot;Content&quot;) then
312 WriteStringtoLogFile(CurElement.TextFieldMaster.Content)
313 End If
314 Wend
315 End Sub
319 Sub GetLinkedFileNames()
320 Dim oDocSections as Object
321 Dim LinkedFileName as String
322 Dim i as Integer
323 If Right(oDocument.URL,3) = &quot;sgl&quot; Then
324 MakeLogHeadLine(&quot;Sub-documents&quot;)
325 oDocSections = oDocument.TextSections
326 For i = 0 to oDocSections.Count - 1
327 LinkedFileName = oDocSections.GetbyIndex(i).FileLink.FileURL
328 If LinkedFileName &lt;&gt; &quot;&quot; Then
329 WriteStringToLogFile(LinkedFileName)
330 End If
331 Next i
332 End If
333 End Sub
336 Sub GetSectionNames()
337 Dim i as integer
338 Dim oDocSections as Object
339 MakeLogHeadLine(&quot;Sections&quot;)
340 oDocSections = oDocument.TextSections
341 For i = 0 to oDocSections.Count-1
342 WriteStringtoLogFile(oDocSections.GetbyIndex(i).Name)
343 Next
344 End Sub
347 Sub GetWriterStrings()
348 GetParagraphTexts(oDocument, &quot;Document Body&quot;)
349 GetGraphicNames()
350 GetStyles()
351 GetControlStrings(oDocument.DrawPage, &quot;Controls&quot;)
352 GetTextFieldStrings()
353 GetSectionNames()
354 GetFrameTexts()
355 GetHyperLinks
356 GetLinkedFileNames()
357 End Sub
360 &apos; ***********************************************Draw-Documents**************************************************
362 Sub GetDrawPageTitles(LocObject as Object)
363 Dim n as integer
364 Dim oPage as Object
366 For n = 0 to LocObject.Count - 1
367 oPage = LocObject.GetbyIndex(n)
368 WriteStringtoLogFile(oPage.Name)
369 &apos; Is the Page a DrawPage and not a MasterPage?
370 If oPage.supportsService(&quot;com.sun.star.drawing.DrawPage&quot;)then
371 &apos; Get the Name of the NotesPage (only relevant for Impress-Documents)
372 If oDocument.supportsService(&quot;com.sun.star.presentation.PresentationDocument&quot;) then
373 WriteStringtoLogFile(oPage.NotesPage.Name)
374 End If
375 End If
376 Next
377 End Sub
380 Sub GetPageStrings(oPages as Object)
381 Dim m, n, s as Integer
382 Dim oPage, oPageElement, oShape as Object
383 For n = 0 to oPages.Count-1
384 oPage = oPages.GetbyIndex(n)
385 If oPage.HasElements then
386 For m = 0 to oPage.Count-1
387 oPageElement = oPage.GetByIndex(m)
388 If HasUnoInterfaces(oPageElement,&quot;com.sun.star.container.XIndexAccess&quot;) Then
389 &apos; The Object &quot;oPageElement&quot; a group of Shapes, that can be accessed by their index
390 For s = 0 To oPageElement.Count - 1
391 WriteStringToLogFile(oPageElement.GetByIndex(s).String)
392 Next s
393 ElseIf HasUnoInterfaces(oPageElement, &quot;com.sun.star.text.XText&quot;) Then
394 WriteStringtoLogFile(oPageElement.String)
395 End If
396 Next
397 End If
398 Next
399 End Sub
402 Sub GetDrawStrings()
403 Dim oDPages, oMPages as Object
405 oDPages = oDocument.DrawPages
406 oMPages = oDocument.Masterpages
408 MakeLogHeadLine(&quot;Titles&quot;)
409 GetDrawPageTitles(oDPages)
410 GetDrawPageTitles(oMPages)
412 MakeLogHeadLine(&quot;Document Body&quot;)
413 GetPageStrings(oDPages)
414 GetPageStrings(oMPages)
415 End Sub
418 &apos; ***********************************************Misc**************************************************
420 Sub GetDocumentProps()
421 Dim oDocuProps as Object
422 MakeLogHeadLine(&quot;Document Properties&quot;)
423 oDocuProps = oDocument.DocumentProperties
424 WriteStringToLogFile(oDocuProps.Title)
425 WriteStringToLogFile(oDocuProps.Description)
426 WriteStringToLogFile(oDocuProps.Subject)
427 WriteStringToLogFile(oDocuProps.Author)
428 &apos; WriteStringToLogFile(oDocuProps.UserDefinedProperties.ReplyTo)
429 &apos; WriteStringToLogFile(oDocuProps.UserDefinedProperties.Recipient)
430 &apos; WriteStringToLogFile(oDocuProps.UserDefinedProperties.References)
431 &apos; WriteStringToLogFile(oDocuProps.Keywords)
432 End Sub
435 Sub GetHyperlinks()
436 Dim i as integer
437 Dim oCrsr as Object
438 Dim oAllHyperLinks as Object
439 Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue
440 Dim oSearchDesc as Object
442 MakeLogHeadLine(&quot;Hyperlinks&quot;)
443 &apos; create a Search-Descriptor
444 oSearchDesc = oDocument.CreateSearchDescriptor
445 oSearchDesc.Valuesearch = False
447 &apos; define the Search-attributes
448 srchattributes(0).Name = &quot;HyperLinkURL&quot;
449 srchattributes(0).Value = &quot;&quot;
450 oSearchDesc.SetSearchAttributes(SrchAttributes())
452 oAllHyperLinks = oDocument.findAll(oSearchDesc())
454 For i = 0 to oAllHyperLinks.Count - 1
455 oFound = oAllHyperLinks(i)
456 oCrsr = oFound.Text.createTextCursorByRange(oFound)
457 WriteStringToLogFile(oCrs.HyperLinkURL) &apos;Url
458 WriteStringToLogFile(oCrs.HyperLinkTarget) &apos;Name
459 WriteStringToLogFile(oCrs.HyperLinkName) &apos;Frame
460 Next i
461 End Sub
464 Sub GetGraphicNames()
465 Dim i as integer
466 Dim oDocGraphics as Object
467 MakeLogHeadLine(&quot;Graphics&quot;)
468 oDocGraphics = oDocument.GraphicObjects
469 For i = 0 to oDocGraphics.count - 1
470 WriteStringtoLogFile(oDocGraphics.GetbyIndex(i).Name)
471 Next
472 End Sub
475 Sub GetStyles()
476 Dim m,n as integer
477 MakeLogHeadLine(&quot;User-defined Templates&quot;)
479 &apos; Check all StyleFamilies(i.e. PageStyles, ParagraphStyles, CharacterStyles, cellStyles)
480 For n = 0 to oDocument.StyleFamilies.Count - 1
481 For m = 0 to oDocument.StyleFamilies.getbyIndex(n).Count-1
482 If oDocument.StyleFamilies.GetbyIndex(n).getbyIndex(m).IsUserDefined then
483 WriteStringtoLogFile(oDocument.StyleFamilies.GetbyIndex(n).getbyIndex(m).Name)
484 End If
485 Next
486 Next
487 End Sub
490 Sub GetControlStrings(oDPage as Object, HeaderLine as String)
491 Dim aForm as Object
492 Dim m,n as integer
493 MakeLogHeadLine(HeaderLine)
494 &apos;SearchFor all possible Controls
495 For n = 0 to oDPage.Forms.Count - 1
496 aForm = oDPage.Forms(n)
497 For m = 0 to aForm.Count-1
498 GetControlContent(aForm.GetbyIndex(m))
499 Next
500 Next
501 End Sub
504 Sub GetControlContent(LocControl as Object)
505 Dim i as integer
507 If LocControl.PropertySetInfo.HasPropertybyName(&quot;Label&quot;) then
508 WriteStringtoLogFile(LocControl.Label)
510 ElseIf LocControl.SupportsService(&quot;com.sun.star.form.component.ListBox&quot;) then
511 For i = 0 to Ubound(LocControl.StringItemList())
512 WriteStringtoLogFile(LocControl.StringItemList(i))
513 Next
514 End If
515 If LocControl.PropertySetInfo.HasPropertybyName(&quot;HelpText&quot;) then
516 WriteStringtoLogFile(LocControl.Helptext)
517 End If
518 End Sub
520 &apos; ***********************************************LogDocument**************************************************
522 Sub WriteStringtoLogFile( sString as String)
523 If (Not FieldInArray(LogArray(),LogIndex,sString))AND (NOT ISNULL(sString)) Then
524 LogArray(LogIndex) = sString
525 LogIndex = LogIndex + 1
526 oLogText.insertString(oLogCursor,sString,False)
527 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
528 End If
529 End Sub
532 Sub MakeLogHeadLine(HeadText as String)
533 oLogCursor.CharStyleName = &quot;Log Header&quot;
534 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
535 oLogText.insertString(oLogCursor,HeadText,False)
536 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
537 oLogCursor.CharStyleName = &quot;Log Body&quot;
538 End Sub
539 </script:module>