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