[Solved] How to Create a Message Box in a Macro?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ollie
Posts: 33
Joined: Sat Mar 28, 2009 4:04 am

[Solved] How to Create a Message Box in a Macro?

Post by ollie »

Is there a way of creating a message box that will give the options "Yes" "No" and "Cancel" when a macro is run from a push button.

Title Edited. A descriptive title for posts helps others who are searching for solutions and increases the chances of a reply. (TheGurkha, Moderator)
Last edited by TheGurkha on Sat Jul 11, 2009 4:07 pm, edited 2 times in total.
Reason: Tagged Solved.
OOo 3.0.X on Ms Windows XP
FJCC
Moderator
Posts: 9273
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to Create a Message Box in a Macro?

Post by FJCC »

You can certainly create a message box with the options Yes, No and Cancel. I have pasted the Help File for that below. The Msgbox command will return a different value depending on which option is selected, so you can change what the macro does on the basis of that.

MsgBox Function [Runtime]
Displays a dialog box containing a message and returns a value.
Syntax:
MsgBox (Text As String [,Type As Integer [,Dialogtitle As String]])
Return value:
Integer
Parameter:
Text: String expression displayed as a message in the dialog box. Line breaks can be inserted with Chr$(13).
DialogTitle: String expression displayed in the title bar of the dialog. If omitted, the name of the respective application is displayed.
Type: Any integer expression that specifies the dialog type and defines the number and type of buttons or icons displayed. Type represents a combination of bit patterns (dialog elements defined by adding the respective values):
Values
0 : Display OK button only.
1 : Display OK and Cancel buttons.
2 : Display Abort, Retry, and Ignore buttons.
3 : Display Yes, No, and Cancel buttons.
4 : Display Yes and No buttons.
5 : Display Retry and Cancel buttons.
16 : Add the Stop icon to the dialog.
32 : Add the Question icon to the dialog.
48 : Add the Exclamation Point icon to the dialog.
64 : Add the Information icon to the dialog.
128 : First button in the dialog as default button.
256 : Second button in the dialog as default button.
512 : Third button in the dialog as default button.
Return value:
1 : OK
2 : Cancel
3 : Abort
4 : Retry
5 : Ignore
6 : Yes
7 : No
Error Codes
5 Invalid procedure call
Example:
Sub ExampleMsgBox
Dim sVar as Integer
sVar = MsgBox("Las Vegas")
sVar = MsgBox("Las Vegas",1)
sVar = MsgBox( "Las Vegas",256 + 16 + 2,"Dialog title")
end sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
ollie
Posts: 33
Joined: Sat Mar 28, 2009 4:04 am

Re: How to Create a Message Box in a Macro?

Post by ollie »

Hi
I have created the following macro.
sub Test
Dim sVar as Integer
sVar = MsgBox("You are about to Format July. Do you want to continue",4,"Warning")
Integer = 7
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "StringName"
args1(0).Value = ""

dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args1())

rem ----------------------------------------------------------------------
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "ToPoint"
args2(0).Value = "$A$2"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args2())

rem ----------------------------------------------------------------------
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "StringName"
args3(0).Value = "Test"

dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args3())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())


end sub

It brings up the message box that I want but it wont opt out of the macro when I push "No". There must be something else I need to add to it.
Can you please help
Thanks
OOo 3.0.X on Ms Windows XP
FJCC
Moderator
Posts: 9273
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to Create a Message Box in a Macro?

Post by FJCC »

The value of sVar is set to 7 when you select "No", but the program has no instruction for handling that (or any other) value of sVar, so it just goes on with the code. I have added an If-Then statement that checks the value of sVar and ends the sub routine if the value is 7. Remember that the code has no idea what you want to do. The Msgbox statement just makes a dialog box on the screen containing certain characters and then provides a return value. If your question was "Do you want to quit?", then the If-Then statement would test if the value of sVar is 6 (6 = Yes). You have to set the code to do what you want with any particular returned value of sVar.

Code: Select all

sub Test
Dim sVar as Integer
sVar = MsgBox("You are about to Format July. Do you want to continue",4,"Warning")

If sVar = 7 then
  Exit sub
End if

Integer = 7
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "StringName"
args1(0).Value = ""

dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args1())

rem ----------------------------------------------------------------------
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "ToPoint"
args2(0).Value = "$A$2"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args2())

rem ----------------------------------------------------------------------
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "StringName"
args3(0).Value = "Test"

dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args3())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())


End Sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
ollie
Posts: 33
Joined: Sat Mar 28, 2009 4:04 am

Re: How to Create a Message Box in a Macro?

Post by ollie »

This is exactly what I want to do.
Thank you very much for the help.
OOo 3.0.X on Ms Windows XP
papijo
Posts: 90
Joined: Sat Nov 08, 2014 5:46 pm
Location: Brittany, West of France

Re: [Solved] How to Create a Message Box in a Macro?

Post by papijo »

Can someone explain why the difference between the return values of the Message Box described in this discussion:
1 : OK; 2 : Cancel; 3 : Abort; 4 : Retry; 5 : Ignore; 6 : Yes; 7 : No

and the constants found at https://www.openoffice.org/api/docs/com ... sults.html:

CANCEL = 0; OK = 1;YES = 2;NO = 3;RETRY = 4;IGNORE = 5;

Thanks!
LO: LibreOffice 6.4.0.3 (x64) on Windows 10 64bits. Split database HSQL 2.3.4.
User avatar
MTP
Volunteer
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: [Solved] How to Create a Message Box in a Macro?

Post by MTP »

I think that api must be for a different kind of dialog. One of the things that makes me think this (in addition to the constants clearly being wrong for what the function MsgBox returns) is the related interface XMessageBoxFactory that has the message string as the last argument, where MsgBox takes the message string as the first argument.
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
Post Reply