ChangeAllChars.xba 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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="ChangeAllChars" script:language="StarBasic">&apos; This macro replaces all characters in a writer-document through &quot;x&quot; or &quot;X&quot; signs.
  21. &apos; It works on the currently activated document.
  22. Private const UPPERREPLACECHAR = &quot;X&quot;
  23. Private const LOWERREPLACECHAR = &quot;x&quot;
  24. Private MSGBOXTITLE
  25. Private NOTSAVEDTEXT
  26. Private WARNING
  27. Sub ChangeAllChars &apos; Change all chars in the active document
  28. Dim oSheets, oPages as Object
  29. Dim i as Integer
  30. Const MBYES = 6
  31. Const MBABORT = 2
  32. Const MBNO = 7
  33. BasicLibraries.LoadLibrary(&quot;Tools&quot;)
  34. MSGBOXTITLE = &quot;Change All Characters to an &apos;&quot; &amp; UPPERREPLACECHAR &amp; &quot;&apos;&quot;
  35. NOTSAVEDTEXT = &quot;This document has already been modified: All characters will be changed to an &quot; &amp; UPPERREPLACECHAR &amp; &quot;&apos;. Should the document be saved now?&quot;
  36. WARNING = &quot;This macro changes all characters and numbers to an &apos;&quot; &amp; UPPERREPLACECHAR &amp; &quot;&apos; in this document.&quot;
  37. On Local Error GoTo NODOCUMENT
  38. oDocument = StarDesktop.ActiveFrame.Controller.Model
  39. NODOCUMENT:
  40. If Err &lt;&gt; 0 Then
  41. Msgbox(WARNING &amp; chr(13) &amp; &quot;First, activate a Writer document.&quot; , 16, GetProductName())
  42. Exit Sub
  43. End If
  44. On Local Error Goto 0
  45. sDocType = GetDocumentType(oDocument)
  46. If oDocument.IsModified And oDocument.Url &lt;&gt; &quot;&quot; Then
  47. Status = MsgBox(NOTSAVEDTEXT, 3+32, MSGBOXTITLE)
  48. Select Case Status
  49. Case MBYES
  50. oDocument.Store
  51. Case MBABORT, MBNO
  52. End
  53. End Select
  54. Else
  55. Status = MsgBox(WARNING, 3+32, MSGBOXTITLE)
  56. If Status = MBNO Or Status = MBABORT Then &apos; No, Abort
  57. End
  58. End If
  59. End If
  60. Select Case sDocType
  61. Case &quot;swriter&quot;
  62. ReplaceAllStrings(oDocument)
  63. Case Else
  64. Msgbox(&quot;This macro only works with Writer documents.&quot;, 16, GetProductName())
  65. End Select
  66. End Sub
  67. Sub ReplaceAllStrings(oContainer as Object)
  68. ReplaceStrings(oContainer, &quot;[a-z]&quot;, LOWERREPLACECHAR)
  69. ReplaceStrings(oContainer, &quot;[à-þ]&quot;, LOWERREPLACECHAR)
  70. ReplaceStrings(oContainer, &quot;[A-Z]&quot;, UPPERREPLACECHAR)
  71. ReplaceStrings(oContainer, &quot;[À-ß]&quot;, UPPERREPLACECHAR)
  72. ReplaceStrings(oContainer, &quot;[0-9]&quot;, UPPERREPLACECHAR)
  73. End Sub
  74. Sub ReplaceStrings(oContainer as Object, sSearchString, sReplaceString as String)
  75. oReplaceDesc = oContainer.createReplaceDescriptor()
  76. oReplaceDesc.SearchCaseSensitive = True
  77. oReplaceDesc.SearchRegularExpression = True
  78. oReplaceDesc.Searchstring = sSearchString
  79. oReplaceDesc.ReplaceString = sReplaceString
  80. oReplCount = oContainer.ReplaceAll(oReplaceDesc)
  81. End Sub</script:module>