1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> 3<!--*********************************************************** 4 * 5 * Licensed to the Apache Software Foundation (ASF) under one 6 * or more contributor license agreements. See the NOTICE file 7 * distributed with this work for additional information 8 * regarding copyright ownership. The ASF licenses this file 9 * to you under the Apache License, Version 2.0 (the 10 * "License"); you may not use this file except in compliance 11 * with the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, 16 * software distributed under the License is distributed on an 17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 18 * KIND, either express or implied. See the License for the 19 * specific language governing permissions and limitations 20 * under the License. 21 * 22 ***********************************************************--> 23<script:module xmlns:script="http://openoffice.org/2000/script" script:name="GetTexts" script:language="StarBasic">Option Explicit 24' Description: 25' This macro extracts the strings out of the currently active document and inserts them into a log document. 26' The aim of the macro is to provide the programmer an insight into the OpenOffice API. 27' It focuses on how document objects are accessed. 28' Therefore not only texts of the document body are retrieved but also texts of general 29' document objects like, annotations, charts and general document information. 30 31Public oLogDocument, oLogText, oLogCursor, oLogHeaderStyle, oLogBodyTextStyle as Object 32Public oDocument as Object 33Public LogArray(1000) as String 34Public LogIndex as Integer 35Public oLocHeaderStyle as Object 36 37Sub Main 38Dim sDocType as String 39Dim oHyperCursor as Object 40Dim oCharStyles as Object 41 BasicLibraries.LoadLibrary("Tools") 42 On Local Error GoTo NODOCUMENT 43 oDocument = StarDesktop.ActiveFrame.Controller.Model 44 sDocType = GetDocumentType(oDocument) 45 NODOCUMENT: 46 If Err <> 0 Then 47 Msgbox("This macro extracts all data from the active Writer, Calc or Draw/Impress document." & chr(13) &_ 48 "To start this macro you have to activate a document first." , 16, GetProductName) 49 Exit Sub 50 End If 51 On Local Error Goto 0 52 53 ' Open a new document where all the texts are inserted 54 oLogDocument = CreateNewDocument("swriter") 55 If Not IsNull(oLogDocument) Then 56 oLogText = oLogDocument.Text 57 58 ' create and define the character styles of the log document 59 oCharStyles = oLogDocument.StyleFamilies.GetByName("CharacterStyles") 60 oLogHeaderStyle = oLogDocument.createInstance("com.sun.star.style.CharacterStyle") 61 oCharStyles.InsertbyName("Log Header", oLogHeaderStyle) 62 63 oLogHeaderStyle.charWeight = com.sun.star.awt.FontWeight.BOLD 64 oLogBodyTextStyle = oLogDocument.createInstance("com.sun.star.style.CharacterStyle") 65 oCharStyles.InsertbyName("Log Body", oLogBodyTextStyle) 66 67 ' Insert the title of the activated document as a hyperlink 68 oHyperCursor = oLogText.createTextCursor() 69 oHyperCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD 70 oHyperCursor.gotoStart(False) 71 oHyperCursor.HyperLinkURL = oDocument.URL 72 oHyperCursor.HyperLinkTarget = oDocument.URL 73 If oDocument.DocumentProperties.Title <> "" Then 74 oHyperCursor.HyperlinkName = oDocument.DocumentProperties.Title 75 End If 76 oLogText.insertString(oHyperCursor, oDocument.DocumentProperties.Title, False) 77 oLogText.insertControlCharacter(oHyperCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False) 78 79 oLogCursor = oLogText.createTextCursor() 80 oLogCursor.GotoEnd(False) 81 ' "Switch off" the Hyperlink - Properties 82 oLogCursor.SetPropertyToDefault("HyperLinkURL") 83 oLogCursor.SetPropertyToDefault("HyperLinkTarget") 84 oLogCursor.SetPropertyToDefault("HyperLinkName") 85 LogIndex = 0 86 87 ' Get the Properties of the document 88 GetDocumentProps() 89 90 Select Case sDocType 91 Case "swriter" 92 GetWriterStrings() 93 Case "scalc" 94 GetCalcStrings() 95 Case "sdraw", "simpress" 96 GetDrawStrings() 97 Case Else 98 Msgbox("This macro only works with a Writer, Calc or Draw/Impress document.", 16, GetProductName()) 99 End Select 100 End If 101End Sub 102 103 104' ***********************************************Calc documents************************************************** 105 106Sub GetCalcStrings() 107Dim i, n as integer 108Dim oSheet as Object 109Dim SheetName as String 110Dim oSheets as Object 111 ' Create a sequence of all sheets within the document 112 oSheets = oDocument.Sheets 113 114 For i = 0 to osheets.Count - 1 115 oSheet = osheets.GetbyIndex(i) 116 SheetName = oSheet.Name 117 MakeLogHeadLine("Sheet No. " & i & " (" & SheetName & ")" ) 118 119 ' Check the "body" of the sheet 120 GetCellTexts(oSheet) 121 122 If oSheet.IsScenario then 123 MakeLogHeadLine("Scenario Comments from " & SheetName & "'") 124 WriteStringtoLogFile(osheet.ScenarioComment) 125 End if 126 127 GetAnnotations(oSheet, "Annotations from '" & SheetName & "'") 128 129 GetChartStrings(oSheet, "Charts from '" & SheetName & "'") 130 131 GetControlStrings(oSheet.DrawPage, "Controls from '" & SheetName & "'") 132 Next 133 134 ' Pictures 135 GetCalcGraphicNames() 136 137 GetNamedRanges() 138End Sub 139 140 141Sub GetCellTexts(oSheet as Object) 142Dim BigRange, BigEnum, oCell as Object 143 BigRange = oDocument.CreateInstance("com.sun.star.sheet.SheetCellRanges") 144 BigRange.InsertbyName("",oSheet) 145 BigEnum = BigRange.GetCells.CreateEnumeration 146 While BigEnum.hasmoreElements 147 oCell = BigEnum.NextElement 148 If oCell.String <> "" And Val(oCell.String) = 0then 149 WriteStringtoLogFile(oCell.String) 150 End If 151 Wend 152End Sub 153 154 155Sub GetAnnotations(oSheet as Object, HeaderLine as String) 156Dim oNotes as Object 157Dim n as Integer 158 oNotes = oSheet.getAnnotations 159 If oNotes.hasElements() then 160 MakeLogHeadLine(HeaderLine) 161 For n = 0 to oNotes.Count-1 162 WriteStringtoLogFile(oNotes.GetbyIndex(n).String) 163 Next 164 End if 165End Sub 166 167 168Sub GetNamedRanges() 169Dim i as integer 170 MakeLogHeadLine("Named Ranges") 171 For i = 0 To oDocument.NamedRanges.Count - 1 172 WriteStringtoLogFile(oDocument.NamedRanges.GetbyIndex(i).Name) 173 Next 174End Sub 175 176 177Sub GetCalcGraphicNames() 178Dim n,m as integer 179 MakeLogHeadLine("Graphics") 180 For n = 0 To oDocument.Drawpages.count-1 181 For m = 0 To oDocument.Drawpages.GetbyIndex(n).Count - 1 182 WriteStringtoLogFile(oDocument.DrawPages.GetbyIndex(n).GetbyIndex(m).Text.String) 183 Next m 184 Next n 185End Sub 186 187 188' ***********************************************Writer documents************************************************** 189 190Sub GetParagraphTexts(oParaObject as Object, HeadLine as String) 191Dim ParaEnum as Object 192Dim oPara as Object 193Dim oTextPortEnum as Object 194Dim oTextPortion as Object 195Dim i as integer 196Dim oCellNames() 197Dim oCell as Object 198 199 MakeLogHeadLine(HeadLine) 200 ParaEnum = oParaObject.Text.CreateEnumeration 201 202 While ParaEnum.HasMoreElements 203 oPara = ParaEnum.NextElement 204 205 ' Note: The enumeration ParaEnum lists all tables and paragraphs. 206 ' Therefore we have to find out what kind of object "oPara" actually is 207 If oPara.supportsService("com.sun.star.text.Paragraph") Then 208 ' "oPara" is a Paragraph 209 oTextPortEnum = oPara.createEnumeration 210 While oTextPortEnum.hasmoreElements 211 oTextPortion = oTextPortEnum.nextElement() 212 WriteStringToLogFile(oTextPortion.String) 213 Wend 214 Else 215 ' "oPara" is a table 216 oCellNames = oPara.CellNames 217 For i = 0 To Ubound(oCellNames()) 218 If oCellNames(i) <> "" Then 219 oCell = oPara.getCellByName(oCellNames(i)) 220 WriteStringToLogFile(oCell.String) 221 End If 222 Next 223 End If 224 Wend 225End Sub 226 227 228Sub GetChartStrings(oSheet as Object, HeaderLine as String) 229Dim i as Integer 230Dim aChartObject as Object 231Dim aChartDiagram as Object 232 233 MakeLogHeadLine(HeaderLine) 234 235 For i = 0 to oSheet.Charts.Count-1 236 aChartObject = oSheet.Charts.GetByIndex(i).EmbeddedObject 237 If aChartObject.HasSubTitle then 238 WriteStringToLogFile(aChartObject.SubTitle.String) 239 End If 240 241 If aChartObject.HasMainTitle then 242 WriteStringToLogFile(aChartObject.Title.String) 243 End If 244 245 aChartDiagram = aChartObject.Diagram 246 247 If aChartDiagram.hasXAxisTitle Then 248 WriteStringToLogFile(aChartDiagram.XAxisTitle) 249 End If 250 251 If aChartDiagram.hasYAxisTitle Then 252 WriteStringToLogFile(aChartDiagram.YAxisTitle) 253 End If 254 255 If aChartDiagram.hasZAxisTitle Then 256 WriteStringToLogFile(aChartDiagram.ZAxisTitle) 257 End If 258 Next i 259End Sub 260 261 262Sub GetFrameTexts() 263Dim i as integer 264Dim oTextFrame as object 265Dim oFrameEnum as Object 266Dim oFramePort as Object 267Dim oFrameTextEnum as Object 268Dim oFrameTextPort as Object 269 270 MakeLogHeadLine("Text Frames") 271 For i = 0 to oDocument.TextFrames.Count-1 272 oTextFrame = oDocument.TextFrames.GetbyIndex(i) 273 WriteStringToLogFile(oTextFrame.Name) 274 275 ' Is the frame bound to the page? 276 If oTextFrame.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE Then 277 GetParagraphTexts(oTextFrame, "Text Frame Contents") 278 End If 279 280 oFrameEnum = oTextFrame.CreateEnumeration 281 While oFrameEnum.HasMoreElements 282 oFramePort = oFrameEnum.NextElement 283 If oFramePort.supportsService("com.sun.star.text.Paragraph") then 284 oFrameTextEnum = oFramePort.createEnumeration 285 While oFrameTextEnum.HasMoreElements 286 oFrameTextPort = oFrameTextEnum.NextElement 287 If oFrameTextPort.SupportsService("com.sun.star.text.TextFrame") Then 288 WriteStringtoLogFile(oFrameTextPort.String) 289 End If 290 Wend 291 Else 292 WriteStringtoLogFile(oFramePort.Name) 293 End if 294 Wend 295 Next 296End Sub 297 298 299Sub GetTextFieldStrings() 300Dim aTextField as Object 301Dim i as integer 302Dim CurElement as Object 303 MakeLogHeadLine("Text Fields") 304 aTextfield = oDocument.getTextfields.CreateEnumeration 305 While aTextField.hasmoreElements 306 CurElement = aTextField.NextElement 307 If CurElement.PropertySetInfo.hasPropertybyName("Content") Then 308 WriteStringtoLogFile(CurElement.Content) 309 ElseIf CurElement.PropertySetInfo.hasPropertybyName("PlaceHolder") Then 310 WriteStringtoLogFile(CurElement.PlaceHolder) 311 WriteStringtoLogFile(CurElement.Hint) 312 ElseIf Curelement.TextFieldMaster.PropertySetInfo.HasPropertybyName("Content") then 313 WriteStringtoLogFile(CurElement.TextFieldMaster.Content) 314 End If 315 Wend 316End Sub 317 318 319Sub GetLinkedFileNames() 320Dim oDocSections as Object 321Dim LinkedFileName as String 322Dim i as Integer 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) 330 End If 331 Next i 332 End If 333End Sub 334 335 336Sub GetSectionNames() 337Dim i as integer 338Dim oDocSections as Object 339 MakeLogHeadLine("Sections") 340 oDocSections = oDocument.TextSections 341 For i = 0 to oDocSections.Count-1 342 WriteStringtoLogFile(oDocSections.GetbyIndex(i).Name) 343 Next 344End Sub 345 346 347Sub GetWriterStrings() 348 GetParagraphTexts(oDocument, "Document Body") 349 GetGraphicNames() 350 GetStyles() 351 GetControlStrings(oDocument.DrawPage, "Controls") 352 GetTextFieldStrings() 353 GetSectionNames() 354 GetFrameTexts() 355 GetHyperLinks 356 GetLinkedFileNames() 357End Sub 358 359 360' ***********************************************Draw/Impress documents************************************************** 361 362Sub GetDrawPageTitles(LocObject as Object) 363Dim n as integer 364Dim oPage as Object 365 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) 374 End If 375 End If 376 Next 377End Sub 378 379 380Sub GetPageStrings(oPages as Object) 381Dim m, n, s as Integer 382Dim 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) 392 Next s 393 ElseIf HasUnoInterfaces(oPageElement, "com.sun.star.text.XText") Then 394 WriteStringtoLogFile(oPageElement.String) 395 End If 396 Next 397 End If 398 Next 399End Sub 400 401 402Sub GetDrawStrings() 403Dim oDPages, oMPages as Object 404 405 oDPages = oDocument.DrawPages 406 oMPages = oDocument.Masterpages 407 408 MakeLogHeadLine("Titles") 409 GetDrawPageTitles(oDPages) 410 GetDrawPageTitles(oMPages) 411 412 MakeLogHeadLine("Document Body") 413 GetPageStrings(oDPages) 414 GetPageStrings(oMPages) 415End Sub 416 417 418' ***********************************************Misc************************************************** 419 420Sub GetDocumentProps() 421Dim 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) 432End Sub 433 434 435Sub GetHyperlinks() 436Dim i as integer 437Dim oCrsr as Object 438Dim oAllHyperLinks as Object 439Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue 440Dim oSearchDesc as Object 441 442 MakeLogHeadLine("Hyperlinks") 443 ' create a Search-Descriptor 444 oSearchDesc = oDocument.CreateSearchDescriptor 445 oSearchDesc.Valuesearch = False 446 447 ' define the Search-attributes 448 srchattributes(0).Name = "HyperLinkURL" 449 srchattributes(0).Value = "" 450 oSearchDesc.SetSearchAttributes(SrchAttributes()) 451 452 oAllHyperLinks = oDocument.findAll(oSearchDesc()) 453 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 460 Next i 461End Sub 462 463 464Sub GetGraphicNames() 465Dim i as integer 466Dim oDocGraphics as Object 467 MakeLogHeadLine("Graphics") 468 oDocGraphics = oDocument.GraphicObjects 469 For i = 0 to oDocGraphics.count - 1 470 WriteStringtoLogFile(oDocGraphics.GetbyIndex(i).Name) 471 Next 472End Sub 473 474 475Sub GetStyles() 476Dim m,n as integer 477 MakeLogHeadLine("User-defined Templates") 478 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) 484 End If 485 Next 486 Next 487End Sub 488 489 490Sub GetControlStrings(oDPage as Object, HeaderLine as String) 491Dim aForm as Object 492Dim m,n as integer 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)) 499 Next 500 Next 501End Sub 502 503 504Sub GetControlContent(LocControl as Object) 505Dim i as integer 506 507 If LocControl.PropertySetInfo.HasPropertybyName("Label") then 508 WriteStringtoLogFile(LocControl.Label) 509 510 ElseIf LocControl.SupportsService("com.sun.star.form.component.ListBox") then 511 For i = 0 to Ubound(LocControl.StringItemList()) 512 WriteStringtoLogFile(LocControl.StringItemList(i)) 513 Next 514 End If 515 If LocControl.PropertySetInfo.HasPropertybyName("HelpText") then 516 WriteStringtoLogFile(LocControl.Helptext) 517 End If 518End Sub 519 520' ***********************************************Log document************************************************** 521 522Sub 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) 528 End If 529End Sub 530 531 532Sub 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" 538End Sub 539</script:module> 540