# How to close running dialog box via macro

I'm attempting to close a dialog named oDlg with the code below. oDlg is defined as a public object variable as I'm attempting to close a dialog that is created by another sub. I have tried killdlg as both private and public which makes no difference. Not sure what I'm doing wrong. I appreciate any input. Obviously, I"m a LO basic noob.

Sub Close_dialog

killdlg= createUnoService("com.sun.star.awt.XDialog")
oDlg.killdlg.endexecute()

End Sub

edit retag close merge delete

Sort by » oldest newest most voted

Try oDlg.dispose()

Sorry that will work but may cause other problems. It depends on how you start the dialog. If oDlg.execute then

oDlg.endExecute()


will work. If you're using setVisible then see this answer for a possible solution non-modal dialog.

Obviously, in order to do this the dialog must be modeless (non-modal) and it's possible to start a modeless dialog with either method.

A note for general knowledge, when using the endExecute method, make sure the object exists first. For example:

Sub CloseDlg
If Not IsNull(oDlg) then
oDlg.endExecute()
End If
End Sub

more

I couldn't get that command to work. It closed the entire application for some reason. More likely to be my code and not your command causing the problem. As it turns out, I ended up going another direction with my macro, so I didn't press the issue. I have no doubt your answer is correct, given our previous encounters. Therefore, I'll mark my question as answered. Thanks, Ratslinger. Docbda

( 2016-04-14 17:20:21 +0100 )edit

Oops! I didn't see that you edited your answer before sending my last comment. I was using "setVisible." I'll check out the link you sent. Thanks for the follow-up. Docbda

( 2016-04-14 17:27:15 +0100 )edit

Not a problem. If you're still having problems, please provide details. I'll be glad to help you work out the details. It looks like a viable solution.

( 2016-04-14 18:06:05 +0100 )edit

I do have a more general question regarding the code that I was initially trying. Due to the fact that the endExecute() is an function under the com.sun.star.awt.XDialog interface, why do I not need to create the Uno service before calling the endExecute() function? Note: I put this question here because it references the code that I included as part of my original question.

( 2016-04-14 18:28:39 +0100 )edit

You don't. Here's my running test:

 Sub CloseDlg
oDlg.endExecute()
End Sub


I simply call this from a push button and it works fine with oDlg being global or Public. All else done when oDlg was created. In other words. endExecute is a method of the dialog object. You can see this with Mri.

( 2016-04-14 18:48:19 +0100 )edit

I see. Thanks for the explanation. Looks like I need to download Mri and learn how to use it. This is similar to Xray, right?

( 2016-04-14 19:29:07 +0100 )edit

They are both similar. I prefer Mri (I have both) but you can do the same with Xray - just inspect oDlg when it is active.

( 2016-04-14 20:01:40 +0100 )edit