PDA

View Full Version : Solved: How do I delete text imediately after find function



Mike100
01-08-2009, 06:17 AM
Hoping someone can help me out here. Im reasonably familar with Excel VBA, but only just started Word VBA today. Im hopelessly stuck!

I need to remove all occurrences of any text between brackets (including the brackets) immediately following the text ?[OPTIONS]? in the active document, if an associated 'Options_checkbox' is not ticked


For example: If the associated checkbox was not ticked, then I would want to remove the following text from the sample text below "<sample text in here>". The text between the brackets differs through-out documents, so I cannot just do a search and replace on the text "<sample text in here>

Sample text

the cat sat on the mat
[OPTIONS]<sample text in here>

the dog chased the cat


Thanks in advance, Mike

Mike100
01-08-2009, 09:42 AM
I found some usefull code in the forums that solved my first problem, with some slight modifications to my layout.

I now delete all text between brackets (including the brackets) immediately following the text 'OPTION'

Sample text :

the cat sat on the mat
[OPTION sample text in here]
the dog chased the cat


I now need to be able to detect if there is a carriage return immediately after the text in brackets, and if there is, delete it. Otherwise a blank line is left where the text is deleted.

My code is below.



Sub Macro()
Dim MyRange As Range
Set MyRange = ActiveDocument.Range
If Option_CheckBox = False Then
Do While MyRange.Find.Execute(findtext:="[OPTION", Forward:=True) = True
MyRange.MoveEndUntil cset:="]"
MyRange.MoveEnd Unit:=wdCharacter, Count:=1
MyRange.Delete
Loop
End If
End Sub


Hope one of you guys can help me out here.

Edit Lucas: VBA tags added to code. Mike, if you select your code when posting and hit the vba button your code will be formatted for the forum.

lucas
01-08-2009, 10:02 AM
Hi Mike,
Can you just look for the strings < and > and delete those and everything between them?

Option Explicit

Sub delStrings(sText1 As String, sText2 As String)
Dim r As Range
Dim j As Long
Dim k As Long

Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
Do While .Execute(Findtext:=sText1, _
Forward:=True) = True
j = r.End
r.Expand unit:=wdSentence
With r.Find
If .Execute(Findtext:=sText2, _
Forward:=True) = True Then
k = r.Start
' MsgBox ActiveDocument.Range(Start:=j - 1, _
' End:=k + 1).Text
ActiveDocument.Range(Start:=j - 1, _
End:=k + 1).Delete
End If
End With
r.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub

Sub CalldelStrings()
Call delStrings("<", ">")
End Sub

Paul_Hossler
01-08-2009, 01:44 PM
Wildcards will help also



Sub Macro1()

Application.ScreenUpdating = False

With ActiveDocument.Range.Find

'replace paragraph + left-bracket + OPTION + anything + right-bracket + paragraph
' with space
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^13\[OPTION *\]^13"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub


Paul

Mike100
01-12-2009, 04:40 AM
Thanks for the help guys, thats given me a few good ideas.

Paul_Hossler
01-17-2009, 07:45 AM
Don't forget to mark your thread "Solved" if you're done

Thread Tools at the top

Paul