VBA to search each row of a word table column
Thanks - that's better, although there is a reason I included the 'On Error GoTo' code. That's because for many VBA functions to work on a table, you need only place the cursor to the immediate right of a table and not in it. When it's to the right, VBA can count the table's columns and rows, for example, just as it can when it's actually within the table. VBA also considers 'If .Information(wdWithInTable)' to be true when the cursor is to the immediate right of the table.
Your line ' Col = .Cells(1).ColumnIndex" tells the code which column to search, but if the cursor is to the right of the table, 'If .Information(wdWithInTable)' will be true but the code will error because there is no column index. That's why I had the 'On Error GoTo NotATable' statements (see attached). It's the only way I could find to tell the macro that the pointer was actually inside the table.
Now it works the way I want it except that it still doesn't perform a MATCHWHOLEWORD search, at least not to my liking. My test table has 21 cells containing 'TO BE SCANNED' and 30 containing "scanned' (all in the same column). Searching on 'TO BE SCANNED' produces 21 hits, as it should, but searching on 'scanned' produces 51, which means it is counting both the cells containing 'TO BE SCANNED' as well as those containing 'scanned'. What I want is to get 30 results when I search on 'scanned'. Isn't that what a MATCHWHOLEWORD search does? That property is true in the code but doesn't seem to work as I expected it to, so is there another way?
|