Word VBA和多个Word实例 [英] Word VBA and Multiple Word Instances

查看:534
本文介绍了Word VBA和多个Word实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好.

我在获取代码以查找单词的其他实例时遇到问题,并且在经过大量的Google搜索后遇到了麻烦.

I am having a problem with getting my code to find other instances of word and have hit a brick wall after much google searching.

下面的我的代码"将找到所有打开的Word文档,并将其填充到组合框中.

My Code below will find all open word documents and populate them into a combo box.

我的问题是我们有一些应用程序(我无法控制它们)将在新实例中打开Word文档,因此我的代码将找不到/控制这些文档.

My problem is we have applications (I have no control over these) that will open word documents in a new instance and therefore my code will not find/control these documents.

有什么想法吗?

Dim objWordDocument As Word.Document
Dim objWordApplication As Word.Application


'//find all open word documents
Set objWordApplication = GetObject(, "Word.Application")

'//clear combobox
    OpenDocs.Clear

'//add all open documents to combo box

        For Each objWordDocument In objWordApplication.Documents

            OpenDocs.AddItem objWordDocument.Name

        Next objWordDocument

推荐答案

据我所见并了解,唯一确定的可行方法是遍历word的运行实例,然后杀死每个实例依次确定要获取下一个实例.

From what I have seen, and come to understand, the only sure fire way to do this is to iterate through the running instances of word and then kill each one in turn to be sure that you are getting the next instance.

由于单词在运行的对象表中针对其自身的每个实例以完全相同的方式注册,因此没有先关闭您正在查看的实例,就无法通过它们进行注册.

Since word registers in the running object table the same exact way for every instance of itself, there is no way to get through them without first closing the one you were looking at.

这种方法的一种选择(可能不希望使用)是在终止应用程序实例时获取所有文件名,然后将其全部重新加载到您创建的一个实例中.

One option to this approach, which is probably not desirable, is to get all the file names while you are killing the application instances and then load them all back in one instance that you create.

或者,如果您知道打开文件的名称,则可以通过打开文件名来"getObject",因为Word会将其文档名压入运行对象表中,可悲的是,这听起来不符合您的情况.

Alternately if you know the names of the open files, you can 'getObject' by open file name since Word will push its document names into the running object table, sadly this does not sound like the case for you.

如果不编写活动的x MFC服务,您将无法做您想做的事情.

Without writing an active x MFC service, you are not going to be able to do what you are looking to do.

我希望这会有所帮助.

I hope that is helpful.

对于子类和Windows API进行了广泛的讨论,以获取句柄以改变焦点. http://www.xtremevbtalk.com/showthread.php?t=314637

there was an extensive discussion on subclassing and windows API's to get handles in order to change focus. http://www.xtremevbtalk.com/showthread.php?t=314637

如果您首先进入该标题,并且能够通过hwnd枚举单词实例,那么您可以依次将每个单词集中起来,然后列出文件名.我确实警告过你;那是一个令人讨厌的子类,这是黑暗的魔术,只有某些真正想意外破坏东西的人才能玩.

if you dove into that head first and were able to enumerate the word instances by hwnd then you could potentially focus each one in turn and then list the file names. I do warn you though; that is some nasty subclassing which is dark magic that only some people who really want to accidentally break stuff play with.

无论如何,如果您想查看一个实例,请杀死,重复并重新打开,请尝试以下操作:

In any event if you wanted to take the look at one instance, kill, repeat, reopen try this:

从此线程改编而成: http://www.xtremevbtalk.com/showthread.php ?t = 316776

Set objWordApplication = GetObject(, "Word.Application")

'//clear combobox
    OpenDocs.Clear

'//add all open documents to combo box

   Do While Not objWordDocument is nothing 

        For Each objWordDocument In objWordApplication.Documents

            OpenDocs.AddItem objWordDocument.Name

        Next objWordDocument
        objWordApplication.Quit False
        Set objWordApplication = Nothing
        Set objWordApplication = GetObject(, "Word.Application")
   loop

   ** use create object to open a new instance of word here and then go though
   ** your list of files until you have opened them all as documents in the new
   ** instance.

这篇关于Word VBA和多个Word实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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