VBA error in Ofc 2003

J

John Beckner

Word 2003 Macro error

I have a macro that I have used daily in Word 2000 and
2002 (XP) versions with no problem. I am now receiving
error 5992. Exact message is:

Run-time error '5992':

Cannot access individual columns in this collection
because the table has mixed cell widths.

I use Word as my time sheet at work. It has 5 columns.
When I run this macro it goes to the bottom of the table
(only 1 table in document), then, if that row is not
empty, a new row is added, and the current time is placed
in column #2. The macro is:

Sub TimeRow()
'
' TimeRow Macro
' Written 1/1/2002 by John Wm Beckner
'
' Checks Last Row, 2nd column cell contents. If something
is there,
' add a row. Select last cell in 2nd column. Put the time
in the cell.
' Advance to next cell and await input.
'
Set otimetable = ActiveDocument.Tables(1) 'use 1st
(and only) table in document
Set oTimeColumn = otimetable.Columns(2) 'time column
*NOTE ERROR OCCURS HERE*
Dim iLastCell As Integer
iLastCell = oTimeColumn.Cells.Count
Set oLastTimeCell = oTimeColumn.Cells(iLastCell)
oLastTimeCell.Select
If Asc(Selection.Text) <> 13 Then
Selection.SelectRow
Selection.InsertRowsBelow
iLastCell = oTimeColumn.Cells.Count
Set oLastTimeCell = oTimeColumn.Cells(iLastCell)
oLastTimeCell.Select
End If
'put time in cell
InsertTime ' another macro
End Sub


The error occurs on the second stmt. The object
otimetable.Columns(2) does not exist because the error
states my columns have mixed cell widths. They don't. I
have set them to fixed widths and this has not resolved
the issue.

Any assistance appreciated. Upon request, I can send
the .DOC file.

John
(e-mail address removed)
 
W

Wei-Dong Xu [MSFT]

Hi John,

Thank you for posting in MSDN managed newsgroup!

From my understanding to this issue, you will obtain the '5992' error when you are going to access the cell in the column 2 of the first table in your
word documentation.

For the column, word will assume one column of one table to have the same width which means word assumes that all the cells in one column will
have all the same width. If one cell has a different width from others, word will report run-time error.

I'd suggest one workaround will help you a lot regarding this issue by locating the second cell of the last row from rows collection, not the columns
collection. There is one sample code for you.
'Code begin -----------------------------------------------------------
...
Dim objTbl As Word.Table
Dim objCel As Word.Cell

Set objTbl = Application.ActiveDocument.Tables(1)

'locate the second cell from the rows collections
Set objCel = objTbl.Rows(objTbl.Rows.Count).Cells(2)
objCel.Select

If Asc(Selection.Text) = 13 Then
MsgBox "Empty table cell"
End If
...

'Code end-------------------------------------------------------------

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Wei-Dong Xu [MSFT]

Hi John,

Furthermore, if one cell in one column contain two columns, word will also report 5992 error when we access this column. So far as I know, this is by
design. In this scenario, we will need to use rows collection to access the cells.

Please feel free to let me know if you have any further questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

John

Thank you for your quick response. I have already
reprogrammed the macro, though your method is better than
my replacement method. My concern was that there may have
been a bug introduced in Word 2003, as I have been using
the same macro and document for years. After receiving
your reply, I created the next macro to check for
mismatched cell widths. After running the macro below, I
received the final msg, "No mismatched cell widths found!"

Sub CellWidthTest()
Dim objTbl As Word.Table
Dim liRow As Integer, liCol As Integer
Dim lnPrev(1 To 4) As Single, lnCurr(1 To 4) As Single
Dim lbMismatched As Boolean
lbMismatched = False
Set objTbl = Application.ActiveDocument.Tables(1)

'Store Base Cell widths for row 1
For liCol = 1 To 4
lnPrev(liCol) = objTbl.Rows(1).Cells(liCol).Width
Next liCol
For liRow = 2 To objTbl.Rows.Count
For liCol = 1 To 4
lnCurr(liCol) = objTbl.Rows(liRow).Cells
(liCol).Width
If lnCurr(liCol) <> lnPrev(liCol) Then
lbMismatched = True
MsgBox "Mismatched Cell size: " & vbCrLf &
_
Format(lnPrev(liCol)) & "," & _
Format(lnCurr(liCol)) & vbCrLf & _
"Row #" & Format(liRow) & vbCrLf &
_
"Col #" & Format(liCol)
Exit For
End If
Next liCol
If lbMismatched Then
Exit For
End If
Next liRow
If Not lbMismatched Then
MsgBox "No mismatched cell widths found!"
End If
End Sub
 
W

Wei-Dong Xu [MSFT]

Hi John,

Thank you for replying and more information about the troubleshooting!

For more information regarding this issue, it will be greatly appreciated that you mail your word doc to me. I have sent one mail to you for this.

Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top