Word VBA 和多个单词实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13633551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Word VBA and Multiple Word Instances
提问by Mikey
Good morning.
早上好。
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.
我在让我的代码查找其他单词实例时遇到问题,并且在经过大量谷歌搜索后遇到了问题。
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.
我的问题是我们有应用程序(我无法控制这些)将在新实例中打开 Word 文档,因此我的代码将无法找到/控制这些文档。
Any ideas?
有任何想法吗?
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
采纳答案by Pow-Ian
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.
从我所看到和理解的情况来看,唯一可靠的方法是遍历正在运行的 word 实例,然后依次杀死每个实例以确保获得下一个实例。
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.
这种方法的一种选择(可能并不理想)是在您终止应用程序实例时获取所有文件名,然后将它们全部加载回您创建的一个实例中。
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.
或者,如果您知道打开文件的名称,您可以通过打开文件名“getObject”,因为 Word 会将其文档名称推送到正在运行的对象表中,遗憾的是,这听起来不像您的情况。
Without writing an active x MFC service, you are not going to be able to do what you are looking to do.
如果不编写活动的 x MFC 服务,您将无法做您想做的事情。
I hope that is helpful.
我希望这是有帮助的。
EDIT:
编辑:
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
对子类化和 Windows API 进行了广泛的讨论,以获取句柄以改变焦点。http://www.xtremevbtalk.com/showthread.php?t=314637
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.
如果您首先深入研究该主题并能够通过 hwnd 枚举单词实例,那么您可能会依次关注每个单词,然后列出文件名。不过我确实警告过你;这是一些令人讨厌的子类化,这是只有一些真正想要不小心破坏东西的人才能玩的黑魔法。
In any event if you wanted to take the look at one instance, kill, repeat, reopen try this:
无论如何,如果您想查看一个实例,杀死,重复,重新打开,请尝试以下操作:
Adapted from this thread: http://www.xtremevbtalk.com/showthread.php?t=316776
改编自此线程: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.
回答by Marcelo Scofano
It's an old thread, but I too have the need to iterate over Word instances and bumped here.
这是一个旧线程,但我也需要遍历 Word 实例并在这里碰到。
Following the @Pow-Ian's advise, I tried to do the
按照@Pow-Ian 的建议,我尝试做
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.
如果您首先深入研究该主题并能够通过 hwnd 枚举单词实例,那么您可能会依次关注每个单词,然后列出文件名。
Although I have managed to get all the handles, I have found an easier strategy with regard to office applications through AccessibleObjectFromWindowand our question is now solved.
虽然我已经设法获得了所有的处理,但我通过AccessibleObjectFromWindow找到了一个关于办公应用程序的更简单的策略,我们的问题现在解决了。
Also, I believe the code showed @Pow-lan's has a mistype on
另外,我相信代码显示@Pow-lan 的输入错误
Do While Not objWordDocument is nothing
and should be:
并且应该是:
Do While Not objWordApplication is nothing