PDA

View Full Version : msgbox&find



raraschek
04-16-2008, 02:37 PM
any help appreciated_
i need an application:
1, to find any text where there is "-"
2, display this found text in the msgbox
3, ask user if he wants to delete "-" from the found text
4, and continue.

thankyou very much:rotlaugh:

Tinbendr
04-17-2008, 01:05 PM
Sub FindRemoveDash()
Dim aDoc As Document
Dim AllRng As Range
Dim SrchRng As Range
Dim Response As Integer
Set aDoc = ActiveDocument
Set AllRng = aDoc.Range
Set SrchRng = AllRng.Duplicate
Do
With SrchRng.Find
.ClearFormatting
.Text = "-"
.Execute
End With
If SrchRng.Find.Found Then
'Expand the find to include the word.
SrchRng.MoveStart wdWord, -1
SrchRng.MoveEnd wdWord, 1
'Ask for response
Response = MsgBox("Remove the dash? " & vbCr & _
SrchRng, vbYesNo, "Please Respond")
Select Case Response
Case 6
'They clicked yes.
SrchRng.Text = Replace(SrchRng.Text, "-", " ")
' or "-", "" if you want to close the gap.

End Select

SrchRng.Start = SrchRng.End + 1
SrchRng.End = AllRng.End
End If
Loop Until Not SrchRng.Find.Found
End Sub

raraschek
04-18-2008, 04:04 AM
thanx!!!

fumei
04-22-2008, 12:01 PM
A shorter alternative. There is no real need for the Response integer, nor the Document object.

Sub AlternativeDashRemoval()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(Findtext:="-", Forward:=True) = True
With r
.MoveStart wdWord, -1
.MoveEnd wdWord, 1
End With
If MsgBox("Remove the dash? " & vbCr & _
r.Text, vbYesNo, "Please Respond") = vbYes Then
r.Text = Replace(r.Text, "-", " ")
End If
r.Collapse 0
Loop
End With
End Sub

fumei
04-22-2008, 05:06 PM
I would like to add something regarding the use of constants. In Tinbendr's code, there is:


SrchRng.MoveStart wdWord, -1
SrchRng.MoveEnd wdWord, 1

and this is perfectly legitmate. However, the full syntax is:
SrchRng.MoveStart Unit:=wdWord, Count:=-1
SrchRng.MoveEnd Unit:=wdWord, Count:=1

Absolutely you can use the numeric constants instead. For people who are starting, I would strongly suggest that you use the full syntax though, and for both. Which is actually my comment. Tinbendr's code uses partial full syntax.

wdWord v.s. Unit:=wdWord

Again, there is nothing wrong with this. VBA is forgiving that way. My point is that one should use numerics where you want, as long as you know what they are, and what they are for.

Technically, Tinbendr's lines could be:

SrchRng.MoveStart 2, -1
SrchRng.MoveEnd 2, 1

as the numeric constant for wdWord = 2

Just as my:

r.Collapse 0



in full syntax is:

r.Collapse Direction:=wdCollapseEnd

They are equivalent.

For interest sake...if anyone is interested...attached is a document listing all constants, and their values, in Word (up to 2002).

fumei
04-22-2008, 05:14 PM
Actually, Tinbendr, I am curious as to why you use Duplicate for this. Further, if you do, in fact, want to use Duplicate (although I do not really know why), you can set the range object directly, like:
Set SrchRng = ActiveDocument.Range.Duplicate
You only need (and, I might add, only use) one object. SrchRng.

So why declare, and Set, those other objects, as you do not really need, or use, them?

Set aDoc = ActiveDocument
Set AllRng = aDoc.Range
Set SrchRng = AllRng.Duplicate

is the same as:

Set SrchRng = ActiveDocument.Range.Duplicate

Tinbendr
04-23-2008, 08:57 AM
Actually, Tinbendr, I am curious as to why... I find that often times, people ask a simple question about a complex macro. If I give the most compact solution, they routinely come back and ask how to incorporate the code I suggested into the larger macro.

I give expanded code to delineate the objects so people can understand and possibly learn how objects/ranges work together.

Our help is not a contest to see who can write the most efficient code. (At least, it's not to me.)

fumei
04-23-2008, 10:10 AM
Oh, well sorry, I was not intending to make anything into any sort of contest. That is not my intention either. Although I do feel that it is always an intention (for anyone) to write efficient code.

You do not answer the question though. I am curious as to why you use Duplicate. Your response does not seem to deal with that. I am not being critical, I am simply curious as to why you use Duplicate.

But...whatever.

Tinbendr
04-23-2008, 12:41 PM
I am curious as to why you use Duplicate.No particular reason.

It's from a book I use frequently. I find the routine useful for several types of search operations I use. I just adapted it to the request, not considering the overhead.

fumei
04-24-2008, 12:15 PM
"No particular reason."

I see. Please, believe me, I am NOT trying to be argumentative, or critical. I am just pointing out that declaring things that do NOT add usefulness seems...well, pointless. I try to only add/use things that DO have a particular reason.

From Help:

By duplicating a Range object, you can change the starting or ending character position of the duplicate range without changing the original range.

Duplicate, in this case, does NOT add any usefulness whatsoever. The original Range object (Set r = ActiveDocument.Range ) can handle the required changes quite nicely.

You are quite correct to state that this forum is not a contest for the most efficient code. However, it IS a place to aim for the most efficient code, as all code, IMO, should aim to be as efficient as possible. I am simply pointing out that:

Duplicate does not add anything useful.
The Document object does not add anything useful.

You have:

Dim aDoc As Document
Dim AllRng As Range
Dim SrchRng As Range
Dim Response As Integer


I have:

Dim r As Range

....as the ONE range object is all you need. The others are not needed, nor does using them add anything useful, in this case.

Yes, Duplicate will work. However, it is not needed to do what needs to get done. I guess it is a matter of philosophical outlook. If I do not need something....why declare and set something that I do not need?