无法使用ExceltoWord设置FSO = CreateObject!加入

时间:2019-05-31 13:46:50

标签: excel vba ms-word createobject fso

我正在使用Excel加载项ExceltoWord从Excel工作表中自动填充Word文档。我遵循了原始开发者here的指示。

我使用的是“我在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上显示错误。

1 个答案:

答案 0 :(得分:0)

如果您添加对 Microsoft脚本运行时的引用(VBE>工具>引用...),则启用您当前已注释掉的“早期绑定”代码,您的代码将起作用。

要在Visual Basic编辑器(VBE)中设置引用,请转到“工具”菜单,然后选择“引用...”功能。

enter image description here

然后从打开的“引用”对话框中滚动,直到找到Microsoft Scripting Runtime,然后对其进行标记,然后单击“确定”。

enter image description here

在您当前的代码中,删除标记为“早期绑定”的两行上的注释标记,并在标记为“晚期绑定”的两行上应用注释标记。

以下是对原始答案的编辑,因为基于注释,您在系统上使用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