Can macros just check tables alone

D

Designingsally

I got a table with me which has 4 rows and 4 coloums. I want the macros do
add comments if certain words are encountered in that table alone. I dont
want the comments to be added inside that table alone. Not rest of the
document or rest of the tables present in the doc. The column headings are
sno, topic, aim, mark. The rows will have details corresponding.

I know
Selection.Comments.Add _
Range:=Selection.Range, Text:="This word is avoided
while writing an elearning course"
is for added the comments. but i dont know how to make macros work for
that particular alone.

is this possible? post in ur views.
 
D

Doug Robbins - Word MVP

Selection.Information(wdWithingTable) will be true if the selection is
located inside a table.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

Designingsally

hi
I m getting error namely- code cant be executed in the break mode. can u let
me what is the mistake.
the code is

Sub Comments()
'
' Comments Macro
'
'
Dim vFindText As Variant
Dim i As Long
vFindText = Array("will")
For i = 0 To UBound(vFindText)
If Selection.Information(wdWithingTable) = True Then
With Selection
.HomeKey wdStory
With .FInd
.ClearFormatting
.MatchWholeWord = True

Do While .Execute(findText:=vFindText(i), _
Forward:=True)
Selection.Comments.Add _
Range:=Selection.Range, Text:="This word is avoided
while writing an elearning course"
Loop

End With
End With
End If
Next

End Sub

--
I believe in Hope.

DesigningSally


Doug Robbins - Word MVP said:
Selection.Information(wdWithingTable) will be true if the selection is
located inside a table.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

Graham Mayor

The following should be on one line

Range:=Selection.Range, Text:="This word is avoided
while writing an elearning course"

Correct the error and reset the macro.
See http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Pesach Shelnitz

Hi Sally,

Graham's suggestion won't solve all of your problems. There is also a
spelling error in the your code. The constant wdWithingTable should be
changed to wdWithinTable. Then in the VB Editor, on the menu bar click Run
and then click Reset. This should let your code run.

I don't think that your code will do what you want. You added the condition
that Doug suggested in such a way that it only checks whether the original
position of the cursor is within a table. If this condition is true, it will
perform the search in the whole document.

I'm also not sure if you want to search one table or all the tables in your
document. If you want to search only one table, the following modification of
yesterday's macro will do it.

Sub SearchAndAddCommentsInTable()
Dim myRange As Range
Dim vFindText As Variant
Dim i As Long

vFindText = Array("pen", "good", "this")
With Selection
If .Information(wdWithinTable) = True Then
Set myRange = .Tables(1).Range
Else
MsgBox "The cursor is not in a table."
Exit Sub
End If
End With
For i = 0 To UBound(vFindText)
myRange.Select
With Selection.Find
.ClearFormatting
.MatchCase = False
.Wrap = wdFindStop
Do While .Execute(findText:=vFindText(i), _
Forward:=True)
If Selection.Start > myRange.End Then
Exit Do
End If
Selection.Comments.Add _
Range:=Selection.Range, Text:="Hi"
Loop
End With
Next
myRange.Select
Selection.Collapse Direction:=wdCollapseEnd
Set myRange = Nothing
End Sub

If you want to search all the tables in the document, you can add a loop
based on the ActiveDocument.Tables collection.
 
G

Graham Mayor

Pesach said:
Graham's suggestion won't solve all of your problems. There is also a
spelling error in the your code. The constant wdWithingTable should be
changed to wdWithinTable.

Good catch - I should have tested before replying and then would have caught
the typo :eek:(

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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