无法使用ExceltoWord设置FSO = CreateObject!加入 [英] Failure to set FSO = CreateObject with ExceltoWord! Add-in

查看:110
本文介绍了无法使用ExceltoWord设置FSO = CreateObject!加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Excel加载项ExceltoWord从Excel工作表中自动填充Word文档.我已遵循原始开发人员

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

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

以下是对原始答案的修改,因为基于注释,您在系统上使用FSO(文件系统对象)代码仍然遇到问题.

以下VBA例程将确定是否存在指定的目录或文件,而不是使用FSO.该例程称为"DoesItExist",我提供了一个示例例程,该例程演示了如何调用"DoesItExist"例程.

  Sub MyTestRoutine()'第一个示例测试特定文件是否存在'包括dirOnly变量的"False"设置是可选的如果DoesItExist("C:\ Users \< userID> \ Documents \ Test \ Mydoc.docx")然后Debug.Print文件存在"别的Debug.Print文件不存在"万一'下一个示例测试目录是否存在,'dirOnly变量的"True"设置对于目录是必需的如果DidsItExist("C:\ Users \< userID> \ Documents \ Test",True)则Debug.Print目录存在"别的Debug.Print目录不存在"万一结束子公共函数DidItExist(ByRef路径名作为字符串,可选的ByRef dirOnly作为布尔型)作为布尔型'此例程检查系统上是否存在文件或文件夹'它可以在基于Windows版本的Office或Mac版本上运行如果是Mac Office,则仅适用于Office 365、2016、2019或更高版本)仅选择案例目录真实情况如果Dir(pathName,vbDirectory)= vbNullString然后是否存在=错误别的是否存在=真实万一大小写错误如果Dir(pathName,vbNormal)= vbNullString然后是否存在=错误别的是否存在=真实万一结束选择结束功能 

I'm using the Excel Add-in ExceltoWord to auto-populate a Word Document from an Excel worksheet. I've followed the instructions from the original developer here.

I'm using the "I created generic bookmark indicators, in Word" and "I put bookmark indicators directly in cells - Left" options with "Delete the Word doc" at the end. When I save settings I get an MS Visual Basic error

Run-time error '429:' ActiveX component can't create object.

I've tried switching different formats of Excel sheet and Word Doc and Word Template as well as leaving the Word Doc closed and opened when saving the configuration.

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 displays an error on Set FSO = CreateObject("Scripting.FileSystemObject") 'late binding.

解决方案

If you add a Reference to Microsoft Scripting Runtime (VBE > Tools > References...) then enable the "Early Binding" code that you currently have commented out, and your code will work.

To set the reference in the Visual Basic Editor (VBE) go to the Tools menu and select the References... function.

Then from the References dialog that opens, scroll until you locate Microsoft Scripting Runtime, and mark it and then click OK.

In your current code remove the comment marks on the two lines marked as "Early Binding" and apply comment marks on the two lines marked as "Late Binding".

The following is an edit to the original answer because, based on comments, you are continuing to have problems with using FSO (File System Object) code on your system.

Instead of using FSO the following VBA routine will determine if either a specified Directory or File exists. The routine is called "DoesItExist" and I have included an example routine that demonstrates how to call the "DoesItExist" routine.

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

这篇关于无法使用ExceltoWord设置FSO = CreateObject!加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