PDA

View Full Version : Find text in frame, then delete frame?



clhare
05-14-2009, 06:59 AM
I have a document with several frames in it. The frames were added to the document using the "Insert Frame" button on the Forms toolbar.

I need to delete a particular frame. I'm thinking I have to first search for some text that is only in that frame so I can get to that frame in particular, but I can't figure out how to select that frame once I've found the text and then delete it.

Any suggestions would be greatly appreciated!

clhare
05-14-2009, 07:16 AM
I just saw the post re finding out if the selected text is in a bookmark and tried adjusting macropod's code to see if I could check for a frame instead. It worked!

Here's what I ended up with:

Sub FindFrameAndDelete()
Dim oFrame As Frame
' Search for text
Selection.Find.ClearFormatting
With Selection.Find
.Text = "frame text"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
End With
Selection.Find.Execute

' Test to see if found text is in a frame
With ActiveDocument
For Each oFrame In .Frames
If Selection.Range.Start <= oFrame.Range.End _
And Selection.Range.End >= oFrame.Range.Start Then _
' If insertion point is in a frame, delete the frame
oFrame.Select
Selection.Delete Unit:=wdCharacter, Count:=1
End If
Next
End With

End Sub

If there's a better way to do this, please let me know.

lucas
05-14-2009, 11:47 AM
Not necessarily better but another method:
Option Explicit
Sub RemoveBoxIfItSaysInstructions()
Dim aFrame As Frame
For Each aFrame In ActiveDocument.Frames
If InStr(1, aFrame.Range, "frame text", vbTextCompare) _
> 0 Then aFrame.Range.Delete
aFrame.Delete
Next
End Sub