Branch libreoffice-5-0-4
[LibreOffice.git] / scripting / examples / basic / SearchAndReplace.xba
blobfb7c60bd4f22cfd4c88294d0fe3fb9f2e6cbb72e
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.
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/.
10 * This file incorporates work covered by the following license notice:
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="SearchAndReplace" script:language="StarBasic">&apos; ***
21 &apos; SearchAndReplace basic script
22 &apos; Uses a user interface to search and replace the specified strings
23 &apos;
24 &apos; author Neil Montgomery
25 &apos; created August 12, 2002
26 &apos; ***
29 &apos; Main subprocedure to start script
30 Sub Main
31 dialogShow()
32 End Sub
35 &apos; Global reference to the dialog object
36 Dim oDialog as Object
39 &apos; Uses the loadDialog subprocedure to load and execute the dialog box
40 Sub dialogShow
41 oDialog = loadDialog(&quot;Standard&quot;,&quot;SearchAndReplaceDialog&quot;)
42 oDialog.execute()
43 End Sub
47 &apos; ***
48 &apos; Loads the dialog from the dialog library
49 &apos;
50 &apos; param Libname the library name where dialog is stored
51 &apos; param DialogName the name of the dialog
52 &apos; param oLibContainer library container to hold the loaded dialog library (optional)
53 &apos; return runtime dialog object
54 &apos; ***
55 Function loadDialog(Libname as String, DialogName as String, Optional oLibContainer)
56 Dim oLib as Object
57 Dim oLibDialog as Object
58 Dim oRuntimeDialog as Object
60 If isMissing(oLibContainer ) then
61 oLibContainer = DialogLibraries
62 End If
63 oLibContainer.loadLibrary(LibName)
64 oLib = oLibContainer.getByName(Libname)
65 oLibDialog = oLib.getByName(DialogName)
66 oRuntimeDialog = createUnoDialog(oLibDialog)
67 loadDialog() = oRuntimeDialog
68 End Function
72 &apos; ***
73 &apos; Creates a connection to the current document.
74 &apos; Gets the search and replace keys from the dialog and replaces all
75 &apos; instances of the search key with the replace key.
76 &apos;
77 &apos; ***
78 Sub getInfoFromDialog
79 Dim oDocument As Object
80 Dim oSearch As Object
81 Dim oFound As Object
82 Dim oFoundCursor As Object
83 Dim oSearchText as Object
84 Dim oReplaceText as Object
86 &apos; Create a document object for the current document then create text and
87 &apos; cursor objects
88 oDocument = StarDesktop.ActiveFrame.Controller.Model
89 oSearch = oDocument.createSearchDescriptor
91 &apos; Replace all instances of the search string with the replavce string
92 oSearch.SearchString = getSearchKey()
93 oSearch.ReplaceString = getReplaceKey()
94 oDocument.replaceAll(oSearch)
95 End Sub
98 &apos; ***
99 &apos; Gets the search key string from the dialog
100 &apos;
101 &apos; returns string representing the search key
102 &apos; ***
103 Function getSearchKey() as String
104 Dim sSearch As String
106 &apos; Get the search key from the dialog
107 oSearchText = oDialog.GetControl(&quot;SearchKeyTextBox&quot;)
108 sSearch = oSearchText.Text
109 getSearchKey = sSearch
110 End Function
114 &apos; ***
115 &apos; Gets the replace key string from the dialog
116 &apos;
117 &apos; returns string representing the replace key
118 &apos; ***
119 Function getReplaceKey() as String
120 Dim sReplace As String
122 &apos; Get the replace key from the dialog
123 oReplaceText = oDialog.GetControl(&quot;ReplaceKeyTextBox&quot;)
124 sReplace = oReplaceText.Text
125 getReplaceKey = sReplace
126 End Function</script:module>