Re: Advanced find/replace in code

Tech-Archive recommends: Speed Up your PC by fixing your registry



If you wanted to delete the tags and format the text in between them, you should have said that was what you wanted to do.

This will do that:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="<\B22>", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set myrange = Selection.Range
myrange.End = ActiveDocument.Range.End
myrange.End = myrange.Start + InStr(myrange, "</B22>") + 5
myrange.Select
myrange.Text = Mid(myrange.Text, 7, Len(myrange.Text) - 12)
myrange.Font.Bold = True
Loop
End With



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
"Jeff Hunt" <JeffHunt@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:7DADC449-A1A2-463F-9171-8F1690280A4F@xxxxxxxxxxxxxxxx
I did put an action there (.font.bold). See my previous post, inside the
parentheses. When I F8 stepped thru the code, I never saw it select anything
except the opening field code (<\B22>), which isn't the part that is supposed
to be changed.

After several days and at least as many reboots, the weirdness from the code
that Pesach Shelnitz posted seems to have gone away, and it is working
consistently now. If you figure out why your code doesn't seem to work for
me, I would be interested in knowing for learning's sake, but since I have
code that works now, you don't need to spend the time unless you want to.
Thanks for your input on my problem!
--
...jeff...
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


"Doug Robbins - Word MVP" wrote:

That was to be expected. You must insert some code to do whatever it is
that you want to do with the text that is selected in place of the

'Do something with myrange

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
"Jeff Hunt" <JeffHunt@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:A5B4FF2E-F7FF-484A-B432-E31290E0D7B7@xxxxxxxxxxxxxxxx
> Hmm, that didn't appear to do anything. When I run it on the document, > it
> does its thing without changing anything (I put a .font.bold = true > where
> you
> had the 'Do something with myrange), and ends up with the cursor placed
> right
> after the last occurance of <\B22>. I stepped through the code, and it > is
> looping correctly, and progressing down the document, but all that it > is
> doing is highlighting the opening field code (<\B22>) and never > selecting
> anything between the codes. Unfortunately, I am not familar enough > with
> ranges to diagnose what it is supposed to be doing other than that.
>
> Thanks.
> -- > ...jeff...
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
>
> "Doug Robbins - Word MVP" wrote:
>
>> Use
>>
>> Dim myrange As Range
>> Selection.HomeKey wdStory
>> Selection.Find.ClearFormatting
>> With Selection.Find
>> Do While .Execute(FindText:="<\B22>", Forward:=True, _
>> MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = >> True
>> Set myrange = Selection.Range
>> myrange.End = ActiveDocument.Range.End
>> myrange.End = myrange.Start + InStr(myrange, "</B22>") - 1
>> myrange.Start = myrange.Start + 6
>> 'myrange will now contain all of the text between <\B22> and
>> </B22>
>> 'Do something with myrange
>> Selection.Collapse wdCollapseEnd
>> Loop
>> End With
>>
>>
>> -- >> Hope this helps.
>>
>> Please reply to the newsgroup unless you wish to avail yourself of my
>> services on a paid consulting basis.
>>
>> Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
>> "Jeff Hunt" <JeffHunt@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
>> news:89CCC085-3C44-474C-B493-A6BEE089B1DF@xxxxxxxxxxxxxxxx
>> >I had already determined that using wildcard searches wasn't going to
>> >cut
>> >it
>> > for what I'm doing. Part of it is because some of the text I need >> > to
>> > find
>> > and format can be paragraphs long, including punctuation. This >> > makes
>> > the
>> > "find" section rather difficult to write, even with wildcards. I >> > was
>> > also
>> > having difficulty applying formatting, because some of what I wanted >> > it
>> > to
>> > do
>> > was apply borders and shading, which I could not get to work >> > properly
>> > in
>> > the
>> > Find/Replace dialog (I even tried making them Styles, which still
>> > didn't
>> > quite work).
>> >
>> > Having said all that, I do want to thank you for the reply, and not
>> > just
>> > for
>> > the effort in making the post. The site you posted is definitely >> > going
>> > to
>> > help me in other aspects of what I do, so thank you very much for >> > that!
>> >
>> > -- >> > ...jeff...
>> > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>> >
>> >
>> > "Jay Freedman" wrote:
>> >
>> >> Jeff Hunt wrote:
>> >> > I would like to scan a document until a certain sequence of
>> >> > characters is found (ie, <\B22>) then select from there until
>> >> > another
>> >> > sequence (</B22>) is found, and then apply formatting changes to >> >> > any
>> >> > characters in between. I tried it with find/replace, but >> >> > couldn't
>> >> > apply all the formatting changes I wanted correctly to the >> >> > "between"
>> >> > characters. Plus, I need to do this for multiple characters with
>> >> > different formatting, so doing it in code will speed that up. >> >> > Any
>> >> > ideas on the best way to do this? FYI, I am proficient at VBA in
>> >> > Access and Excel, but have never coded for Word, so I'm >> >> > unfamiliar
>> >> > with its functions. Oh, and it's Word 2003.
>> >> >
>> >> > Thanks!
>> >>
>> >> You can do this with wildcard searches. See
>> >> http://www.gmayor.com/replace_using_wildcards.htm.
>> >>
>> >> If you get started and then get stuck, post back in this thread >> >> with a
>> >> detailed description of what's going wrong, and include whatever >> >> code
>> >> you
>> >> have so far.
>> >>
>> >> -- >> >> Regards,
>> >> Jay Freedman
>> >> Microsoft Word MVP FAQ: http://word.mvps.org
>> >> Email cannot be acknowledged; please post all follow-ups to the
>> >> newsgroup
>> >> so
>> >> all may benefit.
>> >>
>> >>
>> >>
>>
>>



.



Relevant Pages

  • Re: Page numbering in multiple documents
    ... Doug Robbins - Word MVP, ... documents were based on different templates, the formatting gets messed up ... following Heading 1, similarly Heading 2 and Heading 3, etc. ...
    (microsoft.public.word.numbering)
  • Re: Stopping paste text from adding quotation marks?
    ... What was the formatting that was removed? ... Doug Robbins - Word MVP, ... Set rgeToPaste = mobjDocNew.Bookmarks.Range ...
    (microsoft.public.word.vba.beginners)
  • Re: How do I print a mail merge w/o the blank pages?
    ... Have a look at the formatting of the first paragraph in the Letter. ... > "Doug Robbins - Word MVP" wrote: ... >> directly to me will only be answered on a paid consulting basis. ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Advanced find/replace in code
    ... Doug Robbins - Word MVP, ... had the 'Do something with myrange), and ends up with the cursor placed ... having difficulty applying formatting, because some of what I wanted it ... characters in between. ...
    (microsoft.public.word.vba.general)
  • Re: Unable to merge table ?
    ... Have you checked the formatting of the paragraphs. ... Doug Robbins - Word MVP ...
    (microsoft.public.word.tables)