Retrieving Ms-access form properties in python
Question: I want to retrieve ms-access form dimensions through python, getting the
"width" is no problem, but "height" is not a direct property but consists of
partial heights of sections. In VBA these can be taken by
Form!fName.section(n).height. In python this fails. Does anyone know the
direct access to these properties. The code used is

    
    
    formNames = []
    strDbName = 'D:\\Python\\workspace\\Test_MDB\\kaders.mdb'
    oApp = Dispatch("Access.Application")
    oApp.Visible = True
    win32api.keybd_event(0x10, 0x10, 0, 0)
    oApp.OpenCurrentDatabase(strDbName)
    win32api.keybd_event(0x10, 0x10, 2, 0)
    print("Program start .....")
    for form in oApp.CurrentProject.AllForms:
        formName = form.name                # get form name and open 
        oApp.DoCmd.OpenForm(formName, 1)    # acDesign = 1
        frm = oApp.Forms(formName)          # point to form
    
        print (frm.width)
        print (frm.section(0).height)
    

last line crashes with

print (frm.section(0).height)

File ">", line 2, in section

pywintypes.com_error: (-2147352573, 'can't find member.', None, None)

any ideas?  
thanks,  
Walter


Answer: To get the section for form's height, simply reference the named section of
form without need of a number index: Detail, FormHeader, PageHeader.

Also, I adjusted your code somewhat to convert [twips to
inches](https://support.microsoft.com/en-us/kb/76388) in form's dimensions in
print statements and wrapped loop in a `try/except` (the counterpart to VBA's
`On Error` handle) as you want to always uninitialize com objects from memory
after runtime of script regardless of errors.

    
    
    import win32api
    import win32com.client   
    
    strDbName = 'D:\\Python\\workspace\\Test_MDB\\kaders.mdb'
    
    oApp = win32com.client.Dispatch("Access.Application")
    oApp.Visible = True
    win32api.keybd_event(0x10, 0x10, 0, 0)
    oApp.OpenCurrentDatabase(strDbName)
    win32api.keybd_event(0x10, 0x10, 2, 0)
    print("Program start .....")
    
    try:
        for form in oApp.CurrentProject.AllForms:
            formName = form.name                # get form name and open 
            oApp.DoCmd.OpenForm(formName, 1)    # acDesign = 1
            frm = oApp.Forms(formName)          # point to form
    
            print (form.name)                   
            print ((frm.width)/1440)            # convert twips to inchdes (1440 twips = 1 inch)
            print ((frm.Detail.height/1440))    # convert twips to inchdes (1440 twips = 1 inch)        
            print ("-----------------------")
            oApp.DoCmd.Close(2, formName)       # closing form to remove instance (in case of subforms)
    
    except Exception as e :
            print (e)
    
    oApp.DoCmd.CloseDatabase                    # closing database to release from memory
    oApp = None                                 # uninitializing com object
    

