Ms word 获取上次打开的MS Word文档对象
我有一个python脚本,它是从MS Word 2003模板(.dot)中的VBA AutoNew()子脚本调用的,因此每次从这个Word模板创建文档时,它都会运行 第三方应用程序从此模板创建文档。第三方应用程序如何设置文档存在许多格式问题,因此,我的脚本将在第三方脚本运行完毕后对其进行调整。(我最初是用VBA编写脚本的,但是VBA计时器的问题导致它大部分时间崩溃。python版本工作完美。) 我希望脚本只处理调用它的文档,它将始终是最近打开的Word文件。(文件是.doc而不是.docx,如果这有什么区别的话。)我找到了三种方法来获取Word的打开实例(由于此脚本由AutoNew调用,因此始终会有一个可用的打开实例): 如果新创建的文档是唯一打开的Word文件,那么这三个文件中的任何一个都非常有用。但是如果一个Word文档已经打开,并且我运行第三方软件从这个模板创建一个新文档,那么python脚本每次都会用这三种方法捕获旧的实例 我尝试过搜索Word文档循环的方法,我的想法是我可以检查所有的名称,并选择最后编号最高的名称(在脚本运行时,文档将不会保存,因此其名称将为Document1、Document2等)。不幸的是,我只找到了循环关闭文档的方法(打开一个,做某事,关闭它,继续下一个),而不是(像我的情况)已经打开的 有没有办法将python引导到最近打开的Word文档 编辑相关问题: 我已找到如何获取要控制的文档的Windows句柄整数:Ms word 获取上次打开的MS Word文档对象,ms-word,pywin32,Ms Word,Pywin32,我有一个python脚本,它是从MS Word 2003模板(.dot)中的VBA AutoNew()子脚本调用的,因此每次从这个Word模板创建文档时,它都会运行 第三方应用程序从此模板创建文档。第三方应用程序如何设置文档存在许多格式问题,因此,我的脚本将在第三方脚本运行完毕后对其进行调整。(我最初是用VBA编写脚本的,但是VBA计时器的问题导致它大部分时间崩溃。python版本工作完美。) 我希望脚本只处理调用它的文档,它将始终是最近打开的Word文件。(文件是.doc而不是.docx,如果
import win32gui
import re
#Create a list of all open Microsoft Word document titles and their
#handle integers
titles = []
def foreach_window(hwnd, lParam):
if win32gui.IsWindowVisible(hwnd):
title = win32gui.GetWindowText(hwnd)
if 'Microsoft Word' in title:
titles.append([title, hwnd])
return True
win32gui.EnumWindows(foreach_window, None)
#Find the handle of the newest open, unsaved Word document
winOrder = []
for s in titles:
item = re.search(r'Document\d', s[0])
if item:
winOrder.append(int(re.search(r'\d+', s[0]).group()))
else:
winOrder.append(0)
hwnd = titles[winOrder.index(max(winOrder))][1]
#Get the edit window from inside the Word instance
def callback(hwnd, hwnds):
if win32gui.GetClassName(hwnd) == '_WwG':
hwnds.append(hwnd)
#I think there should be a 'return False' here to let EnumChildWindows
#know it doesn't have to keep looping once it finds the edit window,
#but it crashes with 'pywintypes.error: (0, 'EnumChildWindows',
#'No error message is available') if I try that
return True
hwnds = []
win32gui.EnumChildWindows(whndl, callback, hwnds)
#Something like this...
#window = win32gui.AccessibleObjectFromWindow(hwnds[0])
那么现在-如何从Windows句柄创建COM对象?通过NVDA(非可视桌面访问)的GitHub代码进行搜索,终于找到了我要找的对象:
#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)
你的回答让我看到了
AccessibleObjectFromWindow
,我可以用VBA解决它,即get。非常感谢你的提问和回答。当然Python的方式要优雅得多。
#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)