vba 如何在 Excel 中打开特定版本的 Word 2007/2010

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12073152/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:26:01  来源:igfitidea点击:

How to open specific version of Word 2007/2010 in Excel

excelvbaexcel-vbams-word

提问by user3357963

I have both Word 2007 and 2010 installed. I need to open Word from within Excel but I need to specify which version I need to open within VBA.

我同时安装了 Word 2007 和 2010。我需要从 Excel 中打开 Word,但我需要指定我需要在 VBA 中打开哪个版本。

I've tried late binding

我试过后期绑定

Dim wordApp2007 As Object
Dim wordApp2010 As Object

Set wordApp2007 = CreateObject("Word.Application.12")
wordApp2007.Visible = True
Set wordApp2010 = CreateObject("Word.Application.14")
wordApp2010.Visible = True

but both open Word 2010

但都打开 Word 2010

I've also tried early binding by using

我也尝试过使用早期绑定

Dim wordApp As Word.Application
Set wordApp2007 = New Word.Application
wordApp2007.Visible = True

and setting references to the Word 12.0 object model but this still opens Word 2010 enter image description here

并设置对 Word 12.0 对象模型的引用,但这仍会打开 Word 2010 在此处输入图片说明

If I register each version of Word using

如果我注册每个版本的 Word 使用

"C:\Program Files\Microsoft Office\Office12\WINWORD.EXE" /regserver

"C:\Program Files\Microsoft Office\Office12\WINWORD.EXE" /regserver

"C:\Program Files\Microsoft Office\Office14\WINWORD.EXE" /regserver

"C:\Program Files\Microsoft Office\Office14\WINWORD.EXE" /regserver

then the version registered opens but then I can't open open the non-registered.

然后注册的版本打开但是我无法打开未注册的版本。

Can anyone help and show me how to open a specific version of Word within Excel using VBA?

任何人都可以帮助并告诉我如何使用 VBA 在 Excel 中打开特定版本的 Word?

Thank you

谢谢

Edit: Example code....

编辑:示例代码....

Option Explicit

Dim wordApp2007 As Word.Application

Sub Word_InfoEarly()
'early binding
Set wordApp2007 = New Word.Application
wordApp2007.Visible = True

    'other Stuff
    Stop

    wordApp2007.Quit
    Set wordApp2007 = Nothing

End Sub


Sub Word_InfoLate()
Dim wordApp2007 As Object
Dim wordApp2010 As Object

    Set wordApp2007 = CreateObject("Word.Application.12")
    wordApp2007.Visible = True
    Set wordApp2010 = CreateObject("Word.Application.14")
    wordApp2010.Visible = True

    'other Stuff
    Stop

    wordApp2007.Quit
    Set wordApp2007 = Nothing
    wordApp2010.Quit
    Set wordApp2010 = Nothing

End Sub

采纳答案by SeanC

This is a work around:

这是一个解决方法:

TaskID = Shell("C:\Program Files\Microsoft Office\Office12\WINWORD.EXE",vbHide) '2007
'TaskID = Shell("C:\Program Files\Microsoft Office\Office14\WINWORD.EXE",vbHide) '2010
GetObject(,"Word.Application")

You would also need to test if a previous version of word is open, or use something other than a basic GetObjectto activate the window, else there's no guarantees that it will get the right version.

您还需要测试是否打开了以前版本的 word,或者使用除基本之外的其他东西GetObject来激活窗口,否则无法保证它会获得正确的版本。

The other way would be to pass the document name in the Shellcommand, and then GetObjectcould be called with the document name

另一种方法是在Shell命令中传递文档名称,然后GetObject可以使用文档名称调用

回答by JeffK

This may further explain why the code works some times and not others.

这可能会进一步解释为什么代码有时会起作用,而有时却不起作用。

My observation on the situation of the command

我对指挥部情况的观察

'Set wordAppxxxx = CreateObject("Word.Application.xx")'

working or not on your computer is that it is a function of the latest update you get from Microsoft.

在您的计算机上工作与否取决于它是您从 Microsoft 获得的最新更新的功能。

Why I believe this:

为什么我相信这一点:

