在Excel中获取打开的Word文档的名称 [英] Get the name of open Word document in Excel

查看:35
本文介绍了在Excel中获取打开的Word文档的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开了一本.xlsm工作簿和一本.docx文档.
代码是Excel VBA.

我想在MsgBox中显示Word文件的名称.
如何在Excel VBA中引用Word文档?

我在这里找到了代码

解决方案

GetObject CreateObject 都接受表示ProgID的 String 参数-这是Windows注册表中存在的文字字符串值,将类与特定的类型库相关联.

因此,您希望将该参数括在双引号中,以双引号分隔字符串文字:

 设置WordApp = GetObject(,"Word.Application") 

出现错误的原因是因为其中有一个隐式默认成员调用返回 Word.Application.Name 的值的 Word.Application ,即字符串值"Microsoft Word" ,而"Microsoft Word"是不是有效的注册ProgID.

在项目中具有对Word对象模型的引用意味着您可以执行以下操作:

 将WordApp视作Word.Application设置WordApp = GetObject(,"Word.Application") 

然后针对此 WordApp 对象变量进行的每个成员调用都将在编译时验证(这是 early-bound 的意思).这也意味着您可以执行以下操作:

 将WordApp视作Word.Application设置WordApp =新Word.Application 

代替此:

 将WordApp视作Word.Application设置WordApp = CreateObject("Word.Application") 

通过引用Word对象库,所有 wdXxxxx 常量均已定义并可用.与此相对应的是,您的用户必须在他们的计算机上具有与引用库完全相同的版本.

如果您的用户使用的Word/Office版本与您使用的版本不同,请考虑使用 late 绑定.

您将失去直接使用 wdXxxxx 常量的能力(您将需要使用其基础值,或者将它们定义为 Const 标识符或 Enum成员自己).您还将失去编译时验证,智能感知/自动补全功能,从Word库对任何类进行 New 升级的功能,并且 Option Explicit 将无法保护您从您进行的任何后期绑定成员调用中的错字中提取出来(如果您做错了任何事情,都可能会遇到运行时错误438),但是您还将摆脱对特定版本的Word/Office的依赖.

要使用后期绑定,您需要将对象声明为 AsObject ,如下所示:

 将WordApp视作对象设置WordApp = GetObject(,"Word.Application") 

请注意,即使使用双引号,这也不起作用:

 '设置WordDoc = GetObject(,"Word.Document") 

因为您无法创建 Word.Document -只有Word可以做到这一点,出于同样的原因,您不能创建 Excel.Worksheet ,只能创建Excel可以做到的.要创建 Word.Document ,您需要调用 WordApp 的成员,该成员将创建一个新文档(并返回对它的引用,该引用可以捕获到局部变量中)):

 将WordDoc视作对象的早期绑定:作为Word.Document'设置WordDoc = WordApp.ActiveDocument设置WordDoc = WordApp.Documents.AddMsgBox WordDoc.Name 

I have one .xlsm workbook and one .docx document open.
The code is Excel VBA.

I want to display the name of the Word file in MsgBox.
How do I reference the Word document in Excel VBA?

I found code here https://excel-macro.tutorialhorizon.com/vba-excel-get-the-instance-of-already-opened-word-document/

After I've rewritten it, it is giving me this error:

activex component can't create object 429.

Sub Get_Opened_Doc_Instance()
    'Variables declaration
    'Dim WordApp As Object
    Dim WordApp As Word.Application
    'Dim WordDoc As Object
    Dim WordDoc As Word.Document
    'Dim Text As String             
    'this variable is here because later I'm going to search this file.
    Set WordApp = GetObject(, Word.Application)
    'Set WordDoc = GetObject(, Word.Document)
    WordApp.Visible = True          'this line or the next one returns the error
                                    'activex component can't create object 429
    MsgBox WordApp.ActiveDocument.Name
End Sub

My Excel VBA editor tools->references screenshot.

解决方案

GetObject and CreateObject both accept a String parameter representing a ProgID - that's a literal string value present in the Windows Registry, associating a class with a specific type library.

Hence, you want that argument to be enclosed in double quotes, which delimit a string literal:

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

The reason you're getting an error is because there's an implicit default member call in Word.Application that's returning the value of Word.Application.Name, i.e. the string value "Microsoft Word", and "Microsoft Word" is not a valid, registered ProgID.

Having the reference to the Word object model in your project means you can do this:

Dim WordApp As Word.Application
Set WordApp = GetObject(, "Word.Application")

And then every member call made against this WordApp object variable will be compile-time validated (that's what early-bound means). It also means you can do this:

Dim WordApp As Word.Application
Set WordApp = New Word.Application

Instead of this:

Dim WordApp As Word.Application
Set WordApp = CreateObject("Word.Application")

With the reference to the Word object library, all wdXxxxx constants are defined and usable. The counterpart of that, is that your users must have the exact same version of the referenced library on their machines.

If your users are on different versions of Word / Office than you, consider late binding instead.

You'll lose the ability to use wdXxxxx constants directly (you'll need to use their underlying values, or define them as Const identifiers or Enum members yourself). You'll also lose compile-time validation, intellisense/autocompletion, the ability to New up any class from the Word library, and Option Explicit will not be able to protect you from a typo in any of the late-bound member calls you make (expect run-time error 438 if you do something wrong), but you'll also shed the dependency on a specific version of Word/Office.

To use late binding, you need to declare things As Object, like this:

Dim WordApp As Object
Set WordApp = GetObject(, "Word.Application")

Note that this cannot work, even with the double quotes:

'Set WordDoc = GetObject(, "Word.Document")

Because you can't create a Word.Document - only Word can do that, for the same reason you can't create an Excel.Worksheet, only Excel can do that. To create a Word.Document, you need to invoke a member of WordApp that will create a new document (and return a reference to it that you can capture in a local variable):

Dim WordDoc As Object 'early-bound: As Word.Document
'Set WordDoc = WordApp.ActiveDocument
Set WordDoc = WordApp.Documents.Add

MsgBox WordDoc.Name

这篇关于在Excel中获取打开的Word文档的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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