Runtime error 91: object variable or with block variable not set

754 Views Asked by At

I'm running a macro in Word which, among other things, adds a line to the bottom of a table already existing in the document and fills certain cells. The odd thing is that for the majority of the documents it Works, however there are a couple of documents for which I receive the Run Time error 91.

'Update the document properties, updates the header, updates the table of contents,
' and adds a file to the Version History table.
Sub zzAddVersionHistory(strUsuario As String, strDescripcion As String)
    Dim newDate As String
    Dim rowNumber As Integer
    Dim rowNew As Row
    Dim strIssue As String
    Dim ascIssue As Integer

    'Updates the Date property
    newDate = Format(Date, "dd/MM/yyyy")
    ActiveDocument.CustomDocumentProperties("Date").Value = newDate

    'Finds the version from the Issue property and updates the version
    If DocPropertyExists("Issue") = True Then
        strIssue = ActiveDocument.CustomDocumentProperties("Issue").Value
        ascIssue = (Asc(strIssue)) + 1 'Convierte el Issue en ascii y le suma uno
        strIssue = Chr(ascIssue)       'Convierte el ascii en caracter
        ActiveDocument.CustomDocumentProperties("Issue").Value = strIssue
    End If

    'Updates Header and footer
    zzActualizarHeaderFooter

    'Updates Fields
    zzActualizarCampos

    'Accepts changes in header y footer
    zzAcceptChangesInHeaderFooter

    'Adds a row to the table
    rowNumber = Application.ActiveDocument.Tables(1).Rows.Count
    Set rowNew = Application.ActiveDocument.Tables(1).Rows.Add

    'Inserts KTC Issue In first cell of the new row
    rowNew.Cells(1).Range.InsertAfter (strIssue) ''' Runtime-error here
    'Inserts Issued By in the third cell of the new row
    rowNew.Cells(3).Range.InsertAfter (strUsuario)
    'Inserts the Date in the fourth cell of the new row
    rowNew.Cells(4).Range.InsertAfter (newDate)
    'Inserts Description of Changes in the fifth cell of the new row
    rowNew.Cells(5).Range.InsertAfter (strDescripcion)

    'Updates the Table of Contents
    zzActualizarIndices
End Sub

If needed I can provide the subs and functions called by the macro, but I don't think they have anything to do with the issue. I believe the problem is somewhere in those documents, in the table format, but I could not find an explanation anywhere nor I can find any difference with the tables in other documents.

1

There are 1 best solutions below

0
On

Nested tables mess up the cells collection. Once you manually merge/split cells on the last row and then add a new row, things become... different. Save as rtf, look at the code, and scratch your head.

Use one (the first? second?) "standard" row to count the columns and adjust the code in case the column count / cells count of the last row differs from that "norm". Use "Selection" and a breakpoint to investigate the troublesome table to learn how to handle these special cases.