excel - 使用 ExceltoWord 设置 FSO = CreateObject 失败!加入
问题描述
我正在使用 Excel 加载项 ExceltoWord 从 Excel 工作表中自动填充 Word 文档。我已按照此处原始开发人员的说明进行操作。
我正在使用“我在 Word 中创建了通用书签指示符”和“我将书签指示符直接放在单元格中 - 左侧”选项,最后带有“删除 Word 文档”。保存设置时出现 MS Visual Basic 错误
运行时错误“429:”ActiveX 组件无法创建对象。
我尝试过切换不同格式的 Excel 表格和 Word Doc 和 Word 模板,以及在保存配置时关闭和打开 Word Doc。
Public Function validateFileFolderSelection(ByVal fName As String, fType As String, src As String, bFolderOnly As Boolean) As Boolean
'Dim FSO As FileSystemObject 'early binding
Dim FSO As Object 'late binding
'Set FSO = New FileSystemObject 'early binding
Set FSO = CreateObject("Scripting.FileSystemObject") 'late binding
validateFileFolderSelection = True
'Test for word or excel filename & that the file exists
If Trim(fName) = vbNullString Then
validateFileFolderSelection = False
ElseIf bFolderOnly Then
If Not FSO.FolderExists(fName) Then
validateFileFolderSelection = False
End If
ElseIf Not FSO.fileExists(fName) Then
validateFileFolderSelection = False
End If
End Function
VBA 在Set FSO = CreateObject("Scripting.FileSystemObject") 'late binding
.
解决方案
如果您添加对Microsoft 脚本运行时的引用(VBE > 工具 > 引用...),则启用您当前已注释掉的“早期绑定”代码,您的代码将起作用。
要在 Visual Basic 编辑器 (VBE) 中设置引用,请转到“工具”菜单并选择“引用...”功能。
然后从打开的 References 对话框中滚动,直到找到 Microsoft Scripting Runtime,并标记它,然后单击 OK。
在您当前的代码中,删除标记为“早期绑定”的两行上的注释标记,并在标记为“后期绑定”的两行上应用注释标记。
以下是对原始答案的编辑,因为根据评论,您在系统上使用 FSO(文件系统对象)代码时仍然存在问题。
下面的 VBA 例程将不使用 FSO,而是确定指定的目录或文件是否存在。该例程称为“DoesItExist”,我已包含一个示例例程,该例程演示如何调用“DoesItExist”例程。
Sub MyTestRoutine()
'this first example tests if a specific file exists
'including a "False" setting for the dirOnly variable is optional
If DoesItExist("C:\Users\<userID>\Documents\Test\Mydoc.docx") Then
Debug.Print "File Exists"
Else
Debug.Print "File Does Not Exist"
End If
'the next example tests if a directory exists,
'the "True" setting for the dirOnly variable is required for directories
If DoesItExist("C:\Users\<userID>\Documents\Test", True) Then
Debug.Print "Directory Exists"
Else
Debug.Print "Directory Does Not Exist"
End If
End Sub
Public Function DoesItExist(ByRef pathName As String, Optional ByRef dirOnly As Boolean) As Boolean
'this routine checks if a file or folder exists on the system
'it runs on either a Windows based version of Office or a Mac version
'if Mac Office then only for the Office 365, 2016, 2019, or later)
Select Case dirOnly
Case True
If Dir(pathName, vbDirectory) = vbNullString Then
DoesItExist = False
Else
DoesItExist = True
End If
Case False
If Dir(pathName, vbNormal) = vbNullString Then
DoesItExist = False
Else
DoesItExist = True
End If
End Select
End Function
推荐阅读
- java - 如何使用 jmockit 从静态方法中抛出异常
- java - JDBC 模板多次执行选择查询
- node.js - 如何在哈巴狗/玉模板中显示异步/等待结果
- html - 如何让我的底部边框显示在右侧
- javascript - 如何使用 JavaScript 在动态内容之间创建链接?
- java - 希伯来语的 Joda-Time 格式无效异常
- kotlin - kotlin 只读 var 在密封的数据类结构中
- javascript - 有没有办法提高 MSAL 浏览器 js 登录的性能?
- typescript - 我可以根据对象中的其他属性推断类型吗?
- android - 如何在 Android Studio 的项目文件夹设置下拉列表中包含“Android”?