在python中检索Ms-access表单属性

时间:2015-09-11 16:02:37

标签: python ms-access

我想通过python检索ms-access表单维度,得到" width"没问题,但"身高"不是直接属性,而是由部分高度组成。在VBA中,这些可以由Form!fName.section(n).height获取。在python中失败了。有谁知道对这些属性的直接访问。使用的代码是

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)

最后一行与

崩溃

print(frm.section(0).height)

文件">",第2行,在

部分

pywintypes.com_error :( -2147352573,'找不到会员。',无,无)

任何想法?
谢谢,
沃尔特

1 个答案:

答案 0 :(得分:0)

要获取表单高度的部分,只需引用表单的命名部分,而不需要数字索引:Detail,FormHeader,PageHeader。

另外,我稍微调整了你的代码,以便在print语句中转换表单维度中的twips to inches,并在try/except(与VBA的On Error句柄相对应)中包装循环,因为你想要无论错误如何,在脚本运行后都从内存中取消初始化com对象。

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