GetTexts.xba (3e02b54d) | GetTexts.xba (0a50a83b) |
---|---|
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 --- 7 unchanged lines hidden (view full) --- 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 | 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 --- 7 unchanged lines hidden (view full) --- 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 | 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 | 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") | 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 | 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) &_ | 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 | 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 | 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 --- 29 unchanged lines hidden (view full) --- 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 | 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 --- 29 unchanged lines hidden (view full) --- 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************************************************** | 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 | 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 & ")" ) | 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 --- 54 unchanged lines hidden (view full) --- 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 | 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 --- 54 unchanged lines hidden (view full) --- 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************************************************** | 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 | 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 | 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 --- 5 unchanged lines hidden (view full) --- 220 WriteStringToLogFile(oCell.String) 221 End If 222 Next 223 End If 224 Wend 225End Sub 226 227 | 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 --- 5 unchanged lines hidden (view full) --- 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 --- 18 unchanged lines hidden (view full) --- 255 256 If aChartDiagram.hasZAxisTitle Then 257 WriteStringToLogFile(aChartDiagram.ZAxisTitle) 258 End If 259 Next i 260End Sub 261 262 | 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 --- 18 unchanged lines hidden (view full) --- 254 255 If aChartDiagram.hasZAxisTitle Then 256 WriteStringToLogFile(aChartDiagram.ZAxisTitle) 257 End If 258 Next i 259End Sub 260 261 |
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 | 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 |
277 ' Is the frame bound to the Page 278 If oTextFrame.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE Then | 275 ' Is the frame bound to the page? 276 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 --- 26 unchanged lines hidden (view full) --- 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 | 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 --- 26 unchanged lines hidden (view full) --- 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 |
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 --- 25 unchanged lines hidden (view full) --- 355 GetTextFieldStrings() 356 GetSectionNames() 357 GetFrameTexts() 358 GetHyperLinks 359 GetLinkedFileNames() 360End Sub 361 362 | 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 --- 25 unchanged lines hidden (view full) --- 352 GetTextFieldStrings() 353 GetSectionNames() 354 GetFrameTexts() 355 GetHyperLinks 356 GetLinkedFileNames() 357End Sub 358 359 |
363' ***********************************************Draw-Documents************************************************** | 360' ***********************************************Draw/Impress 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) | 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) |
372 ' Is the Page a DrawPage and not a MasterPage? | 369 ' Is the page a DrawPage and not a MasterPage? |
373 If oPage.supportsService("com.sun.star.drawing.DrawPage")then | 370 If oPage.supportsService("com.sun.star.drawing.DrawPage")then |
374 ' Get the Name of the NotesPage (only relevant for Impress-Documents) | 371 ' 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 --- 40 unchanged lines hidden (view full) --- 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) | 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 --- 40 unchanged lines hidden (view full) --- 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) |
431' WriteStringToLogFile(oDocuProps.UserDefinedProperties.ReplyTo) 432' WriteStringToLogFile(oDocuProps.UserDefinedProperties.Recipient) 433' WriteStringToLogFile(oDocuProps.UserDefinedProperties.References) 434' WriteStringToLogFile(oDocuProps.Keywords) | 428 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.ReplyTo) 429 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.Recipient) 430 ' WriteStringToLogFile(oDocuProps.UserDefinedProperties.References) 431 ' 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 --- 9 unchanged lines hidden (view full) --- 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) | 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 --- 9 unchanged lines hidden (view full) --- 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) |
460 WriteStringToLogFile(oCrs.HyperLinkURL) 'Url | 457 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 --- 46 unchanged lines hidden (view full) --- 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 | 458 WriteStringToLogFile(oCrs.HyperLinkTarget) 'Name 459 WriteStringToLogFile(oCrs.HyperLinkName) 'Frame 460 Next i 461End Sub 462 463 464Sub GetGraphicNames() 465Dim i as integer --- 46 unchanged lines hidden (view full) --- 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 |
523' ***********************************************LogDocument************************************************** | 520' ***********************************************Log document************************************************** |
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) | 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) |
530 oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False) | 527 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> | 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> |