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

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

问题描述

早上好.

我在让我的代码查找其他单词实例时遇到问题,并且在多次谷歌搜索后遇到了问题.

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.

希望对你有帮助.

关于子类化和 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 和多个单词实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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