 |
| Word VBA Discuss using VBA to program Word. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 7th, 2011, 02:52 PM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Search for listbox item in word table and delete the rows
Hello,
Please somebody help me with this problem: I am trying to select an item in a listbox, search this item in a specified table's cell, and once it's found, I want to delete the row. But so far it seems my code is not reading the items on the table. This is what i have so far
Dim j As Integer
With ActiveDocument.Tables(4)
For j = .Rows.Count To 1 Step -1
If .Cell(j, 1).Range.Text = lstBufferSalt.Value Then
.Rows(j).Delete
Else
MsgBox ("Record not found in word table")
End If
Next j
End With
It kept giving me the MsgBox info, because it's not deleting any record from the table.
Please experts, what am I doing wrong?
Bitex
|
|

July 7th, 2011, 10:18 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
|
|
Hi
Your code loops through only the first column. Assuming it to be what you want the problem might arise with the following statement
.Cell(j, 1).Range.Text = lstBufferSalt.Value
If you try to 'watch' the value of .Cell(j, 1).Range.Text in Debug mode you will find some extra character at the end. This is because of the way Table cells are formatted in Word.
You can check by trimming the last character etc
Also the comparison here is case sensitive .. you can also handle by
lcase(.Cell(j, 1).Range.Text) = lcase(lstBufferSalt.Value)
or use String comparison functions that are coming with VBA
Cheers
Shasur
|
|

July 8th, 2011, 11:35 AM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, thanks for your reply, but I have used all your suggestions, and nothing seems to be happening. I counted the invisible character and trimmed as well, but it still kept saying no record found. As a matter of fact, I specifically asked it to deleted a specific by providing the number cell(3,1), but it still dint read it.
However, I used a msgbox to display the content of the box without going through the loop, and it displayed the message, and I even counted the number of character with trim, and they all worked.
So, I think you are right about the code going through only the first row, but I don't know how to make it go through the whole table to do the loop.
Any idea will be grately appreciated.
Thanks
|
|

July 9th, 2011, 11:17 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
|
|
Hi
Here is a hint:
Code:
Sub LoopThroughTable()
Dim oTable As Table
Set oTable = ActiveDocument.Tables(1)
For i1 = 1 To oTable.Rows.Count
For j1 = 1 To oTable.Columns.Count
If InStr(1, oTable.Cell(i1, j1).Range.Text, "complexity", vbTextCompare) Then
oTable.Rows(i1).Delete
End If
Next j1
Next i1
End Sub
Cheers
Shasur
|
|

July 11th, 2011, 09:42 AM
|
|
Authorized User
|
|
Join Date: Jun 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you for your reply again Shasur, but I just recently got it using the "len" and "left" procedure to count and trim the number of characters in the table.
I finally figured out the problem, I stupidly had a preceeding procedure that deletes the chosen item from the list box before the comparison with the word table item kicks in. So, basically I was comparing the value in the table to an empty or wrong value in the listbox, that's why it dint work. so I have rectified that and here is what I have working now:
Dim j As Integer
With ActiveDocument.Tables(4)
If Me.lstBufferSalt.Value = lstBufferSalt Then
For j = .Rows.Count To 1 Step -1
If Left(.Cell(j, 1).Range.Text, Len(.Cell(j, 1).Range.Text) - 2) = lstBufferSalt.Value Then
If MsgBox("Are you sure you want to delete this Information from the table?", vbYesNo + vbQuestion, _
Left(.Cell(j, 1).Range.Text, Len(.Cell(j, 1).Range.Text) - 2)) = vbYes Then
.Rows(j).Delete
Me.lstBufferSalt.RemoveItem (Me.lstBufferSalt.ListIndex)
End If
End If
Next
End If
End With
Thanks so much for your responses! Enjoy your day
|
|
 |