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