Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 9th, 2004, 04:27 PM
Registered User
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default replace fillin with ask fields & add references

Hello Everyone,

I am trying to write a procedure in Word 2000 to replace FILLIN fields with ASK fields.

My idea is to find each instance of of a FILLIN field, and replace the text, "FILLIN" with "ASK b1". The next text "FILLIN" I want to replace with "ASK b2", etc.

The code I wrote is replacing the text on every other instance. Does anyone know why this is?

Public Sub instance()

Public Sub tryit()


   Dim rngOriginalSelection As Word.Range
   Dim colFoundItems As New Collection
   Dim rngCurrent As Word.Range
   Dim strSearchFor As String
   Dim intFindCounter As Integer


   Set rngOriginalSelection = Selection.Range
   strSearchFor = "FILLIN"


   With Selection.Find
      .ClearFormatting
      .Forward = True
      .Wrap = wdFindContinue
      .Text = strSearchFor
      .Execute
      Do While .Found = True
         intFindCounter = intFindCounter + 1
         colFoundItems.Add Selection.Range, CStr(intFindCounter)
         .Execute
      Loop
   End With

   rngOriginalSelection.Select

   If MsgBox("There are " & intFindCounter & " instances of '" _
         & rngOriginalSelection & "' in this document." & vbCrLf & vbCrLf _
         & "Would you like to loop through and display all instances?", _
         vbYesNo) = vbYes Then
      intFindCounter = 0
      For Each rngCurrent In colFoundItems

        With Selection.Find

          intFindCounter = intFindCounter + 1

        .Replacement.Text = "ASK b " & intFindCounter + 1

         rngCurrent.Select
         MsgBox "This is instance #" & intFindCounter

' intFindCounter = intFindCounter + 1
         End With
         Selection.Find.Execute Replace:=wdReplaceOne

      Next
   End If

   rngOriginalSelection.Select


End Sub




 
Old December 15th, 2004, 06:12 AM
Registered User
 
Join Date: Feb 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this

Code:
Dim rngOriginalSelection     As Word.Range
   Dim colFoundItems            As New Collection
   Dim rngCurrent               As Word.Range
   Dim strSearchFor             As String
   Dim intFindCounter           As Integer

   
   Set rngOriginalSelection = Selection.Range
   strSearchFor = "FILLIN"
   
   
   With Selection.Find
      .ClearFormatting
      .Forward = True
      .Wrap = wdFindContinue
      .Text = strSearchFor
      .Execute
      Do While .Found = True
         intFindCounter = intFindCounter + 1
         colFoundItems.Add Selection.Range, CStr(intFindCounter)
         .Execute
      Loop
   End With
   
   rngOriginalSelection.Select
   
   If MsgBox("There are " & intFindCounter & " instances of '" _
         & rngOriginalSelection & "' in this document." & vbCrLf & vbCrLf _
         & "Would you like to loop through and display all instances?", _
         vbYesNo) = vbYes Then
      intFindCounter = 0
      For Each rngCurrent In colFoundItems

        With Selection.Find

          intFindCounter = intFindCounter + 1

        .Replacement.Text = "ASK b " & intFindCounter + 1
         
         rngCurrent.Select
         MsgBox "This is instance #" & intFindCounter
       
'         intFindCounter = intFindCounter + 1
         End With

         Selection.Find.Execute "FILLIN", , , , , , , 1, , "Ask " & intFindCounter
         
      Next
   End If
   
   rngOriginalSelection.Select
Madhivanan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Find&Replace DoctorWho ASP.NET 1.0 and 1.1 Professional 3 June 6th, 2007 09:40 AM
Global Search&Replace Text in all fields in sql db buddyz SQL Server 2000 10 September 14th, 2006 08:24 AM
Error Message from References to Add-ins [email protected] Excel VBA 0 August 7th, 2006 04:27 PM
How to replace pivot table data fields using VBA? rstober Excel VBA 2 August 19th, 2005 06:06 AM
trace tabel & replace the same civa BOOK: Beginning Access VBA 0 January 21st, 2005 01:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.