Bump for 3.6-28
[LibreOffice.git] / wizards / source / gimmicks / GetTexts.xba
blobc98e7d51f580fc77a9d098e36c547364854a04c7
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
11 Public oLogDocument, oLogText, oLogCursor, oLogHeaderStyle, oLogBodyTextStyle as Object
12 Public oDocument as Object
13 Public LogArray(1000) as String
14 Public LogIndex as Integer
15 Public oLocHeaderStyle as Object
17 Sub Main
18 Dim sDocType as String
19 Dim oHyperCursor as Object
20 Dim 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
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
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)
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)
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)
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
67 &apos; Get the Properties of the document
68 GetDocumentProps()
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
81 End Sub
84 &apos; ***********************************************Calc-Documents**************************************************
86 Sub GetCalcStrings()
87 Dim i, n as integer
88 Dim oSheet as Object
89 Dim SheetName as String
90 Dim oSheets as Object
91 &apos; Create a sequence of all sheets within the document
92 oSheets = oDocument.Sheets
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; )
99 &apos; Check the &quot;body&quot; of the sheet
100 GetCellTexts(oSheet)
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
107 GetAnnotations(oSheet, &quot;Annotations from &apos;&quot; &amp; SheetName &amp; &quot;&apos;&quot;)
109 GetChartStrings(oSheet, &quot;Charts from &apos;&quot; &amp; SheetName &amp; &quot;&apos;&quot;)
111 GetControlStrings(oSheet.DrawPage, &quot;Controls from &apos;&quot; &amp; SheetName &amp; &quot;&apos;&quot;)
112 Next
114 &apos; Pictures
115 GetCalcGraphicNames()
117 GetNamedRanges()
118 End Sub
121 Sub GetCellTexts(oSheet as Object)
122 Dim 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
132 End Sub
135 Sub GetAnnotations(oSheet as Object, HeaderLine as String)
136 Dim oNotes as Object
137 Dim 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
145 End Sub
148 Sub GetNamedRanges()
149 Dim 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
154 End Sub
157 Sub GetCalcGraphicNames()
158 Dim 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
165 End Sub
168 &apos; ***********************************************Writer-Documents**************************************************
170 Sub GetParagraphTexts(oParaObject as Object, HeadLine as String)
171 Dim ParaEnum as Object
172 Dim oPara as Object
173 Dim oTextPortEnum as Object
174 Dim oTextPortion as Object
175 Dim i as integer
176 Dim oCellNames()
177 Dim oCell as Object
179 MakeLogHeadLine(HeadLine)
180 ParaEnum = oParaObject.Text.CreateEnumeration
182 While ParaEnum.HasMoreElements
183 oPara = ParaEnum.NextElement
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
205 End Sub
209 Sub GetChartStrings(oSheet as Object, HeaderLine as String)
210 Dim i as Integer
211 Dim aChartObject as Object
212 Dim aChartDiagram as Object
214 MakeLogHeadLine(HeaderLine)
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
222 If aChartObject.HasMainTitle then
223 WriteStringToLogFile(aChartObject.Title.String)
224 End If
226 aChartDiagram = aChartObject.Diagram
228 If aChartDiagram.hasXAxisTitle Then
229 WriteStringToLogFile(aChartDiagram.XAxisTitle)
230 End If
232 If aChartDiagram.hasYAxisTitle Then
233 WriteStringToLogFile(aChartDiagram.YAxisTitle)
234 End If
236 If aChartDiagram.hasZAxisTitle Then
237 WriteStringToLogFile(aChartDiagram.ZAxisTitle)
238 End If
239 Next i
240 End Sub
244 Sub GetFrameTexts()
245 Dim i as integer
246 Dim oTextFrame as object
247 Dim oFrameEnum as Object
248 Dim oFramePort as Object
249 Dim oFrameTextEnum as Object
250 Dim oFrameTextPort as Object
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)
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
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
278 End Sub
281 Sub GetTextFieldStrings()
282 Dim aTextField as Object
283 Dim i as integer
284 Dim 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
298 End Sub
302 Sub GetLinkedFileNames()
303 Dim oDocSections as Object
304 Dim LinkedFileName as String
305 Dim 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
316 End Sub
319 Sub GetSectionNames()
320 Dim i as integer
321 Dim 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
327 End Sub
330 Sub 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()
340 End Sub
343 &apos; ***********************************************Draw-Documents**************************************************
345 Sub GetDrawPageTitles(LocObject as Object)
346 Dim n as integer
347 Dim oPage as Object
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
360 End Sub
363 Sub GetPageStrings(oPages as Object)
364 Dim m, n, s as Integer
365 Dim 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
382 End Sub
385 Sub GetDrawStrings()
386 Dim oDPages, oMPages as Object
388 oDPages = oDocument.DrawPages
389 oMPages = oDocument.Masterpages
391 MakeLogHeadLine(&quot;Titles&quot;)
392 GetDrawPageTitles(oDPages)
393 GetDrawPageTitles(oMPages)
395 MakeLogHeadLine(&quot;Document Body&quot;)
396 GetPageStrings(oDPages)
397 GetPageStrings(oMPages)
398 End Sub
401 &apos; ***********************************************Misc**************************************************
403 Sub GetDocumentProps()
404 Dim 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)
415 End Sub
418 Sub GetHyperlinks()
419 Dim i as integer
420 Dim oCrsr as Object
421 Dim oAllHyperLinks as Object
422 Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue
423 Dim oSearchDesc as Object
425 MakeLogHeadLine(&quot;Hyperlinks&quot;)
426 &apos; create a Search-Descriptor
427 oSearchDesc = oDocument.CreateSearchDescriptor
428 oSearchDesc.Valuesearch = False
430 &apos; define the Search-attributes
431 srchattributes(0).Name = &quot;HyperLinkURL&quot;
432 srchattributes(0).Value = &quot;&quot;
433 oSearchDesc.SetSearchAttributes(SrchAttributes())
435 oAllHyperLinks = oDocument.findAll(oSearchDesc())
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
444 End Sub
447 Sub GetGraphicNames()
448 Dim i as integer
449 Dim 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
455 End Sub
458 Sub GetStyles()
459 Dim m,n as integer
460 MakeLogHeadLine(&quot;User-defined Templates&quot;)
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
470 End Sub
473 Sub GetControlStrings(oDPage as Object, HeaderLine as String)
474 Dim aForm as Object
475 Dim 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
484 End Sub
487 Sub GetControlContent(LocControl as Object)
488 Dim i as integer
490 If LocControl.PropertySetInfo.HasPropertybyName(&quot;Label&quot;) then
491 WriteStringtoLogFile(LocControl.Label)
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
501 End Sub
503 &apos; ***********************************************LogDocument**************************************************
505 Sub 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
512 End Sub
515 Sub 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;
521 End Sub
522 </script:module>