Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Edit macro to loop through word doc

    Not exactly sure how to build a loop into this macro.

    I have a word document with many Table's and I need to look for an X in the table name and replace with sequential numerical numbers.

    From
    Table 3.X.
    Table 3.X.
    Table 3.X.

    To
    Table 3.1.
    Table 3.2.
    Table 3.3.

    Until all Table's 3.X. are updated...

    This macro will replace the first Table 3.X., but how to loop? Any thoughts?

    Code:
    Sub EditFindLoopExample()
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "Table 3.X."
            .Replacement.Text = "Table 3.1."
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        With Selection
            If .Find.Forward = True Then
                .Collapse Direction:=wdCollapseStart
            Else
                .Collapse Direction:=wdCollapseEnd
            End If
            .Find.Execute Replace:=wdReplaceOne
            If .Find.Forward = True Then
                .Collapse Direction:=wdCollapseEnd
            Else
                .Collapse Direction:=wdCollapseStart
            End If
            .Find.Execute
        End With
    End Sub

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    And why aren't you using sequence fields to do this numbering automatically? If you did so, you could replace with the same thing in every instance and the numbers would increment by themselves. You wouldn't need a macro either if you didn't want to repeat it.

    To do this, create a field by pressing Ctrl-F9 and type the following so it looks like (and the brackets are created by the Ctrl-F9)
    {Seq Table}

    Now cut this field to your clipboard and change the replace line to
    .Replacement.Text = "Table 3.^c."

    Now do the replace all and follow it with a select all (Ctrl-A) and update fields (F9)
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    jrb (2011-11-03)

  4. #3
    2 Star Lounger
    Join Date
    Aug 2011
    Posts
    112
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Thanks Andrew, I was unaware about the seq fields but this works great.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •