home *** CD-ROM | disk | FTP | other *** search
/ Kompuutteri Kaikille K-CD 2002 #3 / K-CD_2002-03.iso / OpenOffice / f_0178 / GetTexts.xba < prev    next >
Extensible Markup Language  |  2001-10-08  |  17KB  |  529 lines

  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. ' Option f├╝r doppelte Strings
  5. ' Alternativtexte, Namen usw f├╝r HTML-Seiten--> ist Anbindung an StarOfficeAPI geplant?
  6. ' Verlustanzeige "Wo ist Peggy?"
  7. ' ├£berschriften f├╝r Textfelder
  8. ' GetTextFrames mit Peter absprechen
  9. ' Ole - Objekte auch mit einbeziehen?
  10. ' Redimensionierung des LogArrays, wenn Implementierung so weit ist
  11. ' Namen von Notizenseiten mit Peter durchsprechen
  12.  
  13. ' Macro-Description:
  14. ' This Macro extracts the Strings out of the currently activated document und inserts them into a logdocument
  15. ' The aim of the macro is to provide the programmer an insight into the StarOffice API
  16. ' It focusses on how document-Objects are accessed.
  17. ' Therefor not only texts of the document-body are retrieved but also Texts of general
  18. ' document Objects like, Annotations, charts and general Document Information
  19.  
  20. Public oLogDocument, oLogText, oLogCursor, oLogHeaderStyle, oLogBodyTextStyle as Object
  21. Public oDocument as Object
  22. Public LogArray(1000) as String
  23. Public LogIndex as Integer
  24. Public oLocHeaderStyle as Object
  25.  
  26. Sub Main
  27. Dim sDocType as String
  28. Dim oHyperCursor as Object
  29. Dim oCharStyles as Object
  30.     BasicLibraries.LoadLibrary("Tools")
  31.     On Local Error GoTo NODOCUMENT
  32.     oDocument = StarDesktop.ActiveFrame.Controller.Model
  33.     sDocType = GetDocumentType(oDocument)
  34.     NODOCUMENT:
  35.     If Err <> 0 Then
  36.         Msgbox("This macro extracts all data from the active Writer, Calc or Draw document." & chr(13) &_
  37.                "To start this macro you have to activate a document first." , 16, GetProductName)
  38.         Exit Sub
  39.     End If
  40.     On Local Error Goto 0
  41.  
  42.     ' Open a new document where all the texts are inserted
  43.     oLogDocument = StarDesktop.LoadComponentFromURL( "private:factory/swriter","_blank",0,NoArgs())
  44.     oLogText = oLogDocument.Text
  45.  
  46.     ' create and define the character styles of the Log-document
  47.     oCharStyles = oLogDocument.StyleFamilies.GetByName("CharacterStyles")
  48.     oLogHeaderStyle = oLogDocument.createInstance("com.sun.star.style.CharacterStyle")
  49.     oCharStyles.InsertbyName("Log Header", oLogHeaderStyle)
  50.  
  51.     oLogHeaderStyle.charWeight = com.sun.star.awt.FontWeight.BOLD
  52.     oLogBodyTextStyle = oLogDocument.createInstance("com.sun.star.style.CharacterStyle")
  53.     oCharStyles.InsertbyName("Log Body", oLogBodyTextStyle)
  54.  
  55.     ' Insert the title of the activated document as a hyperlink
  56.     oHyperCursor = oLogText.createTextCursor()
  57.     oHyperCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
  58.     oHyperCursor.gotoStart(False)
  59.     oHyperCursor.HyperLinkURL = oDocument.URL
  60.     oHyperCursor.HyperLinkTarget = oDocument.URL
  61.     If oDocument.DocumentInfo.Title <> "" Then
  62.         oHyperCursor.HyperlinkName = oDocument.DocumentInfo.Title
  63.     End If
  64.     oLogText.insertString(oHyperCursor, oDocument.DocumentInfo.Title, False)
  65.     oLogText.insertControlCharacter(oHyperCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
  66.  
  67.     oLogCursor = oLogText.createTextCursor()
  68.     oLogCursor.GotoEnd(False)
  69.     ' "Switch off" the Hyperlink - Properties
  70.     oLogCursor.SetPropertyToDefault("HyperLinkURL")
  71.     oLogCursor.SetPropertyToDefault("HyperLinkTarget")
  72.     oLogCursor.SetPropertyToDefault("HyperLinkName")
  73.     LogIndex = 0
  74.  
  75.     ' Get the Properties of the document Info
  76.     GetDocumentInfo()
  77.  
  78.     Select Case sDocType
  79.         Case "swriter"
  80.             GetWriterStrings()
  81.         Case "scalc"
  82.             GetCalcStrings()
  83.         Case "sdraw"
  84.             GetDrawStrings()
  85.         Case Else
  86.             Msgbox("This macro only works with a Writer, Calc or Draw/Impress document.", 16, GetProductName())
  87.     End Select
  88.  
  89. End Sub
  90.  
  91.  
  92. ' ***********************************************Calc-Documents**************************************************
  93.  
  94. Sub GetCalcStrings()
  95. Dim i, n as integer
  96. Dim oSheet as Object
  97. Dim SheetName as String
  98. Dim oSheets as Object
  99.     ' Create a sequence of all sheets within the document
  100.     oSheets = oDocument.Sheets
  101.  
  102.     For i = 0 to osheets.Count - 1
  103.         oSheet = osheets.GetbyIndex(i)
  104.         SheetName = oSheet.Name
  105.         MakeLogHeadLine("Sheet No. " & i & "(" & SheetName & ")" )
  106.  
  107.         ' Check the "body" of the sheet
  108.         GetCellTexts(oSheet)
  109.  
  110.         If oSheet.IsScenario then
  111.             MakeLogHeadLine("Scenario Comments from " & SheetName & "'")
  112.             WriteStringtoLogFile(osheet.ScenarioComment)
  113.         End if
  114.  
  115.         GetAnnotations(oSheet, "Annotations from '" & SheetName & "'")
  116.  
  117.         GetChartStrings(oSheet, "Charts from '" & SheetName & "'")
  118.  
  119.         GetControlStrings(oSheet.DrawPage, "Controls from '" & SheetName & "'")
  120.     Next
  121.  
  122.     ' Pictures
  123.     GetCalcGraphicNames()
  124.  
  125.     GetNamedRanges()
  126. End Sub
  127.  
  128.  
  129. Sub GetCellTexts(oSheet as Object)
  130. Dim BigRange, BigEnum, oCell as Object
  131.     BigRange = oDocument.CreateInstance("com.sun.star.sheet.SheetCellRanges")
  132.     BigRange.InsertbyName("",oSheet)
  133.     BigEnum = BigRange.GetCells.CreateEnumeration
  134.     While BigEnum.hasmoreElements
  135.         oCell = BigEnum.NextElement
  136.         If oCell.String <> "" And Val(oCell.String) = 0then
  137.             WriteStringtoLogFile(oCell.String)
  138.         End If
  139.     Wend
  140. End Sub
  141.  
  142.  
  143. Sub GetAnnotations(oSheet as Object, HeaderLine as String)
  144. Dim oNotes as Object
  145. Dim n as Integer
  146.     oNotes = oSheet.getAnnotations
  147.     If oNotes.hasElements() then
  148.         MakeLogHeadLine(HeaderLine)
  149.         For n = 0 to oNotes.Count-1
  150.             WriteStringtoLogFile(oNotes.GetbyIndex(n).String)
  151.         Next
  152.     End if
  153. End Sub
  154.  
  155.  
  156. Sub GetNamedRanges()
  157. Dim i as integer
  158.     MakeLogHeadLine("Named Ranges")
  159.     For i = 0 To oDocument.NamedRanges.Count - 1
  160.         WriteStringtoLogFile(oDocument.NamedRanges.GetbyIndex(i).Name)
  161.     Next
  162. End Sub
  163.  
  164.  
  165. Sub GetCalcGraphicNames()
  166. Dim n,m as integer
  167.     MakeLogHeadLine("Graphics")
  168.     For n = 0 To oDocument.Drawpages.count-1
  169.         For m = 0 To oDocument.Drawpages.GetbyIndex(n).Count - 1
  170.             WriteStringtoLogFile(oDocument.DrawPages.GetbyIndex(n).GetbyIndex(m).Text.String)
  171.         Next m
  172.     Next n
  173. End Sub
  174.  
  175.  
  176. ' ***********************************************Writer-Documents**************************************************
  177.  
  178. Sub GetParagraphTexts(oParaObject as Object, HeadLine as String)
  179. Dim ParaEnum as Object
  180. Dim oPara as Object
  181. Dim oTextPortEnum as Object
  182. Dim oTextPortion as Object
  183. Dim i as integer
  184. Dim oCellNames()
  185. Dim oCell as Object
  186.  
  187.     MakeLogHeadLine(HeadLine)
  188.     ParaEnum = oParaObject.Text.CreateEnumeration
  189.  
  190.     While ParaEnum.HasMoreElements
  191.         oPara = ParaEnum.NextElement
  192.  
  193.         ' Note: The Enumeration ParaEnum lists all tables and Paragraphs.
  194.         ' Therefor we have to find out what kind of object "oPara" actually is
  195.         If oPara.supportsService("com.sun.star.text.Paragraph") Then
  196.             ' "oPara" is a Paragraph
  197.             oTextPortEnum = oPara.createEnumeration
  198.             While oTextPortEnum.hasmoreElements
  199.                 oTextPortion = oTextPortEnum.nextElement()
  200.                 WriteStringToLogFile(oTextPortion.String)
  201.             Wend
  202.         Else
  203.             ' "oPara" is a table
  204.             oCellNames = oPara.CellNames
  205.             For i = 0 To Ubound(oCellNames())
  206.                 If oCellNames(i) <> "" Then
  207.                     oCell = oPara.getCellByName(oCellNames(i))
  208.                     WriteStringToLogFile(oCell.String)
  209.                 End If
  210.             Next
  211.         End If
  212.     Wend
  213. End Sub
  214.  
  215.  
  216.  
  217. Sub GetChartStrings(oSheet as Object, HeaderLine as String)
  218. Dim i as Integer
  219. Dim aChartObject as Object
  220. Dim aChartDiagram as Object
  221.  
  222.     MakeLogHeadLine(HeaderLine)
  223.  
  224.     For i = 0 to oSheet.Charts.Count-1
  225.         aChartObject = oSheet.Charts.GetByIndex(i).EmbeddedObject
  226.         If aChartObject.HasSubTitle then
  227.             WriteStringToLogFile(aChartObject.SubTitle.String)
  228.         End If
  229.  
  230.         If aChartObject.HasMainTitle then
  231.             WriteStringToLogFile(aChartObject.Title.String)
  232.         End If
  233.  
  234.         aChartDiagram = aChartObject.Diagram
  235.  
  236.         If aChartDiagram.hasXAxisTitle Then
  237.             WriteStringToLogFile(aChartDiagram.XAxisTitle)
  238.         End If
  239.  
  240.         If aChartDiagram.hasYAxisTitle Then
  241.             WriteStringToLogFile(aChartDiagram.YAxisTitle)
  242.         End If
  243.  
  244.         If aChartDiagram.hasZAxisTitle Then
  245.             WriteStringToLogFile(aChartDiagram.ZAxisTitle)
  246.         End If
  247.     Next i
  248. End Sub
  249.  
  250.  
  251.  
  252. Sub GetFrameTexts()
  253. Dim i as integer
  254. Dim oTextFrame as object
  255. Dim oFrameEnum as Object
  256. Dim oFramePort as Object
  257. Dim oFrameTextEnum as Object
  258. Dim oFrameTextPort as Object
  259.  
  260.     MakeLogHeadLine("Text Frames")
  261.     For i = 0 to oDocument.TextFrames.Count-1
  262.         oTextFrame = oDocument.TextFrames.GetbyIndex(i)
  263.         WriteStringToLogFile(oTextFrame.Name)
  264.  
  265.         ' Is the frame bound to the Page
  266.         If oTextFrame.AnchorType  = com.sun.star.text.TextContentAnchorType.AT_PAGE  Then
  267.             GetParagraphTexts(oTextFrame, "Text Frame Contents")
  268.         End If
  269.  
  270.         oFrameEnum = oTextFrame.CreateEnumeration
  271.         While oFrameEnum.HasMoreElements
  272.             oFramePort = oFrameEnum.NextElement
  273.             If oFramePort.supportsService("com.sun.star.text.Paragraph") then
  274.                 oFrameTextEnum = oFramePort.createEnumeration
  275.                 While oFrameTextEnum.HasMoreElements
  276.                     oFrameTextPort = oFrameTextEnum.NextElement
  277.                     If oFrameTextPort.SupportsService("com.sun.star.text.TextFrame") Then
  278.                         WriteStringtoLogFile(oFrameTextPort.String)
  279.                     End If
  280.                 Wend
  281.             Else
  282.                 WriteStringtoLogFile(oFramePort.Name)
  283.             End if
  284.         Wend
  285.     Next
  286. End Sub
  287.  
  288.  
  289. Sub GetTextFieldStrings()
  290. Dim aTextField as Object
  291. Dim i as integer
  292. Dim CurElement as Object
  293.     MakeLogHeadLine("Text Fields")
  294.     aTextfield = oDocument.getTextfields.CreateEnumeration
  295.     While aTextField.hasmoreElements
  296.         CurElement = aTextField.NextElement
  297.         If CurElement.PropertySetInfo.hasPropertybyName("Content") Then
  298.             WriteStringtoLogFile(CurElement.Content)
  299.         ElseIf CurElement.PropertySetInfo.hasPropertybyName("PlaceHolder") Then
  300.             WriteStringtoLogFile(CurElement.PlaceHolder)
  301.             WriteStringtoLogFile(CurElement.Hint)
  302.         ElseIf Curelement.TextFieldMaster.PropertySetInfo.HasPropertybyName("Content") then
  303.             WriteStringtoLogFile(CurElement.TextFieldMaster.Content)
  304.         End If
  305.     Wend
  306. End Sub
  307.  
  308.  
  309.  
  310. Sub GetLinkedFileNames()
  311. Dim oDocSections as Object
  312. Dim LinkedFileName as String
  313. Dim i as Integer
  314.     If Right(oDocument.URL,3) = "sgl" Then
  315.         MakeLogHeadLine("Sub-documents")
  316.         oDocSections = oDocument.TextSections
  317.         For i = 0 to oDocSections.Count - 1
  318.             LinkedFileName = oDocSections.GetbyIndex(i).FileLink.FileURL
  319.             If LinkedFileName <> "" Then
  320.                 WriteStringToLogFile(LinkedFileName)
  321.             End If
  322.         Next i
  323.     End If
  324. End Sub
  325.  
  326.  
  327. Sub GetSectionNames()
  328. Dim i as integer
  329. Dim oDocSections as Object
  330.     MakeLogHeadLine("Sections")
  331.     oDocSections = oDocument.TextSections
  332.     For i = 0 to oDocSections.Count-1
  333.         WriteStringtoLogFile(oDocSections.GetbyIndex(i).Name)
  334.     Next
  335. End Sub
  336.  
  337.  
  338. Sub GetWriterStrings()
  339.     GetParagraphTexts(oDocument, "Document Body")
  340.     GetGraphicNames()
  341.     GetStyles()
  342.     GetControlStrings(oDocument.DrawPage, "Controls")
  343.     GetTextFieldStrings()
  344.     GetSectionNames()
  345.     GetFrameTexts()
  346.     GetHyperLinks
  347.     GetLinkedFileNames()
  348. End Sub
  349.  
  350.  
  351. ' ***********************************************Draw-Documents**************************************************
  352.  
  353. Sub GetDrawPageTitles(LocObject as Object)
  354. Dim n as integer
  355. Dim oPage as Object
  356.  
  357.     For n = 0 to LocObject.Count - 1
  358.         oPage = LocObject.GetbyIndex(n)
  359.         WriteStringtoLogFile(oPage.Name)
  360.         ' Is the Page a DrawPage and not a MasterPage?
  361.         If oPage.supportsService("com.sun.star.drawing.DrawPage")then
  362.             ' Get the Name of the NotesPage (only relevant for Impress-Documents)
  363.             If oDocument.supportsService("com.sun.star.presentation.PresentationDocument") then
  364.                 WriteStringtoLogFile(oPage.NotesPage.Name)
  365.             End If
  366.         End If
  367.     Next
  368. End Sub
  369.  
  370.  
  371. Sub GetPageStrings(oPages as Object)
  372. Dim m, n, s as Integer
  373. Dim oPage, oPageElement, oShape as Object
  374.     For n = 0 to oPages.Count-1
  375.         oPage = oPages.GetbyIndex(n)
  376.         If oPage.HasElements then
  377.             For m = 0 to oPage.Count-1
  378.                 oPageElement = oPage.GetByIndex(m)
  379.                 If HasUnoInterfaces(oPageElement,"com.sun.star.container.XIndexAccess") Then
  380.                     ' The Object "oPageElement" a group of Shapes, that can be accessed by their index
  381.                     For s = 0 To oPageElement.Count - 1
  382.                         WriteStringToLogFile(oPageElement.GetByIndex(s).String)
  383.                     Next s
  384.                 ElseIf HasUnoInterfaces(oPageElement, "com.sun.star.text.XText") Then
  385.                     WriteStringtoLogFile(oPageElement.String)
  386.                 End If
  387.             Next
  388.         End If
  389.     Next
  390. End Sub
  391.  
  392.  
  393. Sub GetDrawStrings()
  394. Dim oDPages, oMPages as Object
  395.  
  396.     oDPages = oDocument.DrawPages
  397.     oMPages = oDocument.Masterpages
  398.  
  399.     MakeLogHeadLine("Titles")
  400.     GetDrawPageTitles(oDPages)
  401.     GetDrawPageTitles(oMPages)
  402.  
  403.     MakeLogHeadLine("Document Body")
  404.     GetPageStrings(oDPages)
  405.     GetPageStrings(oMPages)
  406. End Sub
  407.  
  408.  
  409. ' ***********************************************Misc**************************************************
  410.  
  411. Sub GetDocumentInfo()
  412. Dim oDocuInfo as Object
  413.     MakeLogHeadLine("Document Properties")
  414.     oDocuInfo = oDocument.DocumentInfo
  415.     WriteStringToLogFile(oDocuInfo.Title)
  416.     WriteStringToLogFile(oDocuInfo.Description)
  417.     WriteStringToLogFile(oDocuInfo.Theme)
  418.     WriteStringToLogFile(oDocuInfo.Author)
  419.     WriteStringToLogFile(oDocuInfo.ReplyTo)
  420.     WriteStringToLogFile(oDocuInfo.Recipient)
  421.     WriteStringToLogFile(oDocuInfo.References)
  422.     WriteStringToLogFile(oDocuInfo.Keywords)
  423. End Sub
  424.  
  425.  
  426. Sub GetHyperlinks()
  427. Dim i as integer
  428. Dim oCrsr as Object
  429. Dim oAllHyperLinks as Object
  430. Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue
  431. Dim oSearchDesc as Object
  432.  
  433.     MakeLogHeadLine("Hyperlinks")
  434.     ' create a Search-Descriptor
  435.     oSearchDesc = oDocument.CreateSearchDescriptor
  436.     oSearchDesc.Valuesearch = False
  437.  
  438.     ' define the Search-attributes
  439.     srchattributes(0).Name = "HyperLinkURL"
  440.     srchattributes(0).Value = ""
  441.     oSearchDesc.SetSearchAttributes(SrchAttributes())
  442.  
  443.     oAllHyperLinks = oDocument.findAll(oSearchDesc())
  444.  
  445.     For i = 0 to oAllHyperLinks.Count - 1
  446.         oFound = oAllHyperLinks(i)
  447.         oCrsr = oFound.Text.createTextCursorByRange(oFound)
  448.         WriteStringToLogFile(oCrs.HyperLinkURL)       'Url
  449.         WriteStringToLogFile(oCrs.HyperLinkTarget)    'Name
  450.         WriteStringToLogFile(oCrs.HyperLinkName)    'Frame
  451.     Next i
  452. End Sub
  453.  
  454.  
  455. Sub GetGraphicNames()
  456. Dim i as integer
  457. Dim oDocGraphics as Object
  458.     MakeLogHeadLine("Graphics")
  459.     oDocGraphics = oDocument.GraphicObjects
  460.     For i = 0 to oDocGraphics.count - 1
  461.         WriteStringtoLogFile(oDocGraphics.GetbyIndex(i).Name)
  462.     Next
  463. End Sub
  464.  
  465.  
  466. Sub GetStyles()
  467. Dim m,n as integer
  468.     MakeLogHeadLine("User-defined Templates")
  469.  
  470.     ' Check all StyleFamilies(i.e. PageStyles, ParagraphStyles, CharacterStyles, cellStyles)
  471.     For n = 0 to oDocument.StyleFamilies.Count - 1
  472.         For m = 0 to oDocument.StyleFamilies.getbyIndex(n).Count-1
  473.             If oDocument.StyleFamilies.GetbyIndex(n).getbyIndex(m).IsUserDefined then
  474.                 WriteStringtoLogFile(oDocument.StyleFamilies.GetbyIndex(n).getbyIndex(m).Name)
  475.             End If
  476.         Next
  477.     Next
  478. End Sub
  479.  
  480.  
  481. Sub GetControlStrings(oDPage as Object, HeaderLine as String)
  482. Dim aForm as Object
  483. Dim m,n as integer
  484.     MakeLogHeadLine(HeaderLine)
  485.     'SearchFor all possible Controls
  486.     For n = 0 to oDPage.Forms.Count - 1
  487.         aForm = oDPage.Forms(n)
  488.         For m = 0 to aForm.Count-1
  489.             GetControlContent(aForm.GetbyIndex(m))
  490.         Next
  491.     Next
  492. End Sub
  493.  
  494.  
  495. Sub GetControlContent(LocControl as Object)
  496. Dim i as integer
  497.  
  498.     If LocControl.PropertySetInfo.HasPropertybyName("Label") then
  499.         WriteStringtoLogFile(LocControl.Label)
  500.  
  501.     ElseIf LocControl.SupportsService("com.sun.star.form.component.ListBox") then
  502.         For i = 0 to Ubound(LocControl.StringItemList())
  503.             WriteStringtoLogFile(LocControl.StringItemList(i))
  504.         Next
  505.     End If
  506.     If LocControl.PropertySetInfo.HasPropertybyName("HelpText") then
  507.         WriteStringtoLogFile(LocControl.Helptext)
  508.     End If
  509. End Sub
  510.  
  511. ' ***********************************************LogDocument**************************************************
  512.  
  513. Sub WriteStringtoLogFile( sString as String)
  514.     If (Not FieldInArray(LogArray(),LogIndex,sString))AND (NOT ISNULL(sString)) Then
  515.         LogArray(LogIndex) = sString
  516.         LogIndex = LogIndex + 1
  517.         oLogText.insertString(oLogCursor,sString,False)
  518.            oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
  519.     End If
  520. End Sub
  521.  
  522.  
  523. Sub MakeLogHeadLine(HeadText as String)
  524.     oLogCursor.CharStyleName = "Log Header"
  525.     oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
  526.     oLogText.insertString(oLogCursor,HeadText,False)
  527.     oLogText.insertControlCharacter(oLogCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
  528.     oLogCursor.CharStyleName = "Log Body"
  529. End Sub</script:module>