|
-
Apr 10th, 2009, 09:28 PM
#1
Thread Starter
New Member
Delete specific table rows in Word
I have found the following code which works well with a document with one Word table. I need the code to go through the entire document which has a large number of tables. All the tables are two columns (multiple rows) and I need to delete the whole row if it contains a certain word in cell 1.
Sub DeleteRows()
Dim TargetText As String
Dim oRow As Row
If Selection.Information(wdWithInTable) = False Then Exit Sub
TargetText = InputBox$("Enter target text:", "Delete Rows")
For Each oRow In Selection.Tables(1).Rows
If oRow.Cells(1).Range.Text = TargetText & vbCr & Chr(7) Then oRow.Delete
Next oRow
End Sub
I would appreciate any assistance you can offer.
Thanks
Last edited by Gizzmo1; Apr 12th, 2009 at 07:39 AM.
Reason: Resolved
-
Apr 11th, 2009, 06:43 AM
#2
Re: Delete specific table rows in Word
vb Code:
dim t as table for each t in activedocument.tables For Each oRow In t.rows If oRow.Cells(1).Range.Text = TargetText & vbCr & Chr(7) Then oRow.Delete Next oRow next t
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 11th, 2009, 07:10 AM
#3
Re: Delete specific table rows in Word
Moved To Office Development
-
Apr 11th, 2009, 08:48 AM
#4
Thread Starter
New Member
Re: Delete specific table rows in Word
Many thanks for providing a quick answer to my question, however, I am still having trouble. Although the attached document appears to have the text in tables, the VB code will not run. I only want to delete the row if it only contains the text "N/A" (which I have highlighted in yellow).
Thanks in advance.
Last edited by Gizzmo1; Apr 12th, 2009 at 07:43 AM.
Reason: Still having trouble
-
Apr 12th, 2009, 06:17 AM
#5
Re: Delete specific table rows in Word
here is some working code
there were 2 issues, table within tables and i could not make the test string = to the cell content, even though i could see that they were identical and checked the ascii values of every character in both strings
vb Code:
Sub DeleteRows() Dim d As Document, t As Table, t1 As Table Dim TargetText As String Dim oRow As Row Set d = ActiveDocument If Selection.Information(wdWithInTable) = False Then Exit Sub TargetText = "N/A " 'InputBox$("Enter target text:", "Delete Rows") For Each t In d.Tables For Each t1 In t.Tables For Each oRow In t1.Rows If InStr(oRow.Cells(1).Range.Text, TargetText) = 1 Then oRow.Delete Next Next Next End Sub
give it a good test first
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 12th, 2009, 07:42 AM
#6
Thread Starter
New Member
Re: Delete specific table rows in Word
Once again, a big thank you for the code which you provided, it works like a charm! I really do appreciate the time you have taken to resolve my problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|