Little VBA problem

J

Jasmin Bajric

Hi together,
I have a little VBA problem but can not solve it -- perhaps you have an
idea?

The problem:
I have a long txt-file in which I want to search for a specific word and
want to replace it with an other one. The simple search/replace command does
not work, because the specific word should be replaced *only* if it occurs
in specific case.

Here one part of the long txt-file:
---snipp
\subsubsection*{-ed and -ing}
Si quaedam nimis antique, si peraque dure dicere credit eos, ignave multa
fatetur, et sapit et mecum facit et Iova iudicat aequo. Non equidem insector
delendave carmina Livi esse reor, memini quae plagosum mihi parvo Orbilium
dictare; sed emendata videri pulchraque et exactis minimum distantia miror.

\subsubsection*{-fuls and -ful}
Si quaedam nimis antique, si peraque dure dicere credit eos, ignave multa
fatetur, et sapit et mecum facit et Iova iudicat aequo. Non equidem insector
delendave carmina Livi esse reor, memini quae plagosum mihi parvo Orbilium
dictare; sed emendata videri pulchraque et exactis minimum distantia miror.

\subsubsection*{-ic}
Si quaedam nimis antique, si peraque dure dicere credit eos, ignave multa
fatetur, et sapit et mecum facit et Iova iudicat aequo. Non equidem insector
delendave carmina Livi esse reor, memini quae plagosum mihi parvo Orbilium
dictare; sed emendata videri pulchraque et exactis minimum distantia miror.

\subsubsection*{-wise}
Si quaedam nimis antique, si peraque dure dicere credit eos, ignave multa
fatetur, et sapit et mecum facit et Iova iudicat aequo. Non equidem insector
delendave carmina Livi esse reor, memini quae plagosum mihi parvo Orbilium
dictare; sed emendata videri pulchraque et exactis minimum distantia miror.
---snipp

The VB-program:
The program should search the complete file for the expression
"\subsubsection*{".
If this expression is found, it should search in the same line for the word
"and" and should replace it with an other word (like "blabla").
It is important to see, that the "and" is only to be replaced, if it occurs
in the very same line as the expression"\subsection*{" - not in the
following text lines.

Do you think this is possible with VBA? Do you have an idea?
Thanks a lot in advance
Regards
Jasmin
 
D

Doug Robbins - Word MVP

Hi Jasmin,

Use:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="\subsubsection*{", MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range.Bookmarks("\line").Range
If InStr(myrange, "and") Then
myrange.Start = myrange.Start + InStr(myrange, "and") - 1
myrange.End = myrange.Start + 3
myrange.Text = "blabla"
Selection.Collapse wdCollapseEnd
End If
Loop
End With


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Jasmin Bajric

[...]
Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="\subsubsection*{", MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Set myrange = Selection.Range.Bookmarks("\line").Range
If InStr(myrange, "and") Then
myrange.Start = myrange.Start + InStr(myrange, "and") - 1
myrange.End = myrange.Start + 3
myrange.Text = "blabla"
Selection.Collapse wdCollapseEnd
End If
Loop
End With
[...]

Great!
Short, elegant and effective coding!
Thanks so much for this -- it helped me a lot: One can learn also a much
from this kind of coding.

Thanks and have a nice day!
Best regards
Jasmin
 
Top