home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 May / Gamestar_62_2004-05_dvd.iso / Programy / openoffice / f_0100 / GetTexts.xba < prev    next >
Extensible Markup Language  |  2002-10-16  |  17KB  |  521 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. ' 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.  
  11. Public oLogDocument, oLogText, oLogCursor, oLogHeaderStyle, oLogBodyTextStyle as Object
  12. Public oDocument as Object
  13. Public LogArray(1000) as String
  14. Public LogIndex as Integer
  15. Public oLocHeaderStyle as Object
  16.  
  17. Sub Main
  18. Dim sDocType as String
  19. Dim oHyperCursor as Object
  20. Dim 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.DocumentInfo.Title <> "" Then
  54.             oHyperCursor.HyperlinkName = oDocument.DocumentInfo.Title
  55.         End If
  56.         oLogText.insertString(oHyperCursor, oDocument.DocumentInfo.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 Info
  68.         GetDocumentInfo()
  69.  
  70.         Select Case sDocType
  71.             Case "swriter"
  72.                 GetWriterStrings()
  73.             Case "scalc"
  74.                 GetCalcStrings()
  75.             Case "sdraw"
  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
  81. End Sub
  82.  
  83.  
  84. ' ***********************************************Calc-Documents**************************************************
  85.  
  86. Sub GetCalcStrings()
  87. Dim i, n as integer
  88. Dim oSheet as Object
  89. Dim SheetName as String
  90. Dim 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()
  118. End Sub
  119.  
  120.  
  121. Sub GetCellTexts(oSheet as Object)
  122. Dim 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
  132. End Sub
  133.  
  134.  
  135. Sub GetAnnotations(oSheet as Object, HeaderLine as String)
  136. Dim oNotes as Object
  137. Dim 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
  145. End Sub
  146.  
  147.  
  148. Sub GetNamedRanges()
  149. Dim 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
  154. End Sub
  155.  
  156.  
  157. Sub GetCalcGraphicNames()
  158. Dim 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
  165. End Sub
  166.  
  167.  
  168. ' ***********************************************Writer-Documents**************************************************
  169.  
  170. Sub GetParagraphTexts(oParaObject as Object, HeadLine as String)
  171. Dim ParaEnum as Object
  172. Dim oPara as Object
  173. Dim oTextPortEnum as Object
  174. Dim oTextPortion as Object
  175. Dim i as integer
  176. Dim oCellNames()
  177. Dim 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
  205. End Sub
  206.  
  207.  
  208.  
  209. Sub GetChartStrings(oSheet as Object, HeaderLine as String)
  210. Dim i as Integer
  211. Dim aChartObject as Object
  212. Dim 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
  240. End Sub
  241.  
  242.  
  243.  
  244. Sub GetFrameTexts()
  245. Dim i as integer
  246. Dim oTextFrame as object
  247. Dim oFrameEnum as Object
  248. Dim oFramePort as Object
  249. Dim oFrameTextEnum as Object
  250. Dim 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
  278. End Sub
  279.  
  280.  
  281. Sub GetTextFieldStrings()
  282. Dim aTextField as Object
  283. Dim i as integer
  284. Dim 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
  298. End Sub
  299.  
  300.  
  301.  
  302. Sub GetLinkedFileNames()
  303. Dim oDocSections as Object
  304. Dim LinkedFileName as String
  305. Dim 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
  316. End Sub
  317.  
  318.  
  319. Sub GetSectionNames()
  320. Dim i as integer
  321. Dim 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
  327. End Sub
  328.  
  329.  
  330. Sub 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()
  340. End Sub
  341.  
  342.  
  343. ' ***********************************************Draw-Documents**************************************************
  344.  
  345. Sub GetDrawPageTitles(LocObject as Object)
  346. Dim n as integer
  347. Dim 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
  360. End Sub
  361.  
  362.  
  363. Sub GetPageStrings(oPages as Object)
  364. Dim m, n, s as Integer
  365. Dim 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
  382. End Sub
  383.  
  384.  
  385. Sub GetDrawStrings()
  386. Dim 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)
  398. End Sub
  399.  
  400.  
  401. ' ***********************************************Misc**************************************************
  402.  
  403. Sub GetDocumentInfo()
  404. Dim oDocuInfo as Object
  405.     MakeLogHeadLine("Document Properties")
  406.     oDocuInfo = oDocument.DocumentInfo
  407.     WriteStringToLogFile(oDocuInfo.Title)
  408.     WriteStringToLogFile(oDocuInfo.Description)
  409.     WriteStringToLogFile(oDocuInfo.Theme)
  410.     WriteStringToLogFile(oDocuInfo.Author)
  411.     WriteStringToLogFile(oDocuInfo.ReplyTo)
  412.     WriteStringToLogFile(oDocuInfo.Recipient)
  413.     WriteStringToLogFile(oDocuInfo.References)
  414.     WriteStringToLogFile(oDocuInfo.Keywords)
  415. End Sub
  416.  
  417.  
  418. Sub GetHyperlinks()
  419. Dim i as integer
  420. Dim oCrsr as Object
  421. Dim oAllHyperLinks as Object
  422. Dim SrchAttributes(0) as new com.sun.star.beans.PropertyValue
  423. Dim 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
  444. End Sub
  445.  
  446.  
  447. Sub GetGraphicNames()
  448. Dim i as integer
  449. Dim 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
  455. End Sub
  456.  
  457.  
  458. Sub GetStyles()
  459. Dim 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
  470. End Sub
  471.  
  472.  
  473. Sub GetControlStrings(oDPage as Object, HeaderLine as String)
  474. Dim aForm as Object
  475. Dim 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
  484. End Sub
  485.  
  486.  
  487. Sub GetControlContent(LocControl as Object)
  488. Dim 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
  501. End Sub
  502.  
  503. ' ***********************************************LogDocument**************************************************
  504.  
  505. Sub 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
  512. End Sub
  513.  
  514.  
  515. Sub 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"
  521. End Sub</script:module>