home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Bureautique / OpenOffice / Apache_OpenOffice_4.1.1_Win_x86_install_fr.exe / openoffice1.cab / GetTexts.xba < prev    next >
Extensible Markup Language  |  2014-02-25  |  18KB  |  543 lines

  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.  
  31. Public oLogDocument, oLogText, oLogCursor, oLogHeaderStyle, oLogBodyTextStyle as Object
  32. Public oDocument as Object
  33. Public LogArray(1000) as String
  34. Public LogIndex as Integer
  35. Public oLocHeaderStyle as Object
  36.  
  37. Sub Main
  38. Dim sDocType as String
  39. Dim oHyperCursor as Object
  40. Dim 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
  101. End Sub
  102.  
  103.  
  104. ' ***********************************************Calc-Documents**************************************************
  105.  
  106. Sub GetCalcStrings()
  107. Dim i, n as integer
  108. Dim oSheet as Object
  109. Dim SheetName as String
  110. Dim 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()
  138. End Sub
  139.  
  140.  
  141. Sub GetCellTexts(oSheet as Object)
  142. Dim 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
  152. End Sub
  153.  
  154.  
  155. Sub GetAnnotations(oSheet as Object, HeaderLine as String)
  156. Dim oNotes as Object
  157. Dim 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
  165. End Sub
  166.  
  167.  
  168. Sub GetNamedRanges()
  169. Dim 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
  174. End Sub
  175.  
  176.  
  177. Sub GetCalcGraphicNames()
  178. Dim 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
  185. End Sub
  186.  
  187.  
  188. ' ***********************************************Writer-Documents**************************************************
  189.  
  190. Sub GetParagraphTexts(oParaObject as Object, HeadLine as String)
  191. Dim ParaEnum as Object
  192. Dim oPara as Object
  193. Dim oTextPortEnum as Object
  194. Dim oTextPortion as Object
  195. Dim i as integer
  196. Dim oCellNames()
  197. Dim 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
  225. End Sub
  226.  
  227.  
  228.  
  229. Sub GetChartStrings(oSheet as Object, HeaderLine as String)
  230. Dim i as Integer
  231. Dim aChartObject as Object
  232. Dim 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
  260. End Sub
  261.  
  262.  
  263.  
  264. Sub GetFrameTexts()
  265. Dim i as integer
  266. Dim oTextFrame as object
  267. Dim oFrameEnum as Object
  268. Dim oFramePort as Object
  269. Dim oFrameTextEnum as Object
  270. Dim 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
  298. End Sub
  299.  
  300.  
  301. Sub GetTextFieldStrings()
  302. Dim aTextField as Object
  303. Dim i as integer
  304. Dim 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
  318. End Sub
  319.  
  320.  
  321.  
  322. Sub GetLinkedFileNames()
  323. Dim oDocSections as Object
  324. Dim LinkedFileName as String
  325. Dim 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
  336. End Sub
  337.  
  338.  
  339. Sub GetSectionNames()
  340. Dim i as integer
  341. Dim 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
  347. End Sub
  348.  
  349.  
  350. Sub 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()
  360. End Sub
  361.  
  362.  
  363. ' ***********************************************Draw-Documents**************************************************
  364.  
  365. Sub GetDrawPageTitles(LocObject as Object)
  366. Dim n as integer
  367. Dim 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
  380. End Sub
  381.  
  382.  
  383. Sub GetPageStrings(oPages as Object)
  384. Dim m, n, s as Integer
  385. Dim 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
  402. End Sub
  403.  
  404.  
  405. Sub GetDrawStrings()
  406. Dim 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)
  418. End Sub
  419.  
  420.  
  421. ' ***********************************************Misc**************************************************
  422.  
  423. Sub GetDocumentProps()
  424. Dim 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)
  435. End Sub
  436.  
  437.  
  438. Sub GetHyperlinks()
  439. Dim i as integer
  440. Dim oCrsr as Object
  441. Dim oAllHyperLinks as Object
  442. Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue
  443. Dim 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
  464. End Sub
  465.  
  466.  
  467. Sub GetGraphicNames()
  468. Dim i as integer
  469. Dim 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
  475. End Sub
  476.  
  477.  
  478. Sub GetStyles()
  479. Dim 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
  490. End Sub
  491.  
  492.  
  493. Sub GetControlStrings(oDPage as Object, HeaderLine as String)
  494. Dim aForm as Object
  495. Dim 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
  504. End Sub
  505.  
  506.  
  507. Sub GetControlContent(LocControl as Object)
  508. Dim 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
  521. End Sub
  522.  
  523. ' ***********************************************LogDocument**************************************************
  524.  
  525. Sub 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
  532. End Sub
  533.  
  534.  
  535. Sub 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"
  541. End Sub
  542. </script:module>
  543.