调整Excel VBA以使用后期绑定打开Word文档

时间:2018-10-21 22:47:14

标签: excel vba excel-vba binding ms-word

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

根据以下代码,我正在使用Microsoft Excel创建Microsoft Word对象并打开Word文档进行处理。我将变量更改为对象,但是挂断了->设置wrdDocument = wrdApplication.Documents.Open(strPath),在其中打开单词,然后挂断,并建议它在等待资源来完成操作。

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

非常感谢您的帮助!

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

1 个答案:

答案 0 :(得分:0)

这是代码。

我跳过了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