1

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.

5
  • Are you sure that all documents have the Custom properties "Date" and "Issue"? It could be those after all. Also please state the Word version. Commented Jun 10, 2015 at 16:04
  • P.S: I just managed to create a table in a Word document without that table to appear in the ActiveDocument.Tables collection! How? ==> By putting it in a text box... Commented Jun 10, 2015 at 16:07
  • The document properties are updated and the last row is inserted in the table, the macro stops when it tries to insert the text in the first Cell. Commented Jun 10, 2015 at 17:10
  • Fascinating! Gotta play a little with this tomorrow. Last row contains merged cells perhaps? Paragraph style with set page break after/before? That just might cause the new row to actually create a new table. :-? Something seems to screw up the Cells collection of that new row. Commented Jun 10, 2015 at 17:40
  • Thanks, you nailed it. The table had rows where the last cell was split. I merged the rows and the macro is now working. Commented Jun 10, 2015 at 18:04

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.