Previously, we created a simple macro that allows you to look up the currently selected word in online references. While the macro is useful as it is, it has one serious drawback: since the user can't interact with it, you need to create a separate routine for each online reference. This means that if you wish to have 10 online references, you are looking at creating 10 different macros where the only difference is the URL of the online references. Obviously, a more efficient solution would be a macro that allows users to choose a reference from a list and then pick up the appropriate URL.
OOoBasic keeps macros and dialogs (which act as the macros' GUI) separately, so to keep things tidy, start by creating a new library for a new lookup macro and its dialog. To do this, choose Tools -> Macros -> Organize Macros -> OpenOffice.org Basic, press the Organizer button, select My Macros, and create a new library called GUI. Press the Edit button to open the OOoBasic IDE.
Before you start writing the actual macro, you have to build a dialog. To do this, right-click on the Module1 tab at the bottom of the window and select Insert -> BASIC Dialog. This creates a new dialog and opens it for editing. Use the provided tools to create a simple dialog containing three elements: a list box and OK and Cancel buttons. Double-click on the list box and make sure that its name is ListBox1. In the List entries, enter the names of the references you want to use in the macro (for example, "Wikipedia," "Cambridge Dictionary," and "Google Define"). To see what the created dialog looks like in action, press the Test Mode button.
Once you're done with the dialog, you can write the macro. Switch to the Module1 tab, and, as always, start by defining variables:
Sub LookupWithGUI() Dim Dialog As Object, Library As Object Dim TheDialog As Object, DialogField As Object Dim exitOK As String , CurrentItemPos As Integer
You will need the Dialog, Library, TheDialog, and DialogField variables to link the macro to the created dialog, initiate it, and point the macro to the right field. The CurrentItemPos variable will be used to store the position of the item selected from the list.
The next step is to specify the URL of the references you want to use in the macro. There are several ways to do this; in this case, we use a simple array to store all the URLs:
We've used the next code block in almost every macro we've created so far in this series of articles, so it hardly needs any further explanation:
Next step is to point the macro to the created dialog:
|Click to enlarge|
As you can see, linking the macro to the dialog is straightforward: first, the DialogLibraries.GetByName() function selects the appropriate library (GUI, in this case), then the Library.GetByName() function retrieves the name of the dialog (Dialog1). Once the macro knows which dialog to use, it must initiate it and choose the list box field:
When the user selects an item from the list and clicks on the OK button, the macro obtains the position of the selected item:
If Dialog.Execute = exitOK Then CurrentItemPos = DialogField.SelectedItemPos
Finally, the macro uses the position to get the correct URL from the URLArray and sends it to the browser, then disposes of the dialog window:
Shell("C:\Program Files\Mozilla Firefox\firefox.exe",1, URLArray(CurrentItemPos) & LookupWord.String) End If Dialog.Dispose End Sub
And here is the final result:
Sub LookupWithGUI() Dim Dialog As Object, Library As Object Dim TheDialog As Object, DialogField As Object Dim exitOK As String, CurrentItemPos As Integer URLArray=Array("http://en.wikipedia.org/wiki/",_ "http://dictionary.cambridge.org/results.asp?searchword=",_ "http://www.google.com/search?q=define%3A") ThisDoc=ThisComponent LookupWord=ThisDoc.CurrentController.getViewCursor exitOK=com.sun.star.ui.dialogs.ExecutableDialogResults.OK Library=DialogLibraries.GetByName("GUI") TheDialog=Library.GetByName("Dialog1") Dialog=CreateUnoDialog(TheDialog) DialogField=Dialog.GetControl("ListBox1") DialogField.SelectItemPos(1, True) If Dialog.Execute=exitOK Then CurrentItemPos=DialogField.SelectedItemPos Shell("C:\Program Files\Mozilla Firefox\firefox.exe",1, URLArray(CurrentItemPos) & LookupWord.String) End If Dialog.Dispose End Sub
With this GUI version of the macro, you don't have to write a new routine for every new online reference in the macro. Instead, simply add the reference's URL to the array, and add the related item to the list box in the dialog.
Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, US, German, and Danish computer magazines.