1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
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 .
20 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"GetTexts" script:
language=
"StarBasic">Option Explicit
22 ' This macro extracts the strings out of the currently active document and inserts them into a log document.
23 ' The aim of the macro is to provide the programmer an insight into the OpenOffice API.
24 ' It focuses on how document objects are accessed.
25 ' Therefore not only texts of the document body are retrieved but also texts of general
26 ' 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
35 Dim sDocType as String
36 Dim oHyperCursor as Object
37 Dim oCharStyles as Object
38 BasicLibraries.LoadLibrary(
"Tools
")
39 On Local Error GoTo NODOCUMENT
40 oDocument = StarDesktop.ActiveFrame.Controller.Model
41 sDocType = GetDocumentType(oDocument)
43 If Err
<> 0 Then
44 Msgbox(
"This macro extracts all data from the active Writer, Calc or Draw/Impress document.
" & chr(
13)
&_
45 "To start this macro you have to activate a document first.
" ,
16, GetProductName)
50 ' Open a new document where all the texts are inserted
51 oLogDocument = CreateNewDocument(
"swriter
")
52 If Not IsNull(oLogDocument) Then
53 oLogText = oLogDocument.Text
55 ' create and define the character styles of the log document
56 oCharStyles = oLogDocument.StyleFamilies.GetByName(
"CharacterStyles
")
57 oLogHeaderStyle = oLogDocument.createInstance(
"com.sun.star.style.CharacterStyle
")
58 oCharStyles.InsertbyName(
"Log Header
", oLogHeaderStyle)
60 oLogHeaderStyle.charWeight = com.sun.star.awt.FontWeight.BOLD
61 oLogBodyTextStyle = oLogDocument.createInstance(
"com.sun.star.style.CharacterStyle
")
62 oCharStyles.InsertbyName(
"Log Body
", oLogBodyTextStyle)
64 ' 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
<> "" Then
71 oHyperCursor.HyperlinkName = oDocument.DocumentProperties.Title
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 ' "Switch off
" the Hyperlink - Properties
79 oLogCursor.SetPropertyToDefault(
"HyperLinkURL
")
80 oLogCursor.SetPropertyToDefault(
"HyperLinkTarget
")
81 oLogCursor.SetPropertyToDefault(
"HyperLinkName
")
84 ' Get the Properties of the document
88 Case
"swriter
"
90 Case
"scalc
"
92 Case
"sdraw
",
"simpress
"
95 Msgbox(
"This macro only works with a Writer, Calc or Draw/Impress document.
",
16, GetProductName())
101 ' ***********************************************Calc documents**************************************************
106 Dim SheetName as String
107 Dim oSheets as Object
108 ' 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(
"Sheet No.
" & i
& " (
" & SheetName
& ")
" )
116 ' Check the
"body
" of the sheet
119 If oSheet.IsScenario then
120 MakeLogHeadLine(
"Scenario Comments from
" & SheetName
& "'")
121 WriteStringtoLogFile(osheet.ScenarioComment)
124 GetAnnotations(oSheet,
"Annotations from
'" & SheetName
& "'")
126 GetChartStrings(oSheet,
"Charts from
'" & SheetName
& "'")
128 GetControlStrings(oSheet.DrawPage,
"Controls from
'" & SheetName
& "'")
132 GetCalcGraphicNames()
138 Sub GetCellTexts(oSheet as Object)
139 Dim BigRange, BigEnum, oCell as Object
140 BigRange = oDocument.CreateInstance(
"com.sun.star.sheet.SheetCellRanges
")
141 BigRange.InsertbyName(
"",oSheet)
142 BigEnum = BigRange.GetCells.CreateEnumeration
143 While BigEnum.hasmoreElements
144 oCell = BigEnum.NextElement
145 If oCell.String
<> "" And Val(oCell.String) =
0then
146 WriteStringtoLogFile(oCell.String)
152 Sub GetAnnotations(oSheet as Object, HeaderLine as String)
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)
167 MakeLogHeadLine(
"Named Ranges
")
168 For i =
0 To oDocument.NamedRanges.Count -
1
169 WriteStringtoLogFile(oDocument.NamedRanges.GetbyIndex(i).Name)
174 Sub GetCalcGraphicNames()
176 MakeLogHeadLine(
"Graphics
")
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)
185 ' ***********************************************Writer documents**************************************************
187 Sub GetParagraphTexts(oParaObject as Object, HeadLine as String)
188 Dim ParaEnum as Object
190 Dim oTextPortEnum as Object
191 Dim oTextPortion as Object
196 MakeLogHeadLine(HeadLine)
197 ParaEnum = oParaObject.Text.CreateEnumeration
199 While ParaEnum.HasMoreElements
200 oPara = ParaEnum.NextElement
202 ' Note: The enumeration ParaEnum lists all tables and paragraphs.
203 ' Therefore we have to find out what kind of object
"oPara
" actually is
204 If oPara.supportsService(
"com.sun.star.text.Paragraph
") Then
205 ' "oPara
" is a Paragraph
206 oTextPortEnum = oPara.createEnumeration
207 While oTextPortEnum.hasmoreElements
208 oTextPortion = oTextPortEnum.nextElement()
209 WriteStringToLogFile(oTextPortion.String)
212 ' "oPara
" is a table
213 oCellNames = oPara.CellNames
214 For i =
0 To Ubound(oCellNames())
215 If oCellNames(i)
<> "" Then
216 oCell = oPara.getCellByName(oCellNames(i))
217 WriteStringToLogFile(oCell.String)
225 Sub GetChartStrings(oSheet as Object, HeaderLine as String)
227 Dim aChartObject as Object
228 Dim aChartDiagram as Object
230 MakeLogHeadLine(HeaderLine)
232 For i =
0 to oSheet.Charts.Count-
1
233 aChartObject = oSheet.Charts.GetByIndex(i).EmbeddedObject
234 If aChartObject.HasSubTitle then
235 WriteStringToLogFile(aChartObject.SubTitle.String)
238 If aChartObject.HasMainTitle then
239 WriteStringToLogFile(aChartObject.Title.String)
242 aChartDiagram = aChartObject.Diagram
244 If aChartDiagram.hasXAxisTitle Then
245 WriteStringToLogFile(aChartDiagram.XAxisTitle)
248 If aChartDiagram.hasYAxisTitle Then
249 WriteStringToLogFile(aChartDiagram.YAxisTitle)
252 If aChartDiagram.hasZAxisTitle Then
253 WriteStringToLogFile(aChartDiagram.ZAxisTitle)
261 Dim oTextFrame as object
262 Dim oFrameEnum as Object
263 Dim oFramePort as Object
264 Dim oFrameTextEnum as Object
265 Dim oFrameTextPort as Object
267 MakeLogHeadLine(
"Text Frames
")
268 For i =
0 to oDocument.TextFrames.Count-
1
269 oTextFrame = oDocument.TextFrames.GetbyIndex(i)
270 WriteStringToLogFile(oTextFrame.Name)
272 ' Is the frame bound to the page?
273 If oTextFrame.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE Then
274 GetParagraphTexts(oTextFrame,
"Text Frame Contents
")
277 oFrameEnum = oTextFrame.CreateEnumeration
278 While oFrameEnum.HasMoreElements
279 oFramePort = oFrameEnum.NextElement
280 If oFramePort.supportsService(
"com.sun.star.text.Paragraph
") then
281 oFrameTextEnum = oFramePort.createEnumeration
282 While oFrameTextEnum.HasMoreElements
283 oFrameTextPort = oFrameTextEnum.NextElement
284 If oFrameTextPort.SupportsService(
"com.sun.star.text.TextFrame
") Then
285 WriteStringtoLogFile(oFrameTextPort.String)
289 WriteStringtoLogFile(oFramePort.Name)
296 Sub GetTextFieldStrings()
297 Dim aTextField as Object
299 Dim CurElement as Object
300 MakeLogHeadLine(
"Text Fields
")
301 aTextfield = oDocument.getTextfields.CreateEnumeration
302 While aTextField.hasmoreElements
303 CurElement = aTextField.NextElement
304 If CurElement.PropertySetInfo.hasPropertybyName(
"Content
") Then
305 WriteStringtoLogFile(CurElement.Content)
306 ElseIf CurElement.PropertySetInfo.hasPropertybyName(
"PlaceHolder
") Then
307 WriteStringtoLogFile(CurElement.PlaceHolder)
308 WriteStringtoLogFile(CurElement.Hint)
309 ElseIf Curelement.TextFieldMaster.PropertySetInfo.HasPropertybyName(
"Content
") then
310 WriteStringtoLogFile(CurElement.TextFieldMaster.Content)
316 Sub GetLinkedFileNames()
317 Dim oDocSections as Object
318 Dim LinkedFileName as String
320 If Right(oDocument.URL,
3) =
"sgl
" Then
321 MakeLogHeadLine(
"Sub-documents
")
322 oDocSections = oDocument.TextSections
323 For i =
0 to oDocSections.Count -
1
324 LinkedFileName = oDocSections.GetbyIndex(i).FileLink.FileURL
325 If LinkedFileName
<> "" Then
326 WriteStringToLogFile(LinkedFileName)
333 Sub GetSectionNames()
335 Dim oDocSections as Object
336 MakeLogHeadLine(
"Sections
")
337 oDocSections = oDocument.TextSections
338 For i =
0 to oDocSections.Count-
1
339 WriteStringtoLogFile(oDocSections.GetbyIndex(i).Name)
344 Sub GetWriterStrings()
345 GetParagraphTexts(oDocument,
"Document Body
")
348 GetControlStrings(oDocument.DrawPage,
"Controls
")
349 GetTextFieldStrings()
357 ' ***********************************************Draw/Impress documents**************************************************
359 Sub GetDrawPageTitles(LocObject as Object)
363 For n =
0 to LocObject.Count -
1
364 oPage = LocObject.GetbyIndex(n)
365 WriteStringtoLogFile(oPage.Name)
366 ' Is the page a DrawPage and not a MasterPage?
367 If oPage.supportsService(
"com.sun.star.drawing.DrawPage
")then
368 ' Get the name of the NotesPage (only relevant for Impress documents)
369 If oDocument.supportsService(
"com.sun.star.presentation.PresentationDocument
") then
370 WriteStringtoLogFile(oPage.NotesPage.Name)
377 Sub GetPageStrings(oPages as Object)
378 Dim m, n, s as Integer
379 Dim oPage, oPageElement, oShape as Object
380 For n =
0 to oPages.Count-
1
381 oPage = oPages.GetbyIndex(n)
382 If oPage.HasElements then
383 For m =
0 to oPage.Count-
1
384 oPageElement = oPage.GetByIndex(m)
385 If HasUnoInterfaces(oPageElement,
"com.sun.star.container.XIndexAccess
") Then
386 ' The Object
"oPageElement
" a group of Shapes, that can be accessed by their index
387 For s =
0 To oPageElement.Count -
1
388 WriteStringToLogFile(oPageElement.GetByIndex(s).String)
390 ElseIf HasUnoInterfaces(oPageElement,
"com.sun.star.text.XText
") Then
391 WriteStringtoLogFile(oPageElement.String)
400 Dim oDPages, oMPages as Object
402 oDPages = oDocument.DrawPages
403 oMPages = oDocument.Masterpages
405 MakeLogHeadLine(
"Titles
")
406 GetDrawPageTitles(oDPages)
407 GetDrawPageTitles(oMPages)
409 MakeLogHeadLine(
"Document Body
")
410 GetPageStrings(oDPages)
411 GetPageStrings(oMPages)
415 ' ***********************************************Misc**************************************************
417 Sub GetDocumentProps()
418 Dim oDocuProps as Object
419 MakeLogHeadLine(
"Document Properties
")
420 oDocuProps = oDocument.DocumentProperties
421 WriteStringToLogFile(oDocuProps.Title)
422 WriteStringToLogFile(oDocuProps.Description)
423 WriteStringToLogFile(oDocuProps.Subject)
424 WriteStringToLogFile(oDocuProps.Author)
425 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.ReplyTo)
426 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.Recipient)
427 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.References)
428 ' WriteStringToLogFile(oDocuProps.Keywords)
435 Dim oAllHyperLinks as Object
436 Dim SrchAttributes(
0) as new com.sun.star.beans.PropertyValue
437 Dim oSearchDesc as Object
439 MakeLogHeadLine(
"Hyperlinks
")
440 ' create a Search-Descriptor
441 oSearchDesc = oDocument.CreateSearchDescriptor
442 oSearchDesc.Valuesearch = False
444 ' define the Search-attributes
445 srchattributes(
0).Name =
"HyperLinkURL
"
446 srchattributes(
0).Value =
""
447 oSearchDesc.SetSearchAttributes(SrchAttributes())
449 oAllHyperLinks = oDocument.findAll(oSearchDesc())
451 For i =
0 to oAllHyperLinks.Count -
1
452 oFound = oAllHyperLinks(i)
453 oCrsr = oFound.Text.createTextCursorByRange(oFound)
454 WriteStringToLogFile(oCrs.HyperLinkURL)
'Url
455 WriteStringToLogFile(oCrs.HyperLinkTarget)
'Name
456 WriteStringToLogFile(oCrs.HyperLinkName)
'Frame
461 Sub GetGraphicNames()
463 Dim oDocGraphics as Object
464 MakeLogHeadLine(
"Graphics
")
465 oDocGraphics = oDocument.GraphicObjects
466 For i =
0 to oDocGraphics.count -
1
467 WriteStringtoLogFile(oDocGraphics.GetbyIndex(i).Name)
474 MakeLogHeadLine(
"User-defined Templates
")
476 ' Check all StyleFamilies(i.e. PageStyles, ParagraphStyles, CharacterStyles, cellStyles)
477 For n =
0 to oDocument.StyleFamilies.Count -
1
478 For m =
0 to oDocument.StyleFamilies.getbyIndex(n).Count-
1
479 If oDocument.StyleFamilies.GetbyIndex(n).getbyIndex(m).IsUserDefined then
480 WriteStringtoLogFile(oDocument.StyleFamilies.GetbyIndex(n).getbyIndex(m).Name)
487 Sub GetControlStrings(oDPage as Object, HeaderLine as String)
490 MakeLogHeadLine(HeaderLine)
491 'SearchFor all possible Controls
492 For n =
0 to oDPage.Forms.Count -
1
493 aForm = oDPage.Forms(n)
494 For m =
0 to aForm.Count-
1
495 GetControlContent(aForm.GetbyIndex(m))
501 Sub GetControlContent(LocControl as Object)
504 If LocControl.PropertySetInfo.HasPropertybyName(
"Label
") then
505 WriteStringtoLogFile(LocControl.Label)
507 ElseIf LocControl.SupportsService(
"com.sun.star.form.component.ListBox
") then
508 For i =
0 to Ubound(LocControl.StringItemList())
509 WriteStringtoLogFile(LocControl.StringItemList(i))
512 If LocControl.PropertySetInfo.HasPropertybyName(
"HelpText
") then
513 WriteStringtoLogFile(LocControl.Helptext)
517 ' ***********************************************Log document**************************************************
519 Sub WriteStringtoLogFile( sString as String)
520 If (Not FieldInArray(LogArray(),LogIndex,sString))AND (NOT ISNULL(sString)) Then
521 LogArray(LogIndex) = sString
522 LogIndex = LogIndex +
1
523 oLogText.insertString(oLogCursor,sString,False)
524 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
529 Sub MakeLogHeadLine(HeadText as String)
530 oLogCursor.CharStyleName =
"Log Header
"
531 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
532 oLogText.insertString(oLogCursor,HeadText,False)
533 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
534 oLogCursor.CharStyleName =
"Log Body
"