update dev300-m58
[ooovba.git] / scripting / examples / basic / SearchAndReplace.xba
blobd98369e9f372b58196e215e1571943b36369434d
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="SearchAndReplace" script:language="StarBasic">&apos; ***
4 &apos; SearchAndReplace basic script
5 &apos; Uses a user interface to search and replace the specified strings
6 &apos;
7 &apos; author Neil Montgomery
8 &apos; created August 12, 2002
9 &apos; ***
12 &apos; Main subprocedure to start script
13 Sub Main
14 dialogShow()
15 End Sub
18 &apos; Global reference to the dialog object
19 Dim oDialog as Object
22 &apos; Uses the loadDialog subprocedure to load and execute the dialog box
23 Sub dialogShow
24 oDialog = loadDialog(&quot;Standard&quot;,&quot;SearchAndReplaceDialog&quot;)
25 oDialog.execute()
26 End Sub
30 &apos; ***
31 &apos; Loads the dialog from the dialog library
32 &apos;
33 &apos; param Libname the library name where dialog is stored
34 &apos; param DialogName the name of the dialog
35 &apos; param oLibContainer library container to hold the loaded dialog library (optional)
36 &apos; return runtime dialog object
37 &apos; ***
38 Function loadDialog(Libname as String, DialogName as String, Optional oLibContainer)
39 Dim oLib as Object
40 Dim oLibDialog as Object
41 Dim oRuntimeDialog as Object
43 If isMissing(oLibContainer ) then
44 oLibContainer = DialogLibraries
45 End If
46 oLibContainer.loadLibrary(LibName)
47 oLib = oLibContainer.getByName(Libname)
48 oLibDialog = oLib.getByName(DialogName)
49 oRuntimeDialog = createUnoDialog(oLibDialog)
50 loadDialog() = oRuntimeDialog
51 End Function
55 &apos; ***
56 &apos; Creates a connection to the current document.
57 &apos; Gets the search and replace keys from the dialog and replaces all
58 &apos; instances of the search key with the replace key.
59 &apos;
60 &apos; ***
61 Sub getInfoFromDialog
62 Dim oDocument As Object
63 Dim oSearch As Object
64 Dim oFound As Object
65 Dim oFoundCursor As Object
66 Dim oSearchText as Object
67 Dim oReplaceText as Object
69 &apos; Create a document object for the current document then create text and
70 &apos; cursor objects
71 oDocument = StarDesktop.ActiveFrame.Controller.Model
72 oSearch = oDocument.createSearchDescriptor
74 &apos; Replace all instances of the search string with the replavce string
75 oSearch.SearchString = getSearchKey()
76 oSearch.ReplaceString = getReplaceKey()
77 oDocument.replaceAll(oSearch)
78 End Sub
81 &apos; ***
82 &apos; Gets the search key string from the dialog
83 &apos;
84 &apos; returns string representing the search key
85 &apos; ***
86 Function getSearchKey() as String
87 Dim sSearch As String
89 &apos; Get the search key from the dialog
90 oSearchText = oDialog.GetControl(&quot;SearchKeyTextBox&quot;)
91 sSearch = oSearchText.Text
92 getSearchKey = sSearch
93 End Function
97 &apos; ***
98 &apos; Gets the replace key string from the dialog
99 &apos;
100 &apos; returns string representing the replace key
101 &apos; ***
102 Function getReplaceKey() as String
103 Dim sReplace As String
105 &apos; Get the replace key from the dialog
106 oReplaceText = oDialog.GetControl(&quot;ReplaceKeyTextBox&quot;)
107 sReplace = oReplaceText.Text
108 getReplaceKey = sReplace
109 End Function</script:module>