This seems to work actually. Thanks to @flywire
Close a form document, no matter where the code is stored, no matter if the form is embedded or not.
getParentObject finds the containing office document. If that document is embedded, closeEmbeddedFormDocument lets the embedding database document closes the form document. An embedded form needs to be closed by the containing database document, otherwise you get a veto exception.
Sub closeFormDoc(ev)
'mri = createUnoService("mytools.Mri")
doc = getParentObject(ev.Source.Model, "com.sun.star.document.OfficeDocument")
'mri.inspect( doc)
if not (doc.Parent Is Nothing) Then
closeEmbeddedFormDocument doc.getParent(), doc.getTitle()
else
doc.close(False)
endif
End Sub
Function getParentObject(byval m, srvName$)
do until m.supportsService(srvName)
m = m.getParent()
loop
getParentObject = m
End Function
Sub closeEmbeddedFormDocument(odb, sTitle)
forms = odb.FormDocuments
for i = 0 to forms.getCount() -1
form = forms.getByIndex(i)
comp = form.getComponent()
if not isnull(comp) then
if comp.Title = sTitle then
form.close()
exit for
endif
endif
next i
End Sub