Page 1 of 1
[Solved] How to Create a Message Box in a Macro?
Posted: Fri Jul 10, 2009 7:26 am
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)
Re: How to Create a Message Box in a Macro?
Posted: Fri Jul 10, 2009 1:55 pm
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
Re: How to Create a Message Box in a Macro?
Posted: Sat Jul 11, 2009 7:11 am
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
Re: How to Create a Message Box in a Macro?
Posted: Sat Jul 11, 2009 2:21 pm
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
Re: How to Create a Message Box in a Macro?
Posted: Sat Jul 11, 2009 4:05 pm
by ollie
This is exactly what I want to do.
Thank you very much for the help.
Re: [Solved] How to Create a Message Box in a Macro?
Posted: Wed Jan 28, 2015 12:23 am
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!
Re: [Solved] How to Create a Message Box in a Macro?
Posted: Wed Jan 28, 2015 1:06 am
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.