Getting last opened MS Word document object

后端 未结 1 1638
北荒
北荒 2021-01-16 16:25

I have a python script called from a VBA AutoNew() sub in a MS Word 2003 template (.dot) - so it runs every time a document is created from this Word template.

A thi

相关标签:
1条回答
  • 2021-01-16 16:54

    Some searching through the GitHub code of NVDA (Non-Visual Desktop Access) finally got me the object I was looking for:

    #Part of the pywin32 package that must be installed with the pywin32
    #installer:
    import win32com.client as win32
    import win32gui
    
    from ctypes import oledll
    from ctypes import byref
    
    #installed by easy_install comtypes
    from comtypes import POINTER
    from comtypes.automation import IDispatch
    import comtypes.client.dynamic as comDy
    
    #Handle integer hwnds[0] per my edit in the question
    
    OBJID_NATIVEOM = -16
    p = POINTER(IDispatch)()
    oledll.oleacc.AccessibleObjectFromWindow(hwnds[0], OBJID_NATIVEOM,
        byref(IDispatch._iid_), byref(p))
    
    window = comDy.Dispatch(p)
    word = window.application
    cert = word.Documents(1)
    
    0 讨论(0)
提交回复
热议问题