xref: /trunk/main/wizards/source/gimmicks/GetTexts.xba (revision e883eadd19c750cfe26756b7f3a4200f3e440102)
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; Description:
25&apos; This macro extracts the strings out of the currently active document and inserts them into a log document.
26&apos; The aim of the macro is to provide the programmer an insight into the OpenOffice API.
27&apos; It focuses on how document objects are accessed.
28&apos; Therefore 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/Impress 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; Therefore 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
228Sub GetChartStrings(oSheet as Object, HeaderLine as String)
229Dim i as Integer
230Dim aChartObject as Object
231Dim aChartDiagram as Object
232
233    MakeLogHeadLine(HeaderLine)
234
235    For i = 0 to oSheet.Charts.Count-1
236        aChartObject = oSheet.Charts.GetByIndex(i).EmbeddedObject
237        If aChartObject.HasSubTitle then
238            WriteStringToLogFile(aChartObject.SubTitle.String)
239        End If
240
241        If aChartObject.HasMainTitle then
242            WriteStringToLogFile(aChartObject.Title.String)
243        End If
244
245        aChartDiagram = aChartObject.Diagram
246
247        If aChartDiagram.hasXAxisTitle Then
248            WriteStringToLogFile(aChartDiagram.XAxisTitle)
249        End If
250
251        If aChartDiagram.hasYAxisTitle Then
252            WriteStringToLogFile(aChartDiagram.YAxisTitle)
253        End If
254
255        If aChartDiagram.hasZAxisTitle Then
256            WriteStringToLogFile(aChartDiagram.ZAxisTitle)
257        End If
258    Next i
259End Sub
260
261
262Sub GetFrameTexts()
263Dim i as integer
264Dim oTextFrame as object
265Dim oFrameEnum as Object
266Dim oFramePort as Object
267Dim oFrameTextEnum as Object
268Dim oFrameTextPort as Object
269
270    MakeLogHeadLine(&quot;Text Frames&quot;)
271    For i = 0 to oDocument.TextFrames.Count-1
272        oTextFrame = oDocument.TextFrames.GetbyIndex(i)
273        WriteStringToLogFile(oTextFrame.Name)
274
275        &apos; Is the frame bound to the page?
276        If oTextFrame.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE Then
277            GetParagraphTexts(oTextFrame, &quot;Text Frame Contents&quot;)
278        End If
279
280        oFrameEnum = oTextFrame.CreateEnumeration
281        While oFrameEnum.HasMoreElements
282            oFramePort = oFrameEnum.NextElement
283            If oFramePort.supportsService(&quot;com.sun.star.text.Paragraph&quot;) then
284                oFrameTextEnum = oFramePort.createEnumeration
285                While oFrameTextEnum.HasMoreElements
286                    oFrameTextPort = oFrameTextEnum.NextElement
287                    If oFrameTextPort.SupportsService(&quot;com.sun.star.text.TextFrame&quot;) Then
288                        WriteStringtoLogFile(oFrameTextPort.String)
289                    End If
290                Wend
291            Else
292                WriteStringtoLogFile(oFramePort.Name)
293            End if
294        Wend
295    Next
296End Sub
297
298
299Sub GetTextFieldStrings()
300Dim aTextField as Object
301Dim i as integer
302Dim CurElement as Object
303    MakeLogHeadLine(&quot;Text Fields&quot;)
304    aTextfield = oDocument.getTextfields.CreateEnumeration
305    While aTextField.hasmoreElements
306        CurElement = aTextField.NextElement
307        If CurElement.PropertySetInfo.hasPropertybyName(&quot;Content&quot;) Then
308            WriteStringtoLogFile(CurElement.Content)
309        ElseIf CurElement.PropertySetInfo.hasPropertybyName(&quot;PlaceHolder&quot;) Then
310            WriteStringtoLogFile(CurElement.PlaceHolder)
311            WriteStringtoLogFile(CurElement.Hint)
312        ElseIf Curelement.TextFieldMaster.PropertySetInfo.HasPropertybyName(&quot;Content&quot;) then
313            WriteStringtoLogFile(CurElement.TextFieldMaster.Content)
314        End If
315    Wend
316End Sub
317
318
319Sub GetLinkedFileNames()
320Dim oDocSections as Object
321Dim LinkedFileName as String
322Dim 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
333End Sub
334
335
336Sub GetSectionNames()
337Dim i as integer
338Dim 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
344End Sub
345
346
347Sub 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()
357End Sub
358
359
360&apos; ***********************************************Draw/Impress documents**************************************************
361
362Sub GetDrawPageTitles(LocObject as Object)
363Dim n as integer
364Dim oPage as Object
365
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
377End Sub
378
379
380Sub GetPageStrings(oPages as Object)
381Dim m, n, s as Integer
382Dim 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
399End Sub
400
401
402Sub GetDrawStrings()
403Dim oDPages, oMPages as Object
404
405    oDPages = oDocument.DrawPages
406    oMPages = oDocument.Masterpages
407
408    MakeLogHeadLine(&quot;Titles&quot;)
409    GetDrawPageTitles(oDPages)
410    GetDrawPageTitles(oMPages)
411
412    MakeLogHeadLine(&quot;Document Body&quot;)
413    GetPageStrings(oDPages)
414    GetPageStrings(oMPages)
415End Sub
416
417
418&apos; ***********************************************Misc**************************************************
419
420Sub GetDocumentProps()
421Dim 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)
432End Sub
433
434
435Sub GetHyperlinks()
436Dim i as integer
437Dim oCrsr as Object
438Dim oAllHyperLinks as Object
439Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue
440Dim oSearchDesc as Object
441
442    MakeLogHeadLine(&quot;Hyperlinks&quot;)
443    &apos; create a Search-Descriptor
444    oSearchDesc = oDocument.CreateSearchDescriptor
445    oSearchDesc.Valuesearch = False
446
447    &apos; define the Search-attributes
448    srchattributes(0).Name = &quot;HyperLinkURL&quot;
449    srchattributes(0).Value = &quot;&quot;
450    oSearchDesc.SetSearchAttributes(SrchAttributes())
451
452    oAllHyperLinks = oDocument.findAll(oSearchDesc())
453
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
461End Sub
462
463
464Sub GetGraphicNames()
465Dim i as integer
466Dim 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
472End Sub
473
474
475Sub GetStyles()
476Dim m,n as integer
477    MakeLogHeadLine(&quot;User-defined Templates&quot;)
478
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
487End Sub
488
489
490Sub GetControlStrings(oDPage as Object, HeaderLine as String)
491Dim aForm as Object
492Dim 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
501End Sub
502
503
504Sub GetControlContent(LocControl as Object)
505Dim i as integer
506
507    If LocControl.PropertySetInfo.HasPropertybyName(&quot;Label&quot;) then
508        WriteStringtoLogFile(LocControl.Label)
509
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
518End Sub
519
520&apos; ***********************************************Log document**************************************************
521
522Sub 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
529End Sub
530
531
532Sub 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;
538End Sub
539</script:module>
540