Adding comments to different words using vba

R

Raj

Hi,

I have a word document that contains words shown in the vba array
below.

The array of words in vba code are as follows ("May", "July",
"September")

I am looking for a solution where comments are added to each
occurrence of a word in the array. The comment would need to be
conditional. eg. For June the comment should have the text "Form 24
filing", For July, "IT Return Filing", for September, "Quarter
ending".

Thanks in advance for the help.

Regards,
Raj
 
G

Greg Maxey

Hi,

I have a word document that contains words shown in the vba array
below.

The array of words in vba code are as follows ("May",  "July",
"September")

I am looking for a solution where comments are added to each
occurrence of a word in the array. The comment would need to be
conditional. eg. For June the comment should have the text "Form 24
filing", For July, "IT Return Filing", for September, "Quarter
ending".

Thanks in advance for the help.

Regards,
Raj


Maybe something like this:

Sub ScratchMacro()
Dim myArray(2, 1) As Variant
Dim i As Long
Dim oRng As Word.Range
For i = 0 To 2
myArray(i, 0) = Choose(i + 1, "May", "July", "September")
myArray(i, 1) = Choose(i + 1, "For 24 Filing", "IT Return filing",
"Quarter ending")
Next i
Set oRng = ActiveDocument.Range
For i = 0 To 2
With oRng.Find
MsgBox myArray(i, 0)
.Text = myArray(i, 0)
.Wrap = wdFindStop
While .Execute
oRng.Comments.Add oRng, myArray(i, 1)
Wend
End With
Set oRng = ActiveDocument.Range
Next i
End Sub
 
R

Raj

Maybe something like this:

Sub ScratchMacro()
Dim myArray(2, 1) As Variant
Dim i As Long
Dim oRng As Word.Range
For i = 0 To 2
  myArray(i, 0) = Choose(i + 1, "May", "July", "September")
  myArray(i, 1) = Choose(i + 1, "For 24 Filing", "IT Return filing",
"Quarter ending")
Next i
Set oRng = ActiveDocument.Range
For i = 0 To 2
  With oRng.Find
    MsgBox myArray(i, 0)
    .Text = myArray(i, 0)
    .Wrap = wdFindStop
    While .Execute
      oRng.Comments.Add oRng, myArray(i, 1)
    Wend
  End With
  Set oRng = ActiveDocument.Range
Next i
End Sub- Hide quoted text -

- Show quoted text -

Thanks a ton, Greg. It worked exactly as I had in mind.

One small doubt I am asking off-hand: If instead of 3 elements, I
increase the number of elements in the array to 8, all I need to do
is to replace the 2 in the code above with 8 to make it work. Am I
right?

Regards,
Raj.
 
G

Gregory K. Maxey

Raj,

Your welcome.

No, for 8 items you would change the 2 to a 7. The first items in your list
is help in position 0. You would also of course have to add the other list
elemetns to both lines.



Maybe something like this:

Sub ScratchMacro()
Dim myArray(2, 1) As Variant
Dim i As Long
Dim oRng As Word.Range
For i = 0 To 2
myArray(i, 0) = Choose(i + 1, "May", "July", "September")
myArray(i, 1) = Choose(i + 1, "For 24 Filing", "IT Return filing",
"Quarter ending")
Next i
Set oRng = ActiveDocument.Range
For i = 0 To 2
With oRng.Find
MsgBox myArray(i, 0)
.Text = myArray(i, 0)
.Wrap = wdFindStop
While .Execute
oRng.Comments.Add oRng, myArray(i, 1)
Wend
End With
Set oRng = ActiveDocument.Range
Next i
End Sub- Hide quoted text -

- Show quoted text -

Thanks a ton, Greg. It worked exactly as I had in mind.

One small doubt I am asking off-hand: If instead of 3 elements, I
increase the number of elements in the array to 8, all I need to do
is to replace the 2 in the code above with 8 to make it work. Am I
right?

Regards,
Raj.
 

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