none
Word automation RRS feed

  • Question

  • Hi all,

    Can someone explaine the difference between

    Dim appWord As Word.Application

    Set appWord = CreateObject("Word.Application")
    Set appWord = New Word.Application
    Set appWord = GetObject(, "Word.Application")

    Friday, October 14, 2016 8:17 PM

All replies

  • Can someone explaine the difference between

    Dim appWord As Word.Application

    Set appWord = CreateObject("Word.Application")
    Set appWord = New Word.Application
    Set appWord = GetObject(, "Word.Application")

    The first two -- CreateObject and New Word.Application -- both create a new instance of the Word application object and assign that to appWord.  The last one, GetObject, locates an existing instance of Word -- that is, a Word application object that is already open -- and assigns that to appWord.

    The difference between the first two is that CreateObject doesn't require you to have a reference to the Word object library, while "Set appWord = New Word.Application" does require a reference to Word in order for "Word.Application" to be understood.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html


    • Edited by Dirk Goldgar Friday, October 14, 2016 9:04 PM
    • Proposed as answer by ryguy72 Tuesday, October 18, 2016 2:44 PM
    Friday, October 14, 2016 9:02 PM
  • > The difference between the first two is that CreateObject doesn't require you to have a reference to the Word object library, while "Set appWord = New Word.Application" does require a reference to Word in order for "Word.Application" to be understood.

    This is called late binding versus early binding. The reason this is important is if the Word functionality is a minor aspect of the overall app, and some users may not have Word installed. In that case you can test if Word is there with GetObject/CreaeteObject, and if not, then "gracefully degrade" the application. In the case of early binding Word MUST be available for the application to start, even if user never runs the Word functionality.


    -Tom. Microsoft Access MVP


    Saturday, October 15, 2016 7:04 PM
  • Thanks

    That  also means that with late binding you don't need to dimention a variable

    Dim appWord As Word.Application

    And how can we detect if word is available?

    Sunday, October 16, 2016 6:32 PM
  • That  also means that with late binding you don't need to dimention a variable

    Dim appWord As Word.Application

    And how can we detect if word is available?

    You still have to declare a variable, but you would just declare it as Object, which doesn't require any particular object library:

        Dim appWord As Object

    You can detect whether Word is installed by trying to create an instance of the Word application and trapping the error that is raised if the object can't be created.  For example:

        Dim appWord As Object
        Dim lngError As Long

        On Error Resume Next
        Set appWord = CreateObject("Word.Application")
        lngError = Err.Number
        On Error GoTo 0   ' or your own error handler

        If lngError <> 0 Then
            MsgBox "Unable to create Word application.  Is Word installed?"
        Else
            ' Go ahead and work with the Word application referenced by appWord.
        End If


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Sunday, October 16, 2016 7:11 PM
  • I usually use something along the line of :

    Public oWord                  As Object
    Public oWordDoc               As Object
    Public bWordOpened            As Boolean
    
    
    Public Sub LaunchWord(Optional bVisible As Boolean = True)
        On Error Resume Next
        
        Set oWord = GetObject(, "Word.Application")    'Bind to existing instance of Word
        If Err.Number <> 0 Then    'Could not get instance of Word, so create a new one
            Err.Clear
            On Error GoTo Error_Handler
            Set oWord = CreateObject("Word.Application")
            bWordOpened = False
        Else    'Word was already running
            bWordOpened = True
        End If
    
        On Error GoTo Error_Handler
        oWord.Visible = bVisible
    
    Error_Handler_Exit:
        On Error Resume Next
        Exit Sub
    
    Error_Handler:
        MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Source: LaunchWord" & vbCrLf & _
               "Error Description: " & Err.Description _
               , vbOKOnly + vbCritical, "An Error has Occured!"
        Resume Error_Handler_Exit
    End Sub

    You could also use a simple function like the following to determine if Word is installed or not on a given computer.

    Function IsMSWordInstalled() As Boolean
        On Error GoTo Error_Handler
        Dim WSHShell              As Object
    
        Set WSHShell = CreateObject("Wscript.Shell")
        If Len(WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinWord.exe\")) > 0 Then IsWordInstalled = True
    
    Error_Handler_Exit:
        On Error Resume Next
        If Not WSHShell Is Nothing Then Set WSHShell = Nothing
        Exit Function
    
    Error_Handler:
        If Err.Number <> -2147024894 Then
            MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
                   "Error Number: " & Err.Number & vbCrLf & _
                   "Error Source: IsMSWordInstalled" & vbCrLf & _
                   "Error Description: " & Err.Description & _
                   Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
                   , vbOKOnly + vbCritical, "An Error has Occured!"
        End If
        Resume Error_Handler_Exit
    End Function


    Daniel Pineault, 2010-2016
    Microsoft MVP Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net

    Sunday, October 16, 2016 10:31 PM