I have an application that converts a text file to a Word document for backwards compatibility with some legacy apps. The best plan includes using a version of Word similar to the version the legacy apps were designed with/to. As a result, I searched on how to invoke a legacy version of Word as opposed to the default offering on my computer which is Word 2010.

我有一个将文本文件转换为 Word 文档的应用程序,以便向后兼容某些旧版应用程序。最好的计划包括使用与旧版应用程序设计时使用的版本相似的 Word 版本。因此,我搜索了如何调用旧版 Word,而不是我计算机上的默认产品 Word 2010。

The solution noted in this discussion chain provided the answer to my question. (Thank you Stack Overflow contributors!) I wanted to use Word XP, so I looked at my directories and observed that Word XP (aka Word 2002) is a member of Office 10, so I created the command

此讨论链中提到的解决方案为我的问题提供了答案。(感谢 Stack Overflow 的贡献者!)我想使用 Word XP,所以我查看了我的目录,发现 Word XP(又名 Word 2002)是 Office 10 的成员,所以我创建了命令

'Set wordApp2002 = CreateObject("Word.Application.10")'

and my program launched Word 2002 and the world was a happy place.

我的程序启动了 Word 2002,世界是一个快乐的地方。

Over the weekened, I had an update to my computer. I control the updates via an app which gives me control over when updates occur such that I can observe changes to my configuration. This morning (9/30/13) I turned on a computer that had a Word update. I did not know this until after I had made one run of my app from last week. The app ran fine and invoked Word 2002 as expected.

整个周末,我对我的电脑进行了更新。我通过一个应用程序控制更新,该应用程序让我可以控制何时发生更新,以便我可以观察到我的配置的变化。今天早上 (9/30/13) 我打开了一台有 Word 更新的计算机。直到我从上周开始运行我的应用程序之后,我才知道这一点。该应用程序运行良好,并按预期调用了 Word 2002。

But then I got the banner page informing me of a Word 2010 update that was installing itself.

但后来我收到了横幅页面,通知我正在安装自己的 Word 2010 更新。

Afterwards, I ran the app that worked so well for me last week and once today. Now, after the Word update (immediately after!), the same code now launches Word 2010 despite the fact that the command line invoking Word 2002 has not changed.

之后,我在上周和今天运行了一次对我来说效果很好的应用程序。现在,在 Word 更新之后(紧接着!),尽管调用 Word 2002 的命令行没有改变,但现在相同的代码会启动 Word 2010。

This appears strong evidence that a Microsoft update tweaked the settings that previously allowed the VB code to work as expected. This might be a good item to bring to Microsoft's attention so see if we can get this item stabilized in subsequent update packages to allow consistent behavior in future releases.

这似乎是微软更新调整了以前允许 VB 代码按预期工作的设置的有力证据。这可能是引起 Microsoft 关注的好项目,因此看看我们是否可以在后续更新包中稳定此项目,以便在未来版本中保持一致的行为。

I hope this is helpful,

我希望这是有帮助的,

JeffK

杰夫·K

回答by Andy Brown

I wasted half a day on this, and want to help prevent others doing the same! I'm running Windows 7 and Office 2013 and 2010 on the same laptop. I wanted to get Access VBA to open up an old version of Word, as Word 2013 call-outs are printing with thick black borders.

我在这上面浪费了半天时间,并希望帮助防止其他人这样做!我在同一台笔记本电脑上运行 Windows 7 和 Office 2013 和 2010。我想让 Access VBA 打开旧版本的 Word,因为 Word 2013 标注打印时带有粗黑边框。

Having tried lots of variations, here's my code which worked:

尝试了很多变体后,这是我的代码:

Sub GetWordReference()

'finally got Access to open old version of Word

'open Word 2010
Shell "C:\Program Files (x86)\Office 2010\Office14\winword.exe"

'open Word 2013
'Shell "C:\Program Files\Microsoft Office 15\root\office15\winword.exe"

TryAgain:

    On Error GoTo NoWord
    Set word2010 = GetObject(, "Word.Application")
    On Error GoTo 0

    word2010.Visible = True

    'word2010.Documents.Add
    'word2010.Selection.TypeText "This is Word " & word2010.Version

    Exit Sub

