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
21 ' Macro-Description:
22 ' This Macro extracts the strings out of the currently activated document and inserts them into a log document.
23 ' The aim of the macro is to provide the programmer an insight into the StarOffice API
24 ' It focusses 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 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)
226 Sub GetChartStrings(oSheet as Object, HeaderLine as String)
228 Dim aChartObject as Object
229 Dim aChartDiagram as Object
231 MakeLogHeadLine(HeaderLine)
233 For i =
0 to oSheet.Charts.Count-
1
234 aChartObject = oSheet.Charts.GetByIndex(i).EmbeddedObject
235 If aChartObject.HasSubTitle then
236 WriteStringToLogFile(aChartObject.SubTitle.String)
239 If aChartObject.HasMainTitle then
240 WriteStringToLogFile(aChartObject.Title.String)
243 aChartDiagram = aChartObject.Diagram
245 If aChartDiagram.hasXAxisTitle Then
246 WriteStringToLogFile(aChartDiagram.XAxisTitle)
249 If aChartDiagram.hasYAxisTitle Then
250 WriteStringToLogFile(aChartDiagram.YAxisTitle)
253 If aChartDiagram.hasZAxisTitle Then
254 WriteStringToLogFile(aChartDiagram.ZAxisTitle)
263 Dim oTextFrame as object
264 Dim oFrameEnum as Object
265 Dim oFramePort as Object
266 Dim oFrameTextEnum as Object
267 Dim oFrameTextPort as Object
269 MakeLogHeadLine(
"Text Frames
")
270 For i =
0 to oDocument.TextFrames.Count-
1
271 oTextFrame = oDocument.TextFrames.GetbyIndex(i)
272 WriteStringToLogFile(oTextFrame.Name)
274 ' Is the frame bound to the Page
275 If oTextFrame.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE Then
276 GetParagraphTexts(oTextFrame,
"Text Frame Contents
")
279 oFrameEnum = oTextFrame.CreateEnumeration
280 While oFrameEnum.HasMoreElements
281 oFramePort = oFrameEnum.NextElement
282 If oFramePort.supportsService(
"com.sun.star.text.Paragraph
") then
283 oFrameTextEnum = oFramePort.createEnumeration
284 While oFrameTextEnum.HasMoreElements
285 oFrameTextPort = oFrameTextEnum.NextElement
286 If oFrameTextPort.SupportsService(
"com.sun.star.text.TextFrame
") Then
287 WriteStringtoLogFile(oFrameTextPort.String)
291 WriteStringtoLogFile(oFramePort.Name)
298 Sub GetTextFieldStrings()
299 Dim aTextField as Object
301 Dim CurElement as Object
302 MakeLogHeadLine(
"Text Fields
")
303 aTextfield = oDocument.getTextfields.CreateEnumeration
304 While aTextField.hasmoreElements
305 CurElement = aTextField.NextElement
306 If CurElement.PropertySetInfo.hasPropertybyName(
"Content
") Then
307 WriteStringtoLogFile(CurElement.Content)
308 ElseIf CurElement.PropertySetInfo.hasPropertybyName(
"PlaceHolder
") Then
309 WriteStringtoLogFile(CurElement.PlaceHolder)
310 WriteStringtoLogFile(CurElement.Hint)
311 ElseIf Curelement.TextFieldMaster.PropertySetInfo.HasPropertybyName(
"Content
") then
312 WriteStringtoLogFile(CurElement.TextFieldMaster.Content)
319 Sub GetLinkedFileNames()
320 Dim oDocSections as Object
321 Dim LinkedFileName as String
323 If Right(oDocument.URL,
3) =
"sgl
" Then
324 MakeLogHeadLine(
"Sub-documents
")
325 oDocSections = oDocument.TextSections
326 For i =
0 to oDocSections.Count -
1
327 LinkedFileName = oDocSections.GetbyIndex(i).FileLink.FileURL
328 If LinkedFileName
<> "" Then
329 WriteStringToLogFile(LinkedFileName)
336 Sub GetSectionNames()
338 Dim oDocSections as Object
339 MakeLogHeadLine(
"Sections
")
340 oDocSections = oDocument.TextSections
341 For i =
0 to oDocSections.Count-
1
342 WriteStringtoLogFile(oDocSections.GetbyIndex(i).Name)
347 Sub GetWriterStrings()
348 GetParagraphTexts(oDocument,
"Document Body
")
351 GetControlStrings(oDocument.DrawPage,
"Controls
")
352 GetTextFieldStrings()
360 ' ***********************************************Draw-Documents**************************************************
362 Sub GetDrawPageTitles(LocObject as Object)
366 For n =
0 to LocObject.Count -
1
367 oPage = LocObject.GetbyIndex(n)
368 WriteStringtoLogFile(oPage.Name)
369 ' Is the Page a DrawPage and not a MasterPage?
370 If oPage.supportsService(
"com.sun.star.drawing.DrawPage
")then
371 ' Get the Name of the NotesPage (only relevant for Impress-Documents)
372 If oDocument.supportsService(
"com.sun.star.presentation.PresentationDocument
") then
373 WriteStringtoLogFile(oPage.NotesPage.Name)
380 Sub GetPageStrings(oPages as Object)
381 Dim m, n, s as Integer
382 Dim 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,
"com.sun.star.container.XIndexAccess
") Then
389 ' The Object
"oPageElement
" 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)
393 ElseIf HasUnoInterfaces(oPageElement,
"com.sun.star.text.XText
") Then
394 WriteStringtoLogFile(oPageElement.String)
403 Dim oDPages, oMPages as Object
405 oDPages = oDocument.DrawPages
406 oMPages = oDocument.Masterpages
408 MakeLogHeadLine(
"Titles
")
409 GetDrawPageTitles(oDPages)
410 GetDrawPageTitles(oMPages)
412 MakeLogHeadLine(
"Document Body
")
413 GetPageStrings(oDPages)
414 GetPageStrings(oMPages)
418 ' ***********************************************Misc**************************************************
420 Sub GetDocumentProps()
421 Dim oDocuProps as Object
422 MakeLogHeadLine(
"Document Properties
")
423 oDocuProps = oDocument.DocumentProperties
424 WriteStringToLogFile(oDocuProps.Title)
425 WriteStringToLogFile(oDocuProps.Description)
426 WriteStringToLogFile(oDocuProps.Subject)
427 WriteStringToLogFile(oDocuProps.Author)
428 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.ReplyTo)
429 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.Recipient)
430 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.References)
431 ' WriteStringToLogFile(oDocuProps.Keywords)
438 Dim oAllHyperLinks as Object
439 Dim SrchAttributes(
0) as new com.sun.star.beans.PropertyValue
440 Dim oSearchDesc as Object
442 MakeLogHeadLine(
"Hyperlinks
")
443 ' create a Search-Descriptor
444 oSearchDesc = oDocument.CreateSearchDescriptor
445 oSearchDesc.Valuesearch = False
447 ' define the Search-attributes
448 srchattributes(
0).Name =
"HyperLinkURL
"
449 srchattributes(
0).Value =
""
450 oSearchDesc.SetSearchAttributes(SrchAttributes())
452 oAllHyperLinks = oDocument.findAll(oSearchDesc())
454 For i =
0 to oAllHyperLinks.Count -
1
455 oFound = oAllHyperLinks(i)
456 oCrsr = oFound.Text.createTextCursorByRange(oFound)
457 WriteStringToLogFile(oCrs.HyperLinkURL)
'Url
458 WriteStringToLogFile(oCrs.HyperLinkTarget)
'Name
459 WriteStringToLogFile(oCrs.HyperLinkName)
'Frame
464 Sub GetGraphicNames()
466 Dim oDocGraphics as Object
467 MakeLogHeadLine(
"Graphics
")
468 oDocGraphics = oDocument.GraphicObjects
469 For i =
0 to oDocGraphics.count -
1
470 WriteStringtoLogFile(oDocGraphics.GetbyIndex(i).Name)
477 MakeLogHeadLine(
"User-defined Templates
")
479 ' 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)
490 Sub GetControlStrings(oDPage as Object, HeaderLine as String)
493 MakeLogHeadLine(HeaderLine)
494 '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))
504 Sub GetControlContent(LocControl as Object)
507 If LocControl.PropertySetInfo.HasPropertybyName(
"Label
") then
508 WriteStringtoLogFile(LocControl.Label)
510 ElseIf LocControl.SupportsService(
"com.sun.star.form.component.ListBox
") then
511 For i =
0 to Ubound(LocControl.StringItemList())
512 WriteStringtoLogFile(LocControl.StringItemList(i))
515 If LocControl.PropertySetInfo.HasPropertybyName(
"HelpText
") then
516 WriteStringtoLogFile(LocControl.Helptext)
520 ' ***********************************************LogDocument**************************************************
522 Sub 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)
532 Sub MakeLogHeadLine(HeadText as String)
533 oLogCursor.CharStyleName =
"Log Header
"
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 =
"Log Body
"