Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 117,617 VB Programmers for FREE! Ask your question and get quick answers from experts. There are 1,951 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Do Loop Counter And Replacer Not Working At Beginning Or End

 
Closed TopicStart new topic

Do Loop Counter And Replacer Not Working At Beginning Or End

HowdeeDoodee
post 3 Jul, 2008 - 07:04 PM
Post #1


D.I.C Head

**
Joined: 17 Jun, 2008
Posts: 64

I have built a small macro that is not working properly. I want the macro to replace any occurance of the word "Go" with an automatically generated number with an associated word "LineNumber: " When the macro is run, the first replacement action is off. Then, if the number of replacements to be made is less than the loop count in the macro, the loop keeps running making the end of the running not correct.

I need to eliminate the specific number in the loop counter and make the macro stop running after all of the available replacements have been made no matter how few or how many replacements are on the page.


QUOTE
Go
Gt
Go
Gt
Go
Gt
Go
Gt
Go
Gt
Go
Gt
Go
Gt


Result Produced By Macro

QUOTE

LineNumber: 1LineNumber: 2
Gp
Gp
LineNumber: 3
Gp
Gp
LineNumber: 4
Gp
Gp
LineNumber: 5
Gp
Gp
LineNumber: 6
Gp
Gp
LineNumber: 7
Gp
Gp
LineNumber: 8LineNumber: 9LineNumber: 10
Gp
Gp


Result Wanted By Macro

QUOTE

LineNumber: 1
Gp
Gp
LineNumber: 2
Gp
Gp
LineNumber: 3
Gp
Gp
LineNumber: 4
Gp
Gp
LineNumber: 5
Gp
Gp
LineNumber: 6
Gp
Gp
LineNumber: 7
Gp
Gp
LineNumber: 8
Gp
Gp


This page contains 14 replacements

QUOTE
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp


Result Produced By Macro

QUOTE

LineNumber: 1LineNumber: 2
Gp
Gp
LineNumber: 3
Gp
Gp
LineNumber: 4
Gp
Gp
LineNumber: 5
Gp
Gp
LineNumber: 6
Gp
Gp
LineNumber: 7
Gp
Gp
LineNumber: 8
Gp
Gp
LineNumber: 9
Gp
Gp
LineNumber: 10
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp
Go
Gp
Gp

Result Wanted By Macro
QUOTE
LineNumber: 1
Gp
Gp
LineNumber: 2
Gp
Gp
LineNumber: 3
Gp
Gp
LineNumber: 4
Gp
Gp
LineNumber: 5
Gp
Gp
LineNumber: 6
Gp
Gp
LineNumber: 7
Gp
Gp
LineNumber: 8
Gp
Gp
LineNumber: 9
Gp
Gp
LineNumber: 10
Gp
Gp
LineNumber: 11
Gp
Gp
LineNumber: 12
Gp
Gp
LineNumber: 13
Gp
Gp
LineNumber: 14
Gp
Gp

Here is the macro needing changes
CODE

Sub DoLoopDemo()
'
' DoLoopDemo Macro
' Macro created 7/3/2008
'
'Loop: Do...Loop Demo:
   'Selection.TypeText Text:="LineNumber: "
   'Selection.TypeText Text:=intCounter
   Do While intCounter < 10
   intCounter = intCounter + 1
  
  Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "Go"
        .Replacement.Text = intCounter
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        Selection.TypeText Text:="LineNumber: "
                CycleCount = CycleCount + 1
        Selection.TypeText Text:=CycleCount
    
      End With
  Selection.Find.Execute
         Loop

End Sub

Thank you in advance for any replies.
User is offlineProfile CardPM

Go to the top of the page


HowdeeDoodee
post 3 Jul, 2008 - 09:36 PM
Post #2


D.I.C Head

**
Joined: 17 Jun, 2008
Posts: 64

OK, here we go. Here is how to get it done.
CODE

Sub ReplaceDemo()
'
' ReplaceDemo Macro
' Macro recorded 7/3/2008 by Patrick Kelly
'
  Do 'While intCounter < 10
   intCounter = intCounter + 1
  
     Selection.Find.Replacement.ClearFormatting
  With Selection.Find
        .Text = "Go"
        .Replacement.Text = "LineNumber: "
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    If Not Selection.Find.Execute Then Exit Do
    Selection.Find.Execute Replace:=wdReplaceOne
    Selection.TypeText Text:="LineNumber: "
    Selection.TypeText Text:=intCounter
'This is the trouble line...If Not Selection.Find.Execute Then Exit Do
'If intCounter > 20 Then Exit Do
Loop

End Sub
User is offlineProfile CardPM

Go to the top of the page

Closed TopicStart new topic
Time is now: 10/8/08 12:02AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month