0
votes

I am attempting to adjust working code from early binding to late binding to avoid issues with users with different reference versions or missing reference issues. Specifically avoiding early binding for the Microsoft Word reference.

Per the below code, I am using Microsoft Excel to create a Microsoft Word object and open word documents for processing. I changed the variables to objects, but get hung up on --> Set wrdDocument = wrdApplication.Documents.Open(strPath) where it opens word but then hangs up, and advises it is waiting on a resource to complete an action.

What do I need to do to get this to work via late binding? I tried adding the value without setting it, but an unsure what needs to happen. I am sure it has something to do with not needing to set the document the same with a variable, but I am not sure how...

Any help is greatly appreciated!

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
For debugging puposes it's best to make the Word application visible: if there are errors or opening the file triggers a dialog you're not going to see that. - Tim Williams
If you compile your early-binding project on the earliest Office version you need to support, you won't have any library reference issues - plus the code will run faster. That's no guarantee you won't run into problems if your code uses features introduced in later versions, though. That'll become evident the first time you run the code on the system you compile to project on, though. - macropod

1 Answers

0
votes

Here is the code.

I skiped the BBPath variable and the fso object as it wasn't used after their initialization.

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