0
votes

I have two codes that create and/or open a Word document from Excel, both contain similar coding including the syntax

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

In one sub the document opens perfectly fine and in the other sub I get a runtime error 429 on the above syntax, but only when Word is not open. When Word is open, the function is working just fine.

Part of the code of working sub

Cells(ActiveCell.Row, ActiveSheet.Range("zz_templates").Column).Activate

Range("zz_preventloop").Value = "x"
Application.ScreenUpdating = False

Dim DocType As String
If Range("zz_officeversion").Value = "previous to 2007" Then
DocType = ".doc"
Else
DocType = ".docx"
End If

Dim filename As String
filename = Range("zz_envelope_documents").Value + "/" + Cells(ActiveCell.Row, ActiveSheet.Range("zz_locations_doc").Column).Value + "/"
filename = filename + Cells(ActiveCell.Row, ActiveSheet.Range("zz_eDMSname").Column).Value + DocType
      
If Len(filename) < 256 Then
'check the document type
    If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_doc").Column).Value = ".url" Then ''Opening the .url shortcut
        On Error Resume Next
        ActiveWorkbook.FollowHyperlink Range("zz_envelope_templates").Value + "/" + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_locations_temp").Column).Value + "/" _
        + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_hidden_eDMStemp").Column).Value + ".url", NewWindow:=True
    Else
        If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_doc").Column).Value = ".docx" Then
            Application.Calculate
            On Error Resume Next
            Set Word = GetObject(, "Word.Application")
            If Word Is Nothing Then
                Set Word = CreateObject("Word.Application")
            End If

 Rest of sub

Part of code of non-working sub

Cells(ActiveCell.Row, ActiveSheet.Range("zz_templates").Column).Activate

Range("zz_preventloop").Value = "x"
Application.ScreenUpdating = False
      
Dim DocType As String
If Range("zz_officeversion").Value = "previous to 2007" Then
DocType = ".doc"
Else
DocType = ".docx"
End If
    
'check the document type
If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_template").Column).Value = ".url" Then 
''Opening the .url shortcut
    On Error Resume Next
    ActiveWorkbook.FollowHyperlink Range("zz_envelope_templates").Value + "/" + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_locations_temp").Column).Value + "/" _
    + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_hidden_eDMStemp").Column).Value + ".url", NewWindow:=True
Else
    If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_template").Column).Value = ".docx" Then
        Set Word = GetObject(, "Word.Application")
        If Word Is Nothing Then
            Set Word = CreateObject("Word.Application")
        End If

Rest of sub

What am I overlooking in the second function that it is not working when Word is not open?

1

1 Answers

0
votes

The problematic one is not identic with the working one:

Your code is missing On Error Resume Next.

It should be:

On Error Resume Next
Set Word = GetObject(, "Word.Application")
If Word Is Nothing Then
     Err.Clear: On Error GoTo 0 'good to clear the error and let the code raising an error if the case
     Set Word = CreateObject("Word.Application")
End If
On Error GoTo 0

The logic of the above code is:

  1. It tries finding the Word open session and create the Word object, if such a session exists.
  2. If such a session does not exist, it would raise an error, but On Error Resume Next overpass the error.
  3. If the Word object could not be created from the existing session, being Nothing, a new session is created.