在Microsoft Word实例上的python中使用AccessibleObjectFromWindow [英] Using AccessibleObjectFromWindow in python on Microsoft Word instance

查看:464
本文介绍了在Microsoft Word实例上的python中使用AccessibleObjectFromWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python处理一个特定的已打开,未保存(因此没有路径)的Word文档(* .doc).如果仅打开一个Word实例,则该操作效果很好,但是多个实例会带来一些困难,请参阅SO问题

I am trying to manipulate a particular already-open, unsaved (so no path) Word document (*.doc) with python. The manipulations work great if only one Word instance is open, however multiple instances create some difficulty, reference the SO question Word VBA and Multiple Word Instances.

我在C#中找到了一些有关处理此问题的参考(

I have found some references on dealing with this in C# (How to access Microsoft Word existing instance using late binding) and VB.NET (Multiple Instances of Applications) and have managed to translate them to python to a point. Here's where I am now:

import win32com.client as win32
import win32gui
#GUID class from http://svn.python.org/projects/ctypes/tags/release_0_2/ctypes/comtypes/GUID.py
from GUID import GUID
from ctypes import oledll
from ctypes import byref
from comtypes import POINTER
from comtypes.automation import IDispatch

#Use win32 functions to get the handle of the accessible window
#of the desired Word instance.  Specific code not relevant here, 
#but I'll end up with an integer such as:
hwnd = 2492046

OBJID_NATIVEOM = 0xffffff0
IID_IDispatch = byref(GUID("{00020400-0000-0000-C000-000000000046}"))
p = POINTER(IDispatch)()
oledll.oleacc.AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, IID_IDispatch, p)

运行时,这将返回错误消息:

When run, this returns the error message:

  File "wordtest.py", line 55, in <module>
    oledll.oleacc.AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, IID_IDispatch, p)
  File "_ctypes/callproc.c", line 945, in GetResult
WindowsError: [Error -2147024809] The parameter is incorrect

在解决此错误方面的任何帮助将不胜感激!

Any assistance in fixing this error would be much appreciated!

推荐答案

一些在NVDA(非可视桌面访问)的GitHub代码中进行搜索时发现,我为OBJID_NATIVEOM使用的值不正确,我需要包装指针在byref()中,有一种更简单的方法来获取IDispatch的GUID:

Some searching through the GitHub code of NVDA (Non-Visual Desktop Access) showed that the value I was using for OBJID_NATIVEOM was incorrect, I needed to wrap the pointer in a byref(), and there was an easier way to get the GUID of IDispatch:

#Part of the pywin32 package that must be installed with the pywin32
#installer:
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] from code at 
#http://stackoverflow.com/questions/33901597/getting-last-opened-ms-word-document-object

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)

这篇关于在Microsoft Word实例上的python中使用AccessibleObjectFromWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