The following code gets the required objects from the calling event and the name of the control to be focussed from a hidden control named “setFocus”.
Copy the code to somewhere (“MyMacros” or document does not matter).
Call the form navigator and add a hidden control named “setFocus” to the (sub-)form in question where you add the name of the control to be focussed as value.
Get the properties of the form having the hidden control and assign Sub form_Loading
to the form’s loading and reloading events.
REM ***** BASIC *****
Sub form_Loading(ev)
frm = ev.Source
if not frm.hasByName("setFocus") then exit sub
sVal = frm.getByName("setFocus").HiddenValue
REM mri oFormController
focusControl(frm, sVal)
End Sub
Sub focusControl(oForm, sName)
doc = getParentObject(oForm, "com.sun.star.document.OfficeDocument")
view = doc.getCurrentController()
oFormController = view.getFormController(oForm)
oControls = oFormController.getControls()
for i = 0 to uBound(oControls)
box = oControls(i)
if box.Model.getName() = sName then
box.setFocus()
next
next
End Sub
Function getParentObject(byval m, srvName$)
on error goto NullObj
do until m.supportsService(srvName)
m = m.getParent()
loop
getParentObject = m
exit function
NullObj:
getParentObject = Null
End Function
Edit:
biblio_form.odt (51.9 KB) sample document puttin the focus on the “Title” control.
P.S. why do I do that like this?
- The macro should focus a control on any form or subform embedded in any kind of office document being embedded or stand-alone.
- Under certain conditions, ThisComponent may be a database document or any type of embedding office document being embedded in the database or not. Function getParentObject makes it possible to get the embedding form document from the calling form wherever that is in the existing hierarchy of forms.
- HiddenControls are made for macro configuration, so you can use the exact same code for the same task. In my sample document I set
setFocus=Title
. The macro finds the control “setFocus” (exiting silently otherwise) and reads the name from that control. The exact same code can focus any control on any of your forms without changing any constant names in the code.
- This code translates easily into other macro languages.
If you don’t like it that way:
REM Writer document only:
oForm = ThisComponent.DrawPage.Forms.getByName("Form Name")
REM call my routine with hard coded name:
focusControl(oForm, "Control Name")
Same thing in Python, translated in a Python editor from pasted Basic code:
def form_Loading(ev):
frm = ev.Source
if not frm.hasByName("setFocus"):return
sVal = frm.getByName("setFocus").HiddenValue
focusControl(frm, sVal)
def focusControl(oForm, sName):
doc = getParentObject(oForm, "com.sun.star.document.OfficeDocument")
view = doc.getCurrentController()
oFormController = view.getFormController(oForm)
oControls = oFormController.getControls()
for i in range(len(oControls)):
box = oControls[i]
if box.Model.getName() == sName:
box.setFocus()
return
def getParentObject(m, srvName):
while not m.supportsService(srvName):
try:
m = m.getParent()
except:
return None
return m