Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2004

Tip: Looking for answers? Try searching our database.

Little VBA problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jasmin Bajric - 30 May 2004 11:35 GMT
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
Doug Robbins - Word MVP - 30 May 2004 13:58 GMT
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

Signature

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

> Hi together,
> I have a little VBA problem but can not solve it -- perhaps you have an
[quoted text clipped - 46 lines]
> Regards
> Jasmin
Jasmin Bajric - 31 May 2004 11:33 GMT
"Doug Robbins - Word MVP" wrote
[...]
> Dim myrange As Range
> Selection.HomeKey wdStory
[quoted text clipped - 11 lines]
>     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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.