Programming OpenOffice: Using Dialogs in Macros How to Use a Dialog to Obtain Data for an OpenOffice.org Macro
OpenOffice is a powerful suite of tools, and creating Basic Macros makes it even more versatile. Adding dialogs just gives it that human touch.
OpenOffice.org is a invaluable tool to everyone in today's office environment, consisting of:
- a spreadsheet
- a word processor
- a database
- a built in programming language
All of this means that it is very easy for anyone to build a complex application, carrying out all of the business operations required in day to day life. Unfortunately, these applications soon become very complicated and difficult for a new user to understand. Fortunately, it is also very easy to make life much simpler by:
- using macros to automate any repetitive tasks
- using dialogs to provide an interface to the application.
OpenOffice's Built-in Dialogs
OpenOffice's built-in Dialogs are very simple, and are designed to enable the application to supply information to the user, or to obtain limited information from them. The two types are:
- msgbox
- inputbox
OpenOffice's Message Box
The message box is a simple way of outputting information to a user, and allowing them some simple responses; for example this macro has a little conversation with the user:
Sub Main
Dim ip as Integer
mf = msgbox ( "Are you male?", 4, "Application Question")
select case mf
case 6
msgbox "You've said that you're male", 0, "Application Confirmation"
case 7
msgbox "You've said that you're female", 0, "Application Confirmation"
end select
End Sub
This macro uses the message box in two ways:
- firstly, to illicit a response from the user - it does this by displaying a yes-no question and then storing the answer in a variable; and all of the possible results are:
1 - OK
2 - Cancel
3 - Abort
4 - Retry
5 - Ignore
6 - Yes
7 - No - secondly, to display an output which is dependant on the answer to the first question
In each case the variables required by the message box are the same:
- the text to be displayed on the msgbox
- an integer value for the number and type of buttons to be displayed
buttons
0 - show only the OK button
1 - show both OK and Cancel
2 - Abort, Retry, and Ignore
3 - Yes, No, and Cancel
4 - Yes and No
5 - Retry and Cancel
icons
16 - show the Stop icon
32 - Question icon
48 - Exclamation Point
64 - Information icon
default buttons
128 - set the first button as the default button
256 - set the second button as the default
512 - set the third button as the default - the text to be displayed in the title of the msgbox
OpenOffice's Input Box
The input box is as easy to use as the message box - however, this time the user response is always saved into a variable, for example:
username = InputBox ("Please enter your name","Application Information")
The input box just needs two inputs:
- the text to be displayed (for example a question)
- a title for the input box
Using Message Boxes and Input Boxes with OpenOffice Calc
With just these two OpenOffice Basic methods it is possible to greatly simplify the process of entering data into an OpenOffice document:
Sub Main
Dim ip as Integer
Dim oCell
'Get user name
oCell = thisComponent.Sheets(0).getCellRangeByName("B1")
oCell.String = "Name:"
oCell = thisComponent.Sheets(0).getCellRangeByName("B1")
oCell.String = InputBox ("Please enter your name","Application Information")
'Is user male or female?
oCell = thisComponent.Sheets(0).getCellRangeByName("A7")
oCell.String = "M/F"
oCell = thisComponent.Sheets(0).getCellRangeByName("B7")
mf = msgbox ( "Are you male?", 4, "Application Question")
select case mf
case 6
oCell.String = "Male"
case 7
oCell.String = "Female"
end select
'Get user age
oCell = thisComponent.Sheets(0).getCellRangeByName("C7")
oCell.String = "Age:"
oCell = thisComponent.Sheets(0).getCellRangeByName("D7")
oCell.Value = InputBox ("Please enter your age","Application Information")
End Sub
By introducing the message boxes and input boxes a user does not need to know anything about the structure of the document, or what information is needed - all they have to do is to answer the questions posed to them by the macro.
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @dorodor I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x