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 10 11Public oLogDocument, oLogText, oLogCursor, oLogHeaderStyle, oLogBodyTextStyle as Object 12Public oDocument as Object 13Public LogArray(1000) as String 14Public LogIndex as Integer 15Public oLocHeaderStyle as Object 16 17Sub Main 18Dim sDocType as String 19Dim oHyperCursor as Object 20Dim oCharStyles as Object 21 BasicLibraries.LoadLibrary("Tools") 22 On Local Error GoTo NODOCUMENT 23 oDocument = StarDesktop.ActiveFrame.Controller.Model 24 sDocType = GetDocumentType(oDocument) 25 NODOCUMENT: 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) 29 Exit Sub 30 End If 31 On Local Error Goto 0 32 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 37 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) 42 43 oLogHeaderStyle.charWeight = com.sun.star.awt.FontWeight.BOLD 44 oLogBodyTextStyle = oLogDocument.createInstance("com.sun.star.style.CharacterStyle") 45 oCharStyles.InsertbyName("Log Body", oLogBodyTextStyle) 46 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 55 End If 56 oLogText.insertString(oHyperCursor, oDocument.DocumentProperties.Title, False) 57 oLogText.insertControlCharacter(oHyperCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False) 58 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") 65 LogIndex = 0 66 67 ' Get the Properties of the document 68 GetDocumentProps() 69 70 Select Case sDocType 71 Case "swriter" 72 GetWriterStrings() 73 Case "scalc" 74 GetCalcStrings() 75 Case "sdraw", "simpress" 76 GetDrawStrings() 77 Case Else 78 Msgbox("This macro only works with a Writer, Calc or Draw/Impress document.", 16, GetProductName()) 79 End Select 80 End If 81End Sub 82 83 84' ***********************************************Calc-Documents************************************************** 85 86Sub GetCalcStrings() 87Dim i, n as integer 88Dim oSheet as Object 89Dim SheetName as String 90Dim oSheets as Object 91 ' Create a sequence of all sheets within the document 92 oSheets = oDocument.Sheets 93 94 For i = 0 to osheets.Count - 1 95 oSheet = osheets.GetbyIndex(i) 96 SheetName = oSheet.Name 97 MakeLogHeadLine("Sheet No. " & i & "(" & SheetName & ")" ) 98 99 ' Check the "body" of the sheet 100 GetCellTexts(oSheet) 101 102 If oSheet.IsScenario then 103 MakeLogHeadLine("Scenario Comments from " & SheetName & "'") 104 WriteStringtoLogFile(osheet.ScenarioComment) 105 End if 106 107 GetAnnotations(oSheet, "Annotations from '" & SheetName & "'") 108 109 GetChartStrings(oSheet, "Charts from '" & SheetName & "'") 110 111 GetControlStrings(oSheet.DrawPage, "Controls from '" & SheetName & "'") 112 Next 113 114 ' Pictures 115 GetCalcGraphicNames() 116 117 GetNamedRanges() 118End Sub 119 120 121Sub GetCellTexts(oSheet as Object) 122Dim 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) 130 End If 131 Wend 132End Sub 133 134 135Sub GetAnnotations(oSheet as Object, HeaderLine as String) 136Dim oNotes as Object 137Dim n as Integer 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) 143 Next 144 End if 145End Sub 146 147 148Sub GetNamedRanges() 149Dim i as integer 150 MakeLogHeadLine("Named Ranges") 151 For i = 0 To oDocument.NamedRanges.Count - 1 152 WriteStringtoLogFile(oDocument.NamedRanges.GetbyIndex(i).Name) 153 Next 154End Sub 155 156 157Sub GetCalcGraphicNames() 158Dim n,m as integer 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) 163 Next m 164 Next n 165End Sub 166 167 168' ***********************************************Writer-Documents************************************************** 169 170Sub GetParagraphTexts(oParaObject as Object, HeadLine as String) 171Dim ParaEnum as Object 172Dim oPara as Object 173Dim oTextPortEnum as Object 174Dim oTextPortion as Object 175Dim i as integer 176Dim oCellNames() 177Dim oCell as Object 178 179 MakeLogHeadLine(HeadLine) 180 ParaEnum = oParaObject.Text.CreateEnumeration 181 182 While ParaEnum.HasMoreElements 183 oPara = ParaEnum.NextElement 184 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) 193 Wend 194 Else 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) 201 End If 202 Next 203 End If 204 Wend 205End Sub 206 207 208 209Sub GetChartStrings(oSheet as Object, HeaderLine as String) 210Dim i as Integer 211Dim aChartObject as Object 212Dim aChartDiagram as Object 213 214 MakeLogHeadLine(HeaderLine) 215 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) 220 End If 221 222 If aChartObject.HasMainTitle then 223 WriteStringToLogFile(aChartObject.Title.String) 224 End If 225 226 aChartDiagram = aChartObject.Diagram 227 228 If aChartDiagram.hasXAxisTitle Then 229 WriteStringToLogFile(aChartDiagram.XAxisTitle) 230 End If 231 232 If aChartDiagram.hasYAxisTitle Then 233 WriteStringToLogFile(aChartDiagram.YAxisTitle) 234 End If 235 236 If aChartDiagram.hasZAxisTitle Then 237 WriteStringToLogFile(aChartDiagram.ZAxisTitle) 238 End If 239 Next i 240End Sub 241 242 243 244Sub GetFrameTexts() 245Dim i as integer 246Dim oTextFrame as object 247Dim oFrameEnum as Object 248Dim oFramePort as Object 249Dim oFrameTextEnum as Object 250Dim oFrameTextPort as Object 251 252 MakeLogHeadLine("Text Frames") 253 For i = 0 to oDocument.TextFrames.Count-1 254 oTextFrame = oDocument.TextFrames.GetbyIndex(i) 255 WriteStringToLogFile(oTextFrame.Name) 256 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") 260 End If 261 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) 271 End If 272 Wend 273 Else 274 WriteStringtoLogFile(oFramePort.Name) 275 End if 276 Wend 277 Next 278End Sub 279 280 281Sub GetTextFieldStrings() 282Dim aTextField as Object 283Dim i as integer 284Dim 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) 296 End If 297 Wend 298End Sub 299 300 301 302Sub GetLinkedFileNames() 303Dim oDocSections as Object 304Dim LinkedFileName as String 305Dim i as Integer 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) 313 End If 314 Next i 315 End If 316End Sub 317 318 319Sub GetSectionNames() 320Dim i as integer 321Dim oDocSections as Object 322 MakeLogHeadLine("Sections") 323 oDocSections = oDocument.TextSections 324 For i = 0 to oDocSections.Count-1 325 WriteStringtoLogFile(oDocSections.GetbyIndex(i).Name) 326 Next 327End Sub 328 329 330Sub GetWriterStrings() 331 GetParagraphTexts(oDocument, "Document Body") 332 GetGraphicNames() 333 GetStyles() 334 GetControlStrings(oDocument.DrawPage, "Controls") 335 GetTextFieldStrings() 336 GetSectionNames() 337 GetFrameTexts() 338 GetHyperLinks 339 GetLinkedFileNames() 340End Sub 341 342 343' ***********************************************Draw-Documents************************************************** 344 345Sub GetDrawPageTitles(LocObject as Object) 346Dim n as integer 347Dim oPage as Object 348 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) 357 End If 358 End If 359 Next 360End Sub 361 362 363Sub GetPageStrings(oPages as Object) 364Dim m, n, s as Integer 365Dim 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) 375 Next s 376 ElseIf HasUnoInterfaces(oPageElement, "com.sun.star.text.XText") Then 377 WriteStringtoLogFile(oPageElement.String) 378 End If 379 Next 380 End If 381 Next 382End Sub 383 384 385Sub GetDrawStrings() 386Dim oDPages, oMPages as Object 387 388 oDPages = oDocument.DrawPages 389 oMPages = oDocument.Masterpages 390 391 MakeLogHeadLine("Titles") 392 GetDrawPageTitles(oDPages) 393 GetDrawPageTitles(oMPages) 394 395 MakeLogHeadLine("Document Body") 396 GetPageStrings(oDPages) 397 GetPageStrings(oMPages) 398End Sub 399 400 401' ***********************************************Misc************************************************** 402 403Sub GetDocumentProps() 404Dim 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) 415End Sub 416 417 418Sub GetHyperlinks() 419Dim i as integer 420Dim oCrsr as Object 421Dim oAllHyperLinks as Object 422Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue 423Dim oSearchDesc as Object 424 425 MakeLogHeadLine("Hyperlinks") 426 ' create a Search-Descriptor 427 oSearchDesc = oDocument.CreateSearchDescriptor 428 oSearchDesc.Valuesearch = False 429 430 ' define the Search-attributes 431 srchattributes(0).Name = "HyperLinkURL" 432 srchattributes(0).Value = "" 433 oSearchDesc.SetSearchAttributes(SrchAttributes()) 434 435 oAllHyperLinks = oDocument.findAll(oSearchDesc()) 436 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 443 Next i 444End Sub 445 446 447Sub GetGraphicNames() 448Dim i as integer 449Dim oDocGraphics as Object 450 MakeLogHeadLine("Graphics") 451 oDocGraphics = oDocument.GraphicObjects 452 For i = 0 to oDocGraphics.count - 1 453 WriteStringtoLogFile(oDocGraphics.GetbyIndex(i).Name) 454 Next 455End Sub 456 457 458Sub GetStyles() 459Dim m,n as integer 460 MakeLogHeadLine("User-defined Templates") 461 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) 467 End If 468 Next 469 Next 470End Sub 471 472 473Sub GetControlStrings(oDPage as Object, HeaderLine as String) 474Dim aForm as Object 475Dim m,n as integer 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)) 482 Next 483 Next 484End Sub 485 486 487Sub GetControlContent(LocControl as Object) 488Dim i as integer 489 490 If LocControl.PropertySetInfo.HasPropertybyName("Label") then 491 WriteStringtoLogFile(LocControl.Label) 492 493 ElseIf LocControl.SupportsService("com.sun.star.form.component.ListBox") then 494 For i = 0 to Ubound(LocControl.StringItemList()) 495 WriteStringtoLogFile(LocControl.StringItemList(i)) 496 Next 497 End If 498 If LocControl.PropertySetInfo.HasPropertybyName("HelpText") then 499 WriteStringtoLogFile(LocControl.Helptext) 500 End If 501End Sub 502 503' ***********************************************LogDocument************************************************** 504 505Sub 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) 511 End If 512End Sub 513 514 515Sub 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" 521End Sub 522</script:module> 523