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 ' Macro-Description:
5 ' This Macro extracts the Strings out of the currently activated document und inserts them into a logdocument
6 ' The aim of the macro is to provide the programmer an insight into the StarOffice API
7 ' It focusses on how document-Objects are accessed.
8 ' Therefor not only texts of the document-body are retrieved but also Texts of general
9 ' 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
18 Dim sDocType as String
19 Dim oHyperCursor as Object
20 Dim oCharStyles as Object
21 BasicLibraries.LoadLibrary(
"Tools
")
22 On Local Error GoTo NODOCUMENT
23 oDocument = StarDesktop.ActiveFrame.Controller.Model
24 sDocType = GetDocumentType(oDocument)
26 If Err
<> 0 Then
27 Msgbox(
"This macro extracts all data from the active Writer, Calc or Draw document.
" & chr(
13)
&_
28 "To start this macro you have to activate a document first.
" ,
16, GetProductName)
33 ' Open a new document where all the texts are inserted
34 oLogDocument = CreateNewDocument(
"swriter
")
35 If Not IsNull(oLogDocument) Then
36 oLogText = oLogDocument.Text
38 ' create and define the character styles of the Log-document
39 oCharStyles = oLogDocument.StyleFamilies.GetByName(
"CharacterStyles
")
40 oLogHeaderStyle = oLogDocument.createInstance(
"com.sun.star.style.CharacterStyle
")
41 oCharStyles.InsertbyName(
"Log Header
", oLogHeaderStyle)
43 oLogHeaderStyle.charWeight = com.sun.star.awt.FontWeight.BOLD
44 oLogBodyTextStyle = oLogDocument.createInstance(
"com.sun.star.style.CharacterStyle
")
45 oCharStyles.InsertbyName(
"Log Body
", oLogBodyTextStyle)
47 ' 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
<> "" Then
54 oHyperCursor.HyperlinkName = oDocument.DocumentProperties.Title
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 ' "Switch off
" the Hyperlink - Properties
62 oLogCursor.SetPropertyToDefault(
"HyperLinkURL
")
63 oLogCursor.SetPropertyToDefault(
"HyperLinkTarget
")
64 oLogCursor.SetPropertyToDefault(
"HyperLinkName
")
67 ' Get the Properties of the document
71 Case
"swriter
"
73 Case
"scalc
"
75 Case
"sdraw
",
"simpress
"
78 Msgbox(
"This macro only works with a Writer, Calc or Draw/Impress document.
",
16, GetProductName())
84 ' ***********************************************Calc-Documents**************************************************
89 Dim SheetName as String
91 ' 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(
"Sheet No.
" & i
& "(
" & SheetName
& ")
" )
99 ' Check the
"body
" of the sheet
102 If oSheet.IsScenario then
103 MakeLogHeadLine(
"Scenario Comments from
" & SheetName
& "'")
104 WriteStringtoLogFile(osheet.ScenarioComment)
107 GetAnnotations(oSheet,
"Annotations from
'" & SheetName
& "'")
109 GetChartStrings(oSheet,
"Charts from
'" & SheetName
& "'")
111 GetControlStrings(oSheet.DrawPage,
"Controls from
'" & SheetName
& "'")
115 GetCalcGraphicNames()
121 Sub GetCellTexts(oSheet as Object)
122 Dim BigRange, BigEnum, oCell as Object
123 BigRange = oDocument.CreateInstance(
"com.sun.star.sheet.SheetCellRanges
")
124 BigRange.InsertbyName(
"",oSheet)
125 BigEnum = BigRange.GetCells.CreateEnumeration
126 While BigEnum.hasmoreElements
127 oCell = BigEnum.NextElement
128 If oCell.String
<> "" And Val(oCell.String) =
0then
129 WriteStringtoLogFile(oCell.String)
135 Sub GetAnnotations(oSheet as Object, HeaderLine as String)
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)
150 MakeLogHeadLine(
"Named Ranges
")
151 For i =
0 To oDocument.NamedRanges.Count -
1
152 WriteStringtoLogFile(oDocument.NamedRanges.GetbyIndex(i).Name)
157 Sub GetCalcGraphicNames()
159 MakeLogHeadLine(
"Graphics
")
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)
168 ' ***********************************************Writer-Documents**************************************************
170 Sub GetParagraphTexts(oParaObject as Object, HeadLine as String)
171 Dim ParaEnum as Object
173 Dim oTextPortEnum as Object
174 Dim oTextPortion as Object
179 MakeLogHeadLine(HeadLine)
180 ParaEnum = oParaObject.Text.CreateEnumeration
182 While ParaEnum.HasMoreElements
183 oPara = ParaEnum.NextElement
185 ' Note: The Enumeration ParaEnum lists all tables and Paragraphs.
186 ' Therefor we have to find out what kind of object
"oPara
" actually is
187 If oPara.supportsService(
"com.sun.star.text.Paragraph
") Then
188 ' "oPara
" is a Paragraph
189 oTextPortEnum = oPara.createEnumeration
190 While oTextPortEnum.hasmoreElements
191 oTextPortion = oTextPortEnum.nextElement()
192 WriteStringToLogFile(oTextPortion.String)
195 ' "oPara
" is a table
196 oCellNames = oPara.CellNames
197 For i =
0 To Ubound(oCellNames())
198 If oCellNames(i)
<> "" Then
199 oCell = oPara.getCellByName(oCellNames(i))
200 WriteStringToLogFile(oCell.String)
209 Sub GetChartStrings(oSheet as Object, HeaderLine as String)
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)
222 If aChartObject.HasMainTitle then
223 WriteStringToLogFile(aChartObject.Title.String)
226 aChartDiagram = aChartObject.Diagram
228 If aChartDiagram.hasXAxisTitle Then
229 WriteStringToLogFile(aChartDiagram.XAxisTitle)
232 If aChartDiagram.hasYAxisTitle Then
233 WriteStringToLogFile(aChartDiagram.YAxisTitle)
236 If aChartDiagram.hasZAxisTitle Then
237 WriteStringToLogFile(aChartDiagram.ZAxisTitle)
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(
"Text Frames
")
253 For i =
0 to oDocument.TextFrames.Count-
1
254 oTextFrame = oDocument.TextFrames.GetbyIndex(i)
255 WriteStringToLogFile(oTextFrame.Name)
257 ' Is the frame bound to the Page
258 If oTextFrame.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE Then
259 GetParagraphTexts(oTextFrame,
"Text Frame Contents
")
262 oFrameEnum = oTextFrame.CreateEnumeration
263 While oFrameEnum.HasMoreElements
264 oFramePort = oFrameEnum.NextElement
265 If oFramePort.supportsService(
"com.sun.star.text.Paragraph
") then
266 oFrameTextEnum = oFramePort.createEnumeration
267 While oFrameTextEnum.HasMoreElements
268 oFrameTextPort = oFrameTextEnum.NextElement
269 If oFrameTextPort.SupportsService(
"com.sun.star.text.TextFrame
") Then
270 WriteStringtoLogFile(oFrameTextPort.String)
274 WriteStringtoLogFile(oFramePort.Name)
281 Sub GetTextFieldStrings()
282 Dim aTextField as Object
284 Dim CurElement as Object
285 MakeLogHeadLine(
"Text Fields
")
286 aTextfield = oDocument.getTextfields.CreateEnumeration
287 While aTextField.hasmoreElements
288 CurElement = aTextField.NextElement
289 If CurElement.PropertySetInfo.hasPropertybyName(
"Content
") Then
290 WriteStringtoLogFile(CurElement.Content)
291 ElseIf CurElement.PropertySetInfo.hasPropertybyName(
"PlaceHolder
") Then
292 WriteStringtoLogFile(CurElement.PlaceHolder)
293 WriteStringtoLogFile(CurElement.Hint)
294 ElseIf Curelement.TextFieldMaster.PropertySetInfo.HasPropertybyName(
"Content
") then
295 WriteStringtoLogFile(CurElement.TextFieldMaster.Content)
302 Sub GetLinkedFileNames()
303 Dim oDocSections as Object
304 Dim LinkedFileName as String
306 If Right(oDocument.URL,
3) =
"sgl
" Then
307 MakeLogHeadLine(
"Sub-documents
")
308 oDocSections = oDocument.TextSections
309 For i =
0 to oDocSections.Count -
1
310 LinkedFileName = oDocSections.GetbyIndex(i).FileLink.FileURL
311 If LinkedFileName
<> "" Then
312 WriteStringToLogFile(LinkedFileName)
319 Sub GetSectionNames()
321 Dim oDocSections as Object
322 MakeLogHeadLine(
"Sections
")
323 oDocSections = oDocument.TextSections
324 For i =
0 to oDocSections.Count-
1
325 WriteStringtoLogFile(oDocSections.GetbyIndex(i).Name)
330 Sub GetWriterStrings()
331 GetParagraphTexts(oDocument,
"Document Body
")
334 GetControlStrings(oDocument.DrawPage,
"Controls
")
335 GetTextFieldStrings()
343 ' ***********************************************Draw-Documents**************************************************
345 Sub GetDrawPageTitles(LocObject as Object)
349 For n =
0 to LocObject.Count -
1
350 oPage = LocObject.GetbyIndex(n)
351 WriteStringtoLogFile(oPage.Name)
352 ' Is the Page a DrawPage and not a MasterPage?
353 If oPage.supportsService(
"com.sun.star.drawing.DrawPage
")then
354 ' Get the Name of the NotesPage (only relevant for Impress-Documents)
355 If oDocument.supportsService(
"com.sun.star.presentation.PresentationDocument
") then
356 WriteStringtoLogFile(oPage.NotesPage.Name)
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,
"com.sun.star.container.XIndexAccess
") Then
372 ' The Object
"oPageElement
" 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)
376 ElseIf HasUnoInterfaces(oPageElement,
"com.sun.star.text.XText
") Then
377 WriteStringtoLogFile(oPageElement.String)
386 Dim oDPages, oMPages as Object
388 oDPages = oDocument.DrawPages
389 oMPages = oDocument.Masterpages
391 MakeLogHeadLine(
"Titles
")
392 GetDrawPageTitles(oDPages)
393 GetDrawPageTitles(oMPages)
395 MakeLogHeadLine(
"Document Body
")
396 GetPageStrings(oDPages)
397 GetPageStrings(oMPages)
401 ' ***********************************************Misc**************************************************
403 Sub GetDocumentProps()
404 Dim oDocuProps as Object
405 MakeLogHeadLine(
"Document Properties
")
406 oDocuProps = oDocument.DocumentProperties
407 WriteStringToLogFile(oDocuProps.Title)
408 WriteStringToLogFile(oDocuProps.Description)
409 WriteStringToLogFile(oDocuProps.Subject)
410 WriteStringToLogFile(oDocuProps.Author)
411 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.ReplyTo)
412 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.Recipient)
413 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.References)
414 ' WriteStringToLogFile(oDocuProps.Keywords)
421 Dim oAllHyperLinks as Object
422 Dim SrchAttributes(
0) as new com.sun.star.beans.PropertyValue
423 Dim oSearchDesc as Object
425 MakeLogHeadLine(
"Hyperlinks
")
426 ' create a Search-Descriptor
427 oSearchDesc = oDocument.CreateSearchDescriptor
428 oSearchDesc.Valuesearch = False
430 ' define the Search-attributes
431 srchattributes(
0).Name =
"HyperLinkURL
"
432 srchattributes(
0).Value =
""
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)
'Url
441 WriteStringToLogFile(oCrs.HyperLinkTarget)
'Name
442 WriteStringToLogFile(oCrs.HyperLinkName)
'Frame
447 Sub GetGraphicNames()
449 Dim oDocGraphics as Object
450 MakeLogHeadLine(
"Graphics
")
451 oDocGraphics = oDocument.GraphicObjects
452 For i =
0 to oDocGraphics.count -
1
453 WriteStringtoLogFile(oDocGraphics.GetbyIndex(i).Name)
460 MakeLogHeadLine(
"User-defined Templates
")
462 ' 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)
473 Sub GetControlStrings(oDPage as Object, HeaderLine as String)
476 MakeLogHeadLine(HeaderLine)
477 '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))
487 Sub GetControlContent(LocControl as Object)
490 If LocControl.PropertySetInfo.HasPropertybyName(
"Label
") then
491 WriteStringtoLogFile(LocControl.Label)
493 ElseIf LocControl.SupportsService(
"com.sun.star.form.component.ListBox
") then
494 For i =
0 to Ubound(LocControl.StringItemList())
495 WriteStringtoLogFile(LocControl.StringItemList(i))
498 If LocControl.PropertySetInfo.HasPropertybyName(
"HelpText
") then
499 WriteStringtoLogFile(LocControl.Helptext)
503 ' ***********************************************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)
515 Sub MakeLogHeadLine(HeadText as String)
516 oLogCursor.CharStyleName =
"Log Header
"
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 =
"Log Body
"