View Full Version : Solved: Deleting Rows from a Table in Word
arcanedevice
05-24-2009, 11:49 PM
I'm trying to delete some unneeded rows in a table if a certain condition is met in a previous document. I've used the code below:
If CATFile.Tables(1).Cell(7, 1).Range.Text = "Co-requisite units" Then
Template.Tables(1).Rows(6).Delete
Template.Tables(1).Rows(7).Delete
Template.Tables(1).Rows(8).Delete
Else
End If
And while it doesn't return any errors, it doesn't actually delete the three rows. Can anyone tell me where I'm going wrong?
lucas
05-25-2009, 09:21 AM
Either your if statement is wrong or your reference to template or table number. The code works for me. See attached.
fumei
05-25-2009, 12:27 PM
Absolutely. Something is not making a proper reference. BTW: "Template" is not a good name. Are you making a proper reference to whatever is "Template"...which is my point. What the heck is "Template"?
BTW2: are you using Option Explicit? It does not look like it. If Option Explicit was there, then:
"And while it doesn't return any errors, it doesn't actually delete the three rows. "
would not likely happen. You would get an error.
arcanedevice
05-25-2009, 04:16 PM
Thanks for the help - it's certainly confusing me.
1. I know that Template isn't the best name, but that was what I had been working with at the time I raised the question as I'd exhausted a few other options!
2. There is definitely something wrong in the CATFile document, as I've built a little check on open and it returns the wrong message!
lucas
05-25-2009, 08:00 PM
I ran a slightly different check: see attached
arcanedevice
05-25-2009, 08:07 PM
Thanks lucas. I notice that the second line of your messagebox appears to have a bullet point in it, which would explain why mine doesn't work correctly. So I'll have to work out where that is hidden...
fumei
05-26-2009, 11:14 AM
The reason there is that square thing in the messagebox is:
MsgBox "Row 7-Column 1 text is: " & _
Tables(1).Cell(7, 1).Range.Text
Range.Text of a table cell includes the end-of-cell marker.
To NOT include it (i.e. get ONLY the text), you need to strip it off. There are two possible routes. If you get text content from Word table cells often, it is useful to have (and use) either of the following functions.
1. passing the cell Range.Text as a String to a function:
Function CellText(strIn As String) As String
CellText = Left(strIn, Len(strIn) - 2)
End Function
' EXAMPLE
MsgBox "Row 7-Column 1 text is: " & _
CellText(Tables(1).Cell(7, 1).Range.Text)
2. passing the cell itself, as an object, to a function:
Function CellText2(aCell As Cell) As String
Dim sText As String
sText = aCell.Range.Text
CellText2 = Left(sText, Len(sText) - 2)
End Function
' EXAMPLE
MsgBox "Row 7-Column 1 text is: " & _
CellText2(Tables(1).Cell(7, 1))
In both examples, the function returns the text content of a table cell without the end-of-cell marker.
arcanedevice
05-26-2009, 03:51 PM
Thanks fumei
I actually thought of that last night while on the train after your suggestion on my other posting, and thought I'd give it a go, and yes, it works. So once again, thanks for your expert advice again!
:beerchug:
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.