我在Word中运行一个宏,其中包括在文档中已经存在的表的底部添加一行并填充某些单元格。奇怪的是,对于大多数文档来说,它可以工作,但是有几个文档我收到了运行时错误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如果需要,我可以提供宏调用的子类和函数,但我认为它们与问题无关。我相信问题就在这些文件的某个地方,在表格格式上,但我找不到任何解释,也找不到与其他文件中的表格有任何不同之处。
发布于 2015-06-10 20:19:55
嵌套表搞乱了单元格集合。一旦手动合并/拆分最后一行的单元格,然后添加新行,事情就变成.不一样。保存为rtf,看看代码,然后抓起你的头。
用一个(第一个?)第二次?)“标准”行用于计数列并调整代码,以防最后一行的列计数/单元格计数与该“规范”不同。使用“选择”和一个断点来调查麻烦的表,学习如何处理这些特殊情况。
https://stackoverflow.com/questions/30758797
复制相似问题