Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS-WORD VBA Question 1

Status
Not open for further replies.

MyFlight

Technical User
Feb 4, 2002
193
Does anyone know of a Simple way to run a FIND-REPLACE
under an IF-THEN statement?

Example:

Selection.Find.ClearFormatting
With Selection.Find
.Text = _
"UNIT 1A IS NOT SET UP"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1


I need to do an Replace on the above text If-Found, Multiple times (the 1A changes upto 40A).

thank you
 
Not totally sure I am following, but if what you are trying to do is delete all text:

"UNIT 1A IS NOT SET UP"

"UNIT 2A IS NOT SET UP"

"UNIT 3A IS NOT SET UP"

"UNIT 4A IS NOT SET UP"

etc. etc.

Code:
Sub TestReplace()
Dim var
var = 1
Selection.HomeKey Unit:=wdStory
With Selection.Find
    Do While (.Execute(Findtext:="UNIT " & var & _
        "A IS NOT SET UP", _
         Forward:=True) = True) = True
      Selection.Delete
      var = var + 1
    Loop
End With
End Sub
would delete all instances of "UNIT xA IS NOT SET UP"

Note: it would delete "1A" once, then delete 2A once, 3A once etc.

It would have to be modified to only run 1 to 40. As it is, it keeps incrementing var until it can not find an example of varA.

Gerry
My paintings and sculpture
 
fumei,

Excellent One Further question how can I manipulate this to work with other Find-replace Macros with Different Text?

Example

H2: LTU 1. 2 IS NOT SET UP

H2: LTU 1. 3 IS NOT SET UP

H2: LTU 1. 4 IS NOT SET UP

I Really Appreciate you assistance.
 
Ummm, use different text?

The point is the only thing differnt between "UNIT 1A", and "UNIT 2A" is the number. This is covered with the use of a variable - var.

It starts at 1, and increments. So:

H2: LTU 1. 2 IS NOT SET UP

with the bolded number incrementing is no different (mostly...Exception see below) - from

UNIT 3A IS NOT SET UP

with the bolded number incrementing.

In both cases, you are replacing the number with an incrementing variable (var). So to use a different text, put IN different text.

H2: LTU 1. x IS NOT SET UP

Findtext:="H2: LTU 1. " & var & _
" IS NOT SET UP"

To expand on this, and if it is worth it (you have lots of these), AND the text "IS NOT SET UP" is constant, you could do this:
Code:
Sub ReplaceStuff()
Dim var
Dim strIn As String
strIn = InputBox("Please input starting text.")
var = Inputbox("{Please input starting number.")
Selection.HomeKey Unit:=wdStory
With Selection.Find
    Do While (.Execute(Findtext:=strIn & var & _
        "A IS NOT SET UP", _
         Forward:=True) = True) = True
      Selection.Delete
      var = var + 1
    Loop
End With
End Sub

Now this would work for:

UNIT xA IS NOT SET UP
Yadda yadda xA IS NOT SET UP
Whatever xA IS NOT SET UP

You would type in "UNIT ", then the starting number (I am assuming it could NOT always be 1). So UNIT 4A (with 4 as the starting number) could be done.

EXCEPTION: this would NOT work for:

H2: LTU 1. x IS NOT SET UP because the text string is different.

A) - variable (in bold) followed by:
"A IS NOT SET UP" (eg. UNIT 1A IS NOT SET UP)
The "A" is hard coded.


B) - variable (in bold) followed by:
"IS SET UP" (eg. H2: LTU 1. 4 IS NOT SET UP)
There is no hard coded "A"

With a bit more fussing, just about any combination of text and variable could be done. However, it is only worth bothering if this is a constant task, and you have a lot.



Gerry
My paintings and sculpture
 
Just to clarify. You COULD have a starting string, a starting variable, and a following string.
Code:
Sub ReplaceStuff2()
Dim var
Dim strInStart As String
Dim strInEnd As String
strIn = InputBox("Please input starting text.")
var = Inputbox("{Please input starting number.")
strEnd = Inputbox("Please input ending text.")
Selection.HomeKey Unit:=wdStory
With Selection.Find
    Do While (.Execute(Findtext:=strInStart & var & _
        strInEnd, Forward:=True) = True) = True
      Selection.Delete
      var = var + 1
    Loop
End With
End Sub
Now you could enter:

"Unit ", 1, "A IS NOT SET UP"

"Unit ", 1, " IS NOT SET UP" - note no "A"

"H2: LTU 1. ", 1, " IS NOT SET UP"

"Zippy do-dah ", 3891, " whatever...have a nice day"

The only caveat I would suggest is note, and be careful to include, your spaces!

"UNIT", 1, "A IS NOT SET UP" would return a search for:

UNIT1A IS NOT SET UP

"H2: LTU 1.", 2, "IS NOT SET UP" would return as search for:

H2: LTU 1.2IS NOT SET UP

And a final note.

If these are always single line paragraphs (and I have no idea if they are), like this:

H2: LTU 1. 2 IS NOT SET UP

So nothing following "IS NOT SET UP",

AND they always end with "IS NOT SET UP", then there is a much easier way to do this.
Code:
Sub DumpCrap1()
Dim oPara As Word.Paragraph
Dim strDump As String
strDump = "IS NOT SET UP"
For Each oPara In ActiveDocument.Paragraphs
   If Left(Right(oPara.Range.Text, 14), 13) = strDump Then
      oPara.Range.Delete
   End If
Next
End Sub
This deletes ALL paragraphs ending with "IS NOT SET UP". INCLUDING the paragraph mark!!! Important!

If you want to keep the paragraph mark, but have it "empty" ie. blank, then you would use:
Code:
Sub DumpCrap2()
Dim oPara As Word.Paragraph
Dim strDump As String
strDump = "IS NOT SET UP"
For Each oPara In ActiveDocument.Paragraphs
   If Left(Right(oPara.Range.Text, 14), 13) = strDump Then
      oPara.Range.Text = "" & vbCrLf
   End If
Next
End Sub
This replaces ALL paragraphs ending with "IS NOT SET UP", with "", or no text. The paragraph itself is retained.

The beauty of this is there is no need for any search routine at all. There is a check done on each paragraph. If it ends with "IS NOT SET UP", then action is taken.

Bada bing, badda-boom.

Are we having fun yet?

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top