Counter for macro

A

andreas

Dear Experts:

below macro copies all parantheses at the end of the current document.
To spruce this very handy macro up, I would like a counter integrated,
that tells me via a MsgBox ...
.... how many parenthetical expressions have been copied or in the case
none were found
.... no parentheses found in current document

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

Sub CopyParentheticalExpressions()

Dim str As String
str = ""
ActiveWindow.View.ShowFieldCodes = True

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="\(*\)", MatchWildcards:=True,
Forward:=True, Wrap:=wdFindStop) = True

ActiveWindow.View.ShowFieldCodes = True
str = str & Selection.range.Text & vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
ActiveDocument.range.InsertAfter str

ActiveWindow.View.ShowFieldCodes = False

End Sub
 
G

Greg Maxey

Rather than continue to hand you finished code, may I suggest that you show
what you have tried that doesn't work?
 
F

Fumei2 via OfficeKB.com

I agree. If you need a counter for each iteration, then...add a counter. if
you have problems with making it work, show us the code that is the problem.

Greg said:
Rather than continue to hand you finished code, may I suggest that you show
what you have tried that doesn't work?
Dear Experts:
[quoted text clipped - 31 lines]
 
A

andreas

I agree.  If you need a counter for each iteration, then...add a counter.  if
you have problems with making it work, show us the code that is the problem.

Greg said:
Rather than continue to hand you finished code, may I suggest that you show
what you have tried that doesn't work?
[quoted text clipped - 31 lines]

Hi Greg & Fumei,

thank you very much for your swift response.

I did some more trials and came up with my own solutions. It works.
Thank you for taking your time to look into my matter.

Regards, Andreas

Sub CopyParentheticalExpressions()

Dim str As String
Dim intCounter as integer
str = ""
ActiveWindow.View.ShowFieldCodes = True
intCounter = 0
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="\(*\)", MatchWildcards:=True,
Forward:=True, Wrap:=wdFindStop) = True

ActiveWindow.View.ShowFieldCodes = True
str = str & Selection.range.Text & vbCr
Selection.Collapse wdCollapseEnd
intCounter = intCounter + 1
Loop
End With
if intCounter = 0 then
msgbox "None were found"
Else
msgbox "Number found: " & intcounter
end if
ActiveDocument.range.InsertAfter str

ActiveWindow.View.ShowFieldCodes = False

End Sub
 

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