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 / December 2007

Tip: Looking for answers? Try searching our database.

Search and replace within a selection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Wirt - 30 Dec 2007 21:31 GMT
I want to write a procedure that will search through a flat text file to
find fields with a certain fieldname (i.e., "KE[ ") , extend the
election to the entire field (i.e., until the next vbCRLF), and then
replace all commas in that selection with semi-colons.

A basic procedure that will do this is shown below, but how can I
replace text within the selected field? Set the current selection to a
selection object and then search and replace in that object?

Public Sub ReplaceSelwSemiColons()
Dim FoundField as Selection

Selection.HomeKey Unit:=wdStory 'go to beginning of document
Do
  With Selection.Find
    .Text = "KE[ " 'search for next field named KE[^
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = True
  End With
  Selection.Find.Execute
  If Selection.Find.Found = True Then
    Selection.EndOf Unit:=wdParagraph, Extend:=wdExtend
      'if KE[ is found, extend the selection to the entire field

      {now, what goes in here to replace all commas with
       a semicolon?}
      Set FoundField=Selection
      {Then what.....?

  End If
  Selection.Collapse Direction:=wdCollapseEnd
Loop 'loop back to find next instance of the field

End Sub

John Wirt
Helmut Weber - 30 Dec 2007 23:20 GMT
answered in ... vba.general

Rate this thread:






 
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.