Thread: [Solved] Macro Loop Help
View Single Post
 
Old 09-16-2015, 05:47 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
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

You say the employee # occurs twice on each page. The following macro will insert a page break before the first of each pair of employee #s. If the employee # isn't in the first paragraph for each payslip, a minor code adjustment can be made to accommodate that - you just have to tell us which paragraph on each page it's in. Until that adjustment is made, you'll probably also find the macro deleting the wrong first character in the document, too.
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "<[0-9]{7}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    i = (i + 1) Mod 2
    If i = 1 Then
      .Paragraphs.First.Range.InsertBefore Chr(12)
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
  .Characters.First.Delete
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote