AutoText.xba 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <!--
  4. * This file is part of the LibreOffice project.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * This file incorporates work covered by the following license notice:
  11. *
  12. * Licensed to the Apache Software Foundation (ASF) under one or more
  13. * contributor license agreements. See the NOTICE file distributed
  14. * with this work for additional information regarding copyright
  15. * ownership. The ASF licenses this file to you under the Apache
  16. * License, Version 2.0 (the "License"); you may not use this file
  17. * except in compliance with the License. You may obtain a copy of
  18. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  19. -->
  20. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="AutoText" script:language="StarBasic">&apos; BASIC
  21. Option Explicit
  22. Dim oDocument as Object
  23. Dim sDocumentTitle as String
  24. Sub Main()
  25. Dim oTable as Object
  26. Dim oRows as Object
  27. Dim oDocuText as Object
  28. Dim oAutoTextCursor as Object
  29. Dim oAutoTextContainer as Object
  30. Dim oAutogroup as Object
  31. Dim oAutoText as Object
  32. Dim oCharStyles as Object
  33. Dim oContentStyle as Object
  34. Dim oHeaderStyle as Object
  35. Dim oGroupTitleStyle as Object
  36. Dim n, m, iAutoCount as Integer
  37. BasicLibraries.LoadLibrary(&quot;Tools&quot;)
  38. sDocumentTitle = &quot;Installed AutoTexts&quot;
  39. &apos; Open a new empty document
  40. oDocument = CreateNewDocument(&quot;swriter&quot;)
  41. If Not IsNull(oDocument) Then
  42. oDocument.DocumentProperties.Title = sDocumentTitle
  43. oDocuText = oDocument.Text
  44. &apos; Create The Character-templates
  45. oCharStyles = oDocument.StyleFamilies.GetByName(&quot;CharacterStyles&quot;)
  46. &apos; The Characterstyle for the Header that describes the Title of Autotextgroups
  47. oGroupTitleStyle = oDocument.createInstance(&quot;com.sun.star.style.CharacterStyle&quot;)
  48. oCharStyles.InsertbyName(&quot;AutoTextGroupTitle&quot;, oGroupTitleStyle)
  49. oGroupTitleStyle.CharWeight = com.sun.star.awt.FontWeight.BOLD
  50. oGroupTitleStyle.CharHeight = 14
  51. &apos; The Characterstyle for the Header that describes the Title of Autotextgroups
  52. oHeaderStyle = oDocument.createInstance(&quot;com.sun.star.style.CharacterStyle&quot;)
  53. oCharStyles.InsertbyName(&quot;AutoTextHeading&quot;, oHeaderStyle)
  54. oHeaderStyle.CharWeight = com.sun.star.awt.FontWeight.BOLD
  55. &apos; &quot;Ordinary&quot; Table Content
  56. oContentStyle = oDocument.createInstance(&quot;com.sun.star.style.CharacterStyle&quot;)
  57. oCharStyles.InsertbyName(&quot;TableContent&quot;, oContentStyle)
  58. oAutoTextContainer = CreateUnoService(&quot;com.sun.star.text.AutoTextContainer&quot;)
  59. oAutoTextCursor = oDocuText.CreateTextCursor()
  60. oAutoTextCursor.CharStyleName = &quot;AutoTextGroupTitle&quot;
  61. &apos; Link the Title with the following table
  62. oAutoTextCursor.ParaKeepTogether = True
  63. For n = 0 To oAutoTextContainer.Count - 1
  64. oAutoGroup = oAutoTextContainer.GetByIndex(n)
  65. oAutoTextCursor.SetString(oAutoGroup.Title)
  66. oAutoTextCursor.CollapseToEnd()
  67. oDocuText.insertControlCharacter(oAutoTextCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
  68. oTable = oDocument.CreateInstance(&quot;com.sun.star.text.TextTable&quot;)
  69. &apos; Divide the table if necessary
  70. oTable.Split = True
  71. &apos; oTable.KeepTogether = False
  72. oTable.RepeatHeadLine = True
  73. oAutoTextCursor.Text.InsertTextContent(oAutoTextCursor,oTable,False)
  74. InsertStringToCell(&quot;AutoText Name&quot;,oTable.GetCellbyPosition(0,0), &quot;AutoTextHeading&quot;)
  75. InsertStringToCell(&quot;AutoText Shortcut&quot;,oTable.GetCellbyPosition(1,0), &quot;AutoTextHeading&quot;)
  76. &apos; Insert one row at the bottom of the table
  77. oRows = oTable.Rows
  78. iAutoCount = oAutoGroup.Count
  79. For m = 0 To iAutoCount-1
  80. &apos; Insert the name and the title of all Autotexts
  81. oAutoText = oAutoGroup.GetByIndex(m)
  82. InsertStringToCell(oAutoGroup.Titles(m), oTable.GetCellbyPosition(0, m + 1), &quot;TableContent&quot;)
  83. InsertStringToCell(oAutoGroup.ElementNames(m), oTable.GetCellbyPosition(1, m + 1), &quot;TableContent&quot;)
  84. If m &lt; iAutoCount-1 Then
  85. oRows.InsertbyIndex(m + 2,1)
  86. End If
  87. Next m
  88. oDocuText.insertControlCharacter(oAutoTextCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,False)
  89. oAutoTextCursor.CollapseToEnd()
  90. Next n
  91. End If
  92. End Sub
  93. Sub InsertStringToCell(sCellString as String, oCell as Object, sCellStyle as String)
  94. Dim oCellCursor as Object
  95. oCellCursor = oCell.CreateTextCursor()
  96. oCellCursor.CharStyleName = sCellStyle
  97. oCell.Text.insertString(oCellCursor,sCellString,False)
  98. oDocument.CurrentController.Select(oCellCursor)
  99. End Sub</script:module>