NoWord:
    Resume TryAgain

End Sub

I can't get the SO code editor to show this correctly, but copying and pasting should work.

我无法让 SO 代码编辑器正确显示这一点,但复制和粘贴应该可以工作。

回答by Ironbeard

I had a similar issue, and thought I would detail my experience for those that stumble across this in the future.

我有一个类似的问题,我想我会为将来偶然发现这个问题的人详细说明我的经验。

In my situation, I had a Powerpoint macro that was supposed to open a file dialog for the user to select some Excel files and then create tables from the data; I had no problem with it until I recently installed Excel 2003. Now Powerpoint was opening up an Excel 2003 file dialog, which would raise errors when trying to select a *.xlsx file. It didn't matter if I used Excel.Application.10or Excel.Application.14in my code to create the Excel object, it was always an Excel 2003 file dialog.

在我的情况下,我有一个 Powerpoint 宏,它应该打开一个文件对话框,供用户选择一些 Excel 文件,然后根据数据创建表格;在我最近安装 Excel 2003 之前我一直没有问题。现在 Powerpoint 正在打开一个 Excel 2003 文件对话框,当尝试选择 *.xlsx 文件时会引发错误。我是否使用Excel.Application.10Excel.Application.14在我的代码中创建 Excel 对象并不重要,它始终是一个 Excel 2003 文件对话框。

I noticed in Explorer that *.xlsx files were set to be opened in Excel 2010 and *.xls files were set to be opened in Excel 2003. I tried to usual way to reset *.xls files to be opened in 2010 to no avail. I ended up having to delete the registry key and repair Office 2010. Now that all Excel files open in 2010, my problem has been fixed.

我在资源管理器中注意到 *.xlsx 文件被设置为在 Excel 2010 中打开,*.xls 文件被设置为在 Excel 2003 中打开。我尝试用通常的方法将 *.xls 文件重置为在 2010 中打开但无济于事. 我最终不得不删除注册表项并修复 Office 2010。现在所有 Excel 文件都在 2010 年打开,我的问题已得到解决。

I know my problem was a bit different than yours, but I think my experience could help lead to a solution. I think any solution will end up relying on some registry editing.

我知道我的问题与你的有点不同,但我认为我的经验可以帮助找到解决方案。我认为任何解决方案最终都会依赖于一些注册表编辑。

回答by EastOfJupiter

This is a VB.NET solution:

这是一个 VB.NET 解决方案:

Sub Word_InfoLate()
Dim wordApp2007 As Object
Dim wordApp2010 As Object

This is a bit intimidating to some, but there may be a registry edit that can solve this. I am unable to test as I only have one version of MS Office available to me, however, previous versions still have registry keys left over.

这对某些人来说有点吓人,但可能有一个注册表编辑可以解决这个问题。我无法测试,因为我只有一个版本的 MS Office 可供我使用,但是,以前的版本仍然有注册表项。

I found the 2007 version of Word in the registry, and it's default location is C:\program Files\Microsoft Office\Office14\WINWORD.EXE" indicating that older versions of Word are registered to the newest version install location as it's new default.

我在注册表中找到了 2007 版的 Word,它的默认位置是 C:\program Files\Microsoft Office\Office14\WINWORD.EXE”,表明旧版本的 Word 已注册到最新版本的安装位置,因为它是新的默认安装位置。

What you might be able to do is navigate to the registry location

您可以做的是导航到注册表位置

HKEY_CLASSES_ROOT\Word.Documet.12\shell\Open\Command 

Change the (Default) key to read "C:\program Files\Microsoft Office\Office12\WINWORD.EXE" /n "%1"

将(默认)键更改为“C:\program Files\Microsoft Office\Office12\WINWORD.EXE”/n“%1”

In theory whenever

理论上无论何时

Set wordApp2007 = CreateObject("Word.Application.12")

is invoked it may probe the registry for the location of the executable, and find the correct path.

被调用时,它可能会探查注册表以查找可执行文件的位置,并找到正确的路径。