Finding and Deleting Text in MS Word

S

scorpion53061

Hi all MS Word app need to work with 2000 and up. This is vb.net code
adapted from vba. I am hoping that you folks have suggestions.

I am in a situation where I need to find a block of text and delete it from
the document - but only find the next instance of that text and stop there.
The code below worked fine except

1. my boss wants to be able to do a text deletion that was larger than was
allowed. How can I meet his requst?

2. and it went through to the bottom of the document deleting all entries
instead of stopping at the first one. When I switched to wdFindStop it gave
me out of range errors.

I suspect I am overlooking obvious solutions.

thank you for any help you give.....

selectedtext is a string variable.

selectedtext = Dstemplate1.Tables(0).Rows(0).Item("templateContent")
oDoc.Content.Application.Selection.Bookmarks.Add("startingpoint",
oDoc.Content.Application.Selection)

While
oDoc.Content.Application.Selection.Find.Execute(FindText:=selectedtext, _
Wrap:=oDoc.Content.Find.Wrap = 1) 'Word.WdFindWrap.wdFindContinue)

oDoc.Content.Application.Selection.Find.Execute(FindText:=selectedtext, _
ReplaceWith:=" ", _

Replace:=oDoc.Content.Application.Selection.Find.Replacement.Text = " ", _
Wrap:=oDoc.Content.Application.Selection.Find.Wrap = 1)
'WdFindWrap.wdFindContinue)
oDoc.Content.Application.Selection.Delete()
End While

oDoc.Content.Application.ActiveDocument.Bookmarks.Item("startingpoint").Sele
ct()

oDoc.Content.Application.ActiveDocument.Bookmarks.Item("startingpoint").Dele
te()
 
S

scorpion53061

solution found thanks anyway......


scorpion53061 said:
Hi all MS Word app need to work with 2000 and up. This is vb.net code
adapted from vba. I am hoping that you folks have suggestions.

I am in a situation where I need to find a block of text and delete it from
the document - but only find the next instance of that text and stop there.
The code below worked fine except

1. my boss wants to be able to do a text deletion that was larger than was
allowed. How can I meet his requst?

2. and it went through to the bottom of the document deleting all entries
instead of stopping at the first one. When I switched to wdFindStop it gave
me out of range errors.

I suspect I am overlooking obvious solutions.

thank you for any help you give.....

selectedtext is a string variable.

selectedtext = Dstemplate1.Tables(0).Rows(0).Item("templateContent")
oDoc.Content.Application.Selection.Bookmarks.Add("startingpoint",
oDoc.Content.Application.Selection)

While
oDoc.Content.Application.Selection.Find.Execute(FindText:=selectedtext, _
Wrap:=oDoc.Content.Find.Wrap = 1) 'Word.WdFindWrap.wdFindContinue)

oDoc.Content.Application.Selection.Find.Execute(FindText:=selectedtext, _
ReplaceWith:=" ", _

Replace:=oDoc.Content.Application.Selection.Find.Replacement.Text = " ", _
Wrap:=oDoc.Content.Application.Selection.Find.Wrap = 1)
'WdFindWrap.wdFindContinue)
oDoc.Content.Application.Selection.Delete()
End While

oDoc.Content.Application.ActiveDocument.Bookmarks.Item("startingpoint").Sele
oDoc.Content.Application.ActiveDocument.Bookmarks.Item("startingpoint").Dele
te()
 

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