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' Macro-Description: 25' This Macro extracts the Strings out of the currently activated document und inserts them into a logdocument 26' The aim of the macro is to provide the programmer an insight into the StarOffice API 27' It focusses on how document-Objects are accessed. 28' Therefor 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 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 ' Therefor 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 228 229Sub GetChartStrings(oSheet as Object, HeaderLine as String) 230Dim i as Integer 231Dim aChartObject as Object 232Dim aChartDiagram as Object 233 234 MakeLogHeadLine(HeaderLine) 235 236 For i = 0 to oSheet.Charts.Count-1 237 aChartObject = oSheet.Charts.GetByIndex(i).EmbeddedObject 238 If aChartObject.HasSubTitle then 239 WriteStringToLogFile(aChartObject.SubTitle.String) 240 End If 241 242 If aChartObject.HasMainTitle then 243 WriteStringToLogFile(aChartObject.Title.String) 244 End If 245 246 aChartDiagram = aChartObject.Diagram 247 248 If aChartDiagram.hasXAxisTitle Then 249 WriteStringToLogFile(aChartDiagram.XAxisTitle) 250 End If 251 252 If aChartDiagram.hasYAxisTitle Then 253 WriteStringToLogFile(aChartDiagram.YAxisTitle) 254 End If 255 256 If aChartDiagram.hasZAxisTitle Then 257 WriteStringToLogFile(aChartDiagram.ZAxisTitle) 258 End If 259 Next i 260End Sub 261 262 263 264Sub GetFrameTexts() 265Dim i as integer 266Dim oTextFrame as object 267Dim oFrameEnum as Object 268Dim oFramePort as Object 269Dim oFrameTextEnum as Object 270Dim oFrameTextPort as Object 271 272 MakeLogHeadLine("Text Frames") 273 For i = 0 to oDocument.TextFrames.Count-1 274 oTextFrame = oDocument.TextFrames.GetbyIndex(i) 275 WriteStringToLogFile(oTextFrame.Name) 276 277 ' Is the frame bound to the Page 278 If oTextFrame.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE Then 279 GetParagraphTexts(oTextFrame, "Text Frame Contents") 280 End If 281 282 oFrameEnum = oTextFrame.CreateEnumeration 283 While oFrameEnum.HasMoreElements 284 oFramePort = oFrameEnum.NextElement 285 If oFramePort.supportsService("com.sun.star.text.Paragraph") then 286 oFrameTextEnum = oFramePort.createEnumeration 287 While oFrameTextEnum.HasMoreElements 288 oFrameTextPort = oFrameTextEnum.NextElement 289 If oFrameTextPort.SupportsService("com.sun.star.text.TextFrame") Then 290 WriteStringtoLogFile(oFrameTextPort.String) 291 End If 292 Wend 293 Else 294 WriteStringtoLogFile(oFramePort.Name) 295 End if 296 Wend 297 Next 298End Sub 299 300 301Sub GetTextFieldStrings() 302Dim aTextField as Object 303Dim i as integer 304Dim CurElement as Object 305 MakeLogHeadLine("Text Fields") 306 aTextfield = oDocument.getTextfields.CreateEnumeration 307 While aTextField.hasmoreElements 308 CurElement = aTextField.NextElement 309 If CurElement.PropertySetInfo.hasPropertybyName("Content") Then 310 WriteStringtoLogFile(CurElement.Content) 311 ElseIf CurElement.PropertySetInfo.hasPropertybyName("PlaceHolder") Then 312 WriteStringtoLogFile(CurElement.PlaceHolder) 313 WriteStringtoLogFile(CurElement.Hint) 314 ElseIf Curelement.TextFieldMaster.PropertySetInfo.HasPropertybyName("Content") then 315 WriteStringtoLogFile(CurElement.TextFieldMaster.Content) 316 End If 317 Wend 318End Sub 319 320 321 322Sub GetLinkedFileNames() 323Dim oDocSections as Object 324Dim LinkedFileName as String 325Dim i as Integer 326 If Right(oDocument.URL,3) = "sgl" Then 327 MakeLogHeadLine("Sub-documents") 328 oDocSections = oDocument.TextSections 329 For i = 0 to oDocSections.Count - 1 330 LinkedFileName = oDocSections.GetbyIndex(i).FileLink.FileURL 331 If LinkedFileName <> "" Then 332 WriteStringToLogFile(LinkedFileName) 333 End If 334 Next i 335 End If 336End Sub 337 338 339Sub GetSectionNames() 340Dim i as integer 341Dim oDocSections as Object 342 MakeLogHeadLine("Sections") 343 oDocSections = oDocument.TextSections 344 For i = 0 to oDocSections.Count-1 345 WriteStringtoLogFile(oDocSections.GetbyIndex(i).Name) 346 Next 347End Sub 348 349 350Sub GetWriterStrings() 351 GetParagraphTexts(oDocument, "Document Body") 352 GetGraphicNames() 353 GetStyles() 354 GetControlStrings(oDocument.DrawPage, "Controls") 355 GetTextFieldStrings() 356 GetSectionNames() 357 GetFrameTexts() 358 GetHyperLinks 359 GetLinkedFileNames() 360End Sub 361 362 363' ***********************************************Draw-Documents************************************************** 364 365Sub GetDrawPageTitles(LocObject as Object) 366Dim n as integer 367Dim oPage as Object 368 369 For n = 0 to LocObject.Count - 1 370 oPage = LocObject.GetbyIndex(n) 371 WriteStringtoLogFile(oPage.Name) 372 ' Is the Page a DrawPage and not a MasterPage? 373 If oPage.supportsService("com.sun.star.drawing.DrawPage")then 374 ' Get the Name of the NotesPage (only relevant for Impress-Documents) 375 If oDocument.supportsService("com.sun.star.presentation.PresentationDocument") then 376 WriteStringtoLogFile(oPage.NotesPage.Name) 377 End If 378 End If 379 Next 380End Sub 381 382 383Sub GetPageStrings(oPages as Object) 384Dim m, n, s as Integer 385Dim oPage, oPageElement, oShape as Object 386 For n = 0 to oPages.Count-1 387 oPage = oPages.GetbyIndex(n) 388 If oPage.HasElements then 389 For m = 0 to oPage.Count-1 390 oPageElement = oPage.GetByIndex(m) 391 If HasUnoInterfaces(oPageElement,"com.sun.star.container.XIndexAccess") Then 392 ' The Object "oPageElement" a group of Shapes, that can be accessed by their index 393 For s = 0 To oPageElement.Count - 1 394 WriteStringToLogFile(oPageElement.GetByIndex(s).String) 395 Next s 396 ElseIf HasUnoInterfaces(oPageElement, "com.sun.star.text.XText") Then 397 WriteStringtoLogFile(oPageElement.String) 398 End If 399 Next 400 End If 401 Next 402End Sub 403 404 405Sub GetDrawStrings() 406Dim oDPages, oMPages as Object 407 408 oDPages = oDocument.DrawPages 409 oMPages = oDocument.Masterpages 410 411 MakeLogHeadLine("Titles") 412 GetDrawPageTitles(oDPages) 413 GetDrawPageTitles(oMPages) 414 415 MakeLogHeadLine("Document Body") 416 GetPageStrings(oDPages) 417 GetPageStrings(oMPages) 418End Sub 419 420 421' ***********************************************Misc************************************************** 422 423Sub GetDocumentProps() 424Dim oDocuProps as Object 425 MakeLogHeadLine("Document Properties") 426 oDocuProps = oDocument.DocumentProperties 427 WriteStringToLogFile(oDocuProps.Title) 428 WriteStringToLogFile(oDocuProps.Description) 429 WriteStringToLogFile(oDocuProps.Subject) 430 WriteStringToLogFile(oDocuProps.Author) 431' WriteStringToLogFile(oDocuProps.UserDefinedProperties.ReplyTo) 432' WriteStringToLogFile(oDocuProps.UserDefinedProperties.Recipient) 433' WriteStringToLogFile(oDocuProps.UserDefinedProperties.References) 434' WriteStringToLogFile(oDocuProps.Keywords) 435End Sub 436 437 438Sub GetHyperlinks() 439Dim i as integer 440Dim oCrsr as Object 441Dim oAllHyperLinks as Object 442Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue 443Dim oSearchDesc as Object 444 445 MakeLogHeadLine("Hyperlinks") 446 ' create a Search-Descriptor 447 oSearchDesc = oDocument.CreateSearchDescriptor 448 oSearchDesc.Valuesearch = False 449 450 ' define the Search-attributes 451 srchattributes(0).Name = "HyperLinkURL" 452 srchattributes(0).Value = "" 453 oSearchDesc.SetSearchAttributes(SrchAttributes()) 454 455 oAllHyperLinks = oDocument.findAll(oSearchDesc()) 456 457 For i = 0 to oAllHyperLinks.Count - 1 458 oFound = oAllHyperLinks(i) 459 oCrsr = oFound.Text.createTextCursorByRange(oFound) 460 WriteStringToLogFile(oCrs.HyperLinkURL) 'Url 461 WriteStringToLogFile(oCrs.HyperLinkTarget) 'Name 462 WriteStringToLogFile(oCrs.HyperLinkName) 'Frame 463 Next i 464End Sub 465 466 467Sub GetGraphicNames() 468Dim i as integer 469Dim oDocGraphics as Object 470 MakeLogHeadLine("Graphics") 471 oDocGraphics = oDocument.GraphicObjects 472 For i = 0 to oDocGraphics.count - 1 473 WriteStringtoLogFile(oDocGraphics.GetbyIndex(i).Name) 474 Next 475End Sub 476 477 478Sub GetStyles() 479Dim m,n as integer 480 MakeLogHeadLine("User-defined Templates") 481 482 ' Check all StyleFamilies(i.e. PageStyles, ParagraphStyles, CharacterStyles, cellStyles) 483 For n = 0 to oDocument.StyleFamilies.Count - 1 484 For m = 0 to oDocument.StyleFamilies.getbyIndex(n).Count-1 485 If oDocument.StyleFamilies.GetbyIndex(n).getbyIndex(m).IsUserDefined then 486 WriteStringtoLogFile(oDocument.StyleFamilies.GetbyIndex(n).getbyIndex(m).Name) 487 End If 488 Next 489 Next 490End Sub 491 492 493Sub GetControlStrings(oDPage as Object, HeaderLine as String) 494Dim aForm as Object 495Dim m,n as integer 496 MakeLogHeadLine(HeaderLine) 497 'SearchFor all possible Controls 498 For n = 0 to oDPage.Forms.Count - 1 499 aForm = oDPage.Forms(n) 500 For m = 0 to aForm.Count-1 501 GetControlContent(aForm.GetbyIndex(m)) 502 Next 503 Next 504End Sub 505 506 507Sub GetControlContent(LocControl as Object) 508Dim i as integer 509 510 If LocControl.PropertySetInfo.HasPropertybyName("Label") then 511 WriteStringtoLogFile(LocControl.Label) 512 513 ElseIf LocControl.SupportsService("com.sun.star.form.component.ListBox") then 514 For i = 0 to Ubound(LocControl.StringItemList()) 515 WriteStringtoLogFile(LocControl.StringItemList(i)) 516 Next 517 End If 518 If LocControl.PropertySetInfo.HasPropertybyName("HelpText") then 519 WriteStringtoLogFile(LocControl.Helptext) 520 End If 521End Sub 522 523' ***********************************************LogDocument************************************************** 524 525Sub WriteStringtoLogFile( sString as String) 526 If (Not FieldInArray(LogArray(),LogIndex,sString))AND (NOT ISNULL(sString)) Then 527 LogArray(LogIndex) = sString 528 LogIndex = LogIndex + 1 529 oLogText.insertString(oLogCursor,sString,False) 530 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False) 531 End If 532End Sub 533 534 535Sub MakeLogHeadLine(HeadText as String) 536 oLogCursor.CharStyleName = "Log Header" 537 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False) 538 oLogText.insertString(oLogCursor,HeadText,False) 539 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False) 540 oLogCursor.CharStyleName = "Log Body" 541End Sub 542</script:module> 543