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. Here's my code, written through the MS Visual Basic Editor, Version 7.1, running on Mac OS Mojave 10.14.5. Word & Excel are both version 16.26. Each Sub below is a different attempt. I'm new to VBA, so trying lots of things that I don't quite understand. Each Sub gives a different error:
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
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.
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
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.
The referenced test.xlsx exists at the specified location, with 777 permissions.
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
What am I missing? Thank you.