【问题标题】:Adjusting late binding for opening Word documents调整后期装订以打开 Word 文档
【发布时间】:2020-06-03 12:56:23
【问题描述】:

我正在尝试将工作代码从早期绑定调整为后期绑定,以避免用户使用不同的参考版本或缺少参考问题。特别是避免早期绑定 Microsoft Word 参考。

根据下面的代码,我正在使用 Microsoft Excel 创建一个 Microsoft Word 对象并打开 Word 文档进行处理。我将变量更改为对象,但挂断了 --> Set wrdDocument = wrdApplication.Documents.Open(strPath) 打开 word 但随后挂断,并建议它正在等待资源完成操作。

我需要做些什么才能通过后期绑定使其工作?我尝试在不设置的情况下添加值,但不确定需要发生什么。我确信这与不需要将文档设置为与变量相同有关,但我不确定如何...

非常感谢任何帮助!

Function AddRemoveWatermark()
    'Word Variables
    Dim wrdApplication As Object
    Dim wrdDocument As Object

    Set wrdApplication = CreateObject("Word.Application")

    Dim strDocumentName As String
    Dim strPath As String
    Dim strBBPath As String
    Dim lngCount As Long
    Dim fso As Object

    strBBPath = "C:\Users\" & (Environ$("Username")) & "\AppData\Roaming\Microsoft\Document Building Blocks\1033\" & lngMicrosoftVersion & "\Built-In Building Blocks.dotx"
    Set fso = CreateObject("Scripting.FileSystemObject")

    ' Open the file dialog
    With Application.FileDialog(1)  'msoFileDialogOpen
        .AllowMultiSelect = True
        .Show

        'Set wrdApplication = New Word.Application
        AddRemoveWatermark = .SelectedItems.Count

        ' Display paths of each file selected
        For lngCount = 1 To .SelectedItems.Count
            strPath = .SelectedItems(lngCount)
            Set wrdDocument = wrdApplication.Documents.Open(strPath)

            strDocumentName = wrdDocument.FullName 'Record the document name
            wrdApplication.Templates.LoadBuildingBlocks
        Next lngCount
    End With
End Sub

【问题讨论】:

  • 对于调试目的,最好让 Word 应用程序可见:如果出现错误或打开文件会触发一个对话框,您将不会看到。
  • 如果您在需要支持的最早的 Office 版本上编译您的早期绑定项目,您将不会遇到任何库引用问题 - 而且代码运行速度会更快。但是,如果您的代码使用更高版本中引入的功能,这并不能保证您不会遇到问题。不过,当您第一次在要编译以进行项目的系统上运行代码时,这一点就会变得很明显。

标签: excel vba ms-word binding


【解决方案1】:

这里是代码。

我跳过了 BBPath 变量和 fso 对象,因为它们在初始化后没有被使用。

Sub OpenWordDocsFromExcelLateBinding()

    ' Declare objects
    Dim wrdApplication As Object
    Dim wrdDocument As Object

    ' Declare other variables
    Dim wrdDocumentFullPath As String
    Dim wrdDocumentName As String
    Dim documentCounter As Integer

    ' Check if Word is already opened
    On Error Resume Next

    Set wrdApplication = GetObject(, "Word.Application")

    If Err.Number <> 0 Then
        ' Open a new instance
        Set wrdApplication = CreateObject("Word.Application")
        wrdApplication.Visible = True
    End If

    ' Reset error handling
    Err.Clear
    On Error GoTo 0

    ' Open file dialog
    With Application.FileDialog(1)  'msoFileDialogOpen
        .AllowMultiSelect = True
        .Show

        'Set wrdApplication = New Word.Application
        documentCounter = .SelectedItems.Count

        ' For each document selected in dialog
        For documentCounter = 1 To .SelectedItems.Count

            ' Get full path and name of each file selected
            wrdDocumentFullPath = .SelectedItems(documentCounter)
            wrdDocumentName = Mid(.SelectedItems(documentCounter), InStrRev(.SelectedItems(documentCounter), "\") + 1)

            ' Check if document is already opened
            On Error Resume Next

            Set wrdDocument = wrdApplication.documents(wrdDocumentName)

            If Err.Number <> 0 Then
                ' Open word document
                Set wrdDocument = wrdApplication.documents.Open(wrdDocumentFullPath)
            End If

            ' Reset error handling
            Err.Clear
            On Error GoTo 0

        Next documentCounter

    End With

    ' This extra step is only because OP (Original Poster) had it in the question
    wrdApplication.Templates.LoadBuildingBlocks


End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多