Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-11-2013, 08:18 AM
DJSOUND DJSOUND is offline End or alter a loop? Windows 7 64bit End or alter a loop? Office 2010 64bit
Novice
End or alter a loop?
 
Join Date: Oct 2013
Posts: 1
DJSOUND is on a distinguished road
Default End or alter a loop?

Hi
I have this code below where I am looking for a certain character style, replacing the character two spaces preceding it with a comma (when text in the character style is "abc") and inserting a period immediately following the character style regardless of the text.


I have it working, but when I tried to do a loop it won't end. I've found that my method is probably not the correct one, but I can't figure out how to do it any other way. Is there a way to just tell the program to find an ending point that it will actually find?
Code:
Code:
Sub Macro9()
'
' Macro9 Macro
'
Do Until ActiveDocument.Bookmarks("\Sel") = ActiveDocument.Bookmarks("\EndOfDoc")
  Selection.Find.ClearFormatting
  Selection.Find.Style = ActiveDocument.Styles("one_car")
  Selection.Find.Replacement.ClearFormatting
  With Selection.Find
    .Text = "abc"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
   .MatchAllWordForms = False
  End With
  Selection.Find.Execute
  Selection.MoveLeft Unit:=wdCharacter, Count:=2
  Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
  Selection.TypeText Text:=","
  Selection.Find.ClearFormatting
  Selection.Find.Style = ActiveDocument.Styles("one_car")
  Selection.Find.Replacement.ClearFormatting
  With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  Selection.Find.Execute
  Selection.MoveRight Unit:=wdCharacter, Count:=1
  Selection.TypeText Text:="."
Loop
End Sub

Thanks!

Last edited by macropod; 10-11-2013 at 07:49 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 10-11-2013, 08:11 PM
macropod's Avatar
macropod macropod is offline End or alter a loop? Windows 7 32bit End or alter a loop? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
Set Rng = ActiveDocument.Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = True
    .Forward = True
    .Wrap = wdFindStop
    .Text = "abc"
    .Style = "one_car"
    .Replacement.Text = ""
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    .MoveStart Unit:=wdCharacter, Count:=-3
    .InsertBefore ","
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
  .Start = Rng.Start
  With .Find
    .Text = ""
    .Execute
  End With
  Do While .Find.Found
    .InsertAfter "."
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
or, even simpler:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Format = True
    .Forward = True
    .Wrap = wdFindStop
    .Text = "abc"
    .Style = "one_car"
    .Replacement.Text = ""
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    .MoveStart Unit:=wdCharacter, Count:=-3
    .InsertBefore ","
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
  With .Find
    .Text = ""
    .Replacement.Text = "^&."
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
End or alter a loop? How to a For loop in VBA Jennifer Murphy Word VBA 1 01-29-2013 03:30 AM
How to use for loop in formula in VBA? tinfanide Excel Programming 1 12-06-2011 08:33 AM
While loop not working right Cbrehm Excel Programming 0 05-11-2011 11:05 AM
End or alter a loop? Alter right click menu? markg2 Office 1 01-10-2011 08:10 AM
End or alter a loop? Continuous Loop cksm4 Word 6 01-06-2011 09:03 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:09 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft