1 Replies - 603 Views - Last Post: 03 July 2008 - 09:36 PM Rate Topic: -----

#1 HowdeeDoodee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 17-June 08

Do Loop Counter And Replacer Not Working At Beginning Or End

Posted 03 July 2008 - 07:04 PM

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
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.

Is This A Good Question/Topic? 0
  • +

Replies To: Do Loop Counter And Replacer Not Working At Beginning Or End

#2 HowdeeDoodee  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 17-June 08

Re: Do Loop Counter And Replacer Not Working At Beginning Or End

Posted 03 July 2008 - 09:36 PM

OK, here we go. Here is how to get it done.
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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1