Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and replace text-problem...

Status
Not open for further replies.

nesplb

Programmer
Jul 29, 2003
109
NO
Hi!
I want to search for some text...
The text can be on the format
Code:
<td>1.2</td>, <td>10.2</td>, 
<td>1.1.2</td> or <td>10.1.2</td>

-Then I want to replace the text with this:
Code:
<td><a href = &quot;chap[The first number in  the text that was found].html#[The whole number in the text that was found]&quot;>[The whole number in the text found]</a></td>
Result may be like this:

the text that was found:
Code:
<td>1.2.3</td>
the replaced text:
Code:
<td><a href = &quot;chap1.html#1.2.3&quot;>1.2.3</a></td>

I have tried using the find function but I think the conditions are too complex....

Can anyone help me..?

Thanks



Pål Nesteby

PDC-Tangen
Norway
 
The ansver is....

For those who are of interest, I found this solution for all the numbers of the format [numer.number]
For the other numbers it's just to adjust the 3rd if sentence, the &quot;ord5&quot;-variable and the selection.find.text- variable


Code:
Dim MyWord As Range
Dim ord
Dim ord2
Dim ord3
Dim ord4
Dim ord5
Dim ord6


Dim MyWord2 As Range

For Each MyWord In ActiveDocument.Words
 
    If MyWord.Text = &quot;<&quot; Then
     
      If MyWord.Next(wdWord, 1).Text = &quot;td&quot; Then
  
        ord = MyWord.Next(wdWord, 3).Text
        ord2 = MyWord.Next(wdWord, 4).Text
        ord3 = MyWord.Next(wdWord, 5).Text
        ord4 = MyWord.Next(wdWord, 6).Text
        ord6 = MyWord.Next(wdWord, 7).Text
                
        
        If IsNumeric(ord) And Len(ord) = 1 And ord2 = &quot;.&quot; And ord4 = &quot;</&quot; Then
      
        
       
     ord5 = &quot;<td><a href = 'chap0&quot; + ord + &quot;.hmtl#&quot; + ord + ord2 + ord3 + &quot;'>&quot; + ord + ord2 + ord3 + &quot;</a></td>&quot;
        
        
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = &quot;<td>&quot; + ord + &quot;.&quot; + ord3 + &quot;</td>&quot;
        .Replacement.Text = ord5
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    
        With Selection
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseStart
        Else
            .Collapse Direction:=wdCollapseEnd
        End If
        .Find.Execute Replace:=wdReplaceOne
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseEnd
        Else
            .Collapse Direction:=wdCollapseStart
        End If
        .Find.Execute
    End With 
        
    End If    
        
    End If
  
End If
    
   Next MyWord

Pål Nesteby

PDC-Tangen
Norway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top