简体   繁体   English

我应该如何从 MS Word VBA 打开 Excel 工作簿

[英]How should I open an Excel Workbook from MS Word VBA

There are other variants of this question on StackOverflow, but they're all from several years ago and the answers from them seem to reference older versions of VBA... or at least they give me errors I've been unable to resolve otherwise. StackOverflow 上还有这个问题的其他变体,但它们都来自几年前,并且它们的答案似乎引用了旧版本的 VBA ......或者至少它们给了我错误,否则我无法解决。 Here's my code, written through the MS Visual Basic Editor, Version 7.1, running on Mac OS Mojave 10.14.5.这是我的代码,通过 MS Visual Basic 编辑器编写,版本 7.1,在 Mac OS Mojave 10.14.5 上运行。 Word & Excel are both version 16.26. Word 和 Excel 都是 16.26 版。 Each Sub below is a different attempt.下面的每个 Sub 都是不同的尝试。 I'm new to VBA, so trying lots of things that I don't quite understand.我是 VBA 新手,所以尝试了很多我不太了解的东西。 Each Sub gives a different error:每个 Sub 给出不同的错误:

Sub OpenExcelFile1()
    Dim oExcel As Excel.Application
    Dim oWB As Excel.Workbook
    Dim granted as Boolean
    Debug.Print "a"

    Set oExcel = New Excel.Application
    granted = GrantAccessToMultipleFiles(Array("/users/sarah/workspace/report-writer/"))
    Debug.Print "b"
    Set oWB = oExcel.Workbooks.Open("/Users/sarah/workspace/report-writer/test.xlsx", ReadOnly:=True)
    Debug.Print "c"
End Sub

OpenExcelFile1 above hangs for about a minute between "a" and "b", then gives Run-time error '-2146959355 (80080005)': Automation error上面的 OpenExcelFile1 在“a”和“b”之间挂起大约一分钟,然后给出Run-time error '-2146959355 (80080005)': Automation error

Sub OpenExcelFile2()
    Dim oWB As Excel.Workbook
    Dim granted As Boolean
    granted = GrantAccessToMultipleFiles(Array("/Users/sarah/workspace/report-writer/"))
    Debug.Print "d"

    Set oWB = Excel.Workbooks.Open("/Users/sarah/workspace/report-writer/test.xlsx", ReadOnly:=True)
    Debug.Print "e"
End Sub

OpenExcelFile2 above gets to "d", then gives Run-time error '430': Class does not support Automation or does not support expected interface.上面的 OpenExcelFile2 变为“d”,然后给出Run-time error '430': Class does not support Automation or does not support expected interface.

Sub OpenExcelFile3()
    Dim oWB As Excel.Workbook
    Dim granted As Boolean
    granted = GrantAccessToMultipleFiles(Array("/Users/sarah/workspace/report-writer/"))
    Debug.Print "f"

    Set oWB = Excel.Application.Workbooks.Open("/Users/sarah/workspace/report-writer/test.xlsx", ReadOnly:=True)
    Debug.Print "g"
End Sub

OpenExcelFile3 gets to "f", then prints the same error as OpenExcelFile2 OpenExcelFile3 变为“f”,然后打印与 OpenExcelFile2 相同的错误

Sub OpenExcelFile4()
    Dim oWB As Excel.Workbook
    Dim granted As Boolean
    granted = GrantAccessToMultipleFiles(Array("/Users/sarah/workspace/report-writer/"))
    Debug.Print "h"
    Set oWB = Excel.Application.WorkbookOpen("/Users/sarah/workspace/report-writer/test.xlsx", ReadOnly:=True)
    Debug.Print "i"
End Sub

OpenExcelFile4 gets to "h", then prints the same error as OpenExcelFile2 and OpenExcelFile3. OpenExcelFile4 变为“h”,然后打印与 OpenExcelFile2 和 OpenExcelFile3 相同的错误。

The referenced test.xlsx exists at the specified location, with 777 permissions.引用的 test.xlsx 存在于指定位置,具有 777 权限。

I have installed (checked? used? referenced?) the following six references: * Visual Basic For Applications * Microsoft Word 16.0 Object Library * Microsoft Forms 2.0 Object Library * Microsoft Office 16.0 Object Library * Microsoft Excel 16.0 Object Library * Microsoft Visual Basic for Applications Extensibility 5.3我已经安装(检查过?使用过?引用过?)以下六个参考资料:* Visual Basic For Applications * Microsoft Word 16.0 Object Library * Microsoft Forms 2.0 Object Library * Microsoft Office 16.0 Object Library * Microsoft Excel 16.0 Object Library * Microsoft Visual Basic for应用程序可扩展性 5.3

What am I missing?我错过了什么? Thank you.谢谢你。

Found a solution which I dislike:找到了一个我不喜欢解决方案:

  1. Use Windows, not Mac.使用 Windows,而不是 Mac。
  2. Remove the GrantAccessToMultipleFiles lines, since this function is only present in the Mac version of VBA删除GrantAccessToMultipleFiles行,因为此功能仅存在于 Mac 版本的 VBA 中
  3. Remove the reference to "Microsoft Forms 2.0 Object Library" (I can't find it on Windows. It's probably innocuous if it's on someone else's machine.)删除对“Microsoft Forms 2.0 Object Library”的引用(我在 Windows 上找不到它。如果它在别人的机器上可能是无害的。)

Am posting this as a "solution" because with these modifications because they let me do the basic open-excel-file operation for the methods OpenExcelFile1, OpenExcelFile2, and OpenExcelFile3.我将此作为“解决方案”发布,因为进行了这些修改,因为它们让我对方法 OpenExcelFile1、OpenExcelFile2 和 OpenExcelFile3 进行基本的打开 Excel 文件操作。 The last one, OpenExcelFile4 did not work.最后一个,OpenExcelFile4 不起作用。 It appears the Application.WorkbookOpen() is not present in Windows either. Windows 中似乎也不存在 Application.WorkbookOpen()。 This was tested and working for Windows 10 Pro v 1903 build 18362.175 with MS VBA version 7.1 and Office version 16.这已经过测试,适用于 Windows 10 Pro v 1903 build 18362.175,MS VBA 7.1 版和 Office 16 版。

I don't like the solution because the specific project I'm researching this for is Mac-primary.我不喜欢这个解决方案,因为我正在研究的特定项目是 Mac-primary。 If others have a Mac-friendly solution, I'd love to see it.如果其他人有适合 Mac 的解决方案,我很乐意看到。

You're probably missing a reference to the DLL that supports the automation of Excel from Word.您可能缺少对支持 Word 中 Excel 自动化的 DLL 的引用。 Try adding the reference by entering the VBA editor, then clicking Tools > References then scrolling down the list to Microsoft Excel 16.0 Object Library .尝试通过输入 VBA 编辑器添加引用,然后单击Tools > References然后将列表向下滚动到Microsoft Excel 16.0 Object Library Your version might vary;您的版本可能有所不同; I'm (apparently) on Office v16.我(显然)使用的是 Office v16。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM