0

我正在使用 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.

4

1 回答 1

0

如果您添加对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
于 2019-05-31T15:09:37.137 回答