Results 1 to 13 of 13
  1. #1
    New Lounger
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word - For In Do Loop (Word 2003)

    Please consider the macro below. I'm stumped as to how to perform an initial total line count, then do the math (see notations). I'm then stumped how to code a proper For In Do lopp in VBA for the second section. Thanks!

    ' Macro should initially perform the following math:
    ' Total loop iterations = (total line count / 2) + 1
    '
    ' VariousArtists2 Macro
    ' Macro recorded 1/12/2006 by cmorton
    ' The initial series comprises up to 40 sets of two lines each.
    ' For each set, insert line 2 into line 1 just prior to the '.',
    ' then separate the org. line 1 and line 2 data with a {sp}-{sp}
    '
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Cut
    Selection.MoveUp Unit:=wdLine, Count:=1
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "."
    End With
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:=" "
    Selection.PasteAndFormat (wdPasteDefault)
    Selection.TypeBackspace
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.TypeBackspace
    Selection.TypeText Text:=" -"
    '
    ' End of first set manipulation
    ' Process remaining sets
    ' For In Do loop (using Total iterations var)
    '
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Cut
    Selection.MoveUp Unit:=wdLine, Count:=1
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "."
    End With
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:=" "
    Selection.PasteAndFormat (wdPasteDefault)
    Selection.TypeBackspace
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.TypeBackspace
    Selection.TypeText Text:=" -"
    '
    ' Next
    '
    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word - For In Do Loop (Word 2003)

    Try this:
    <code>
    Sub Replace2()
    With ActiveDocument.Content.Find
    .Text = "(*)(.^13)(*)(.^13)"
    .Replacement.Text = "1 - 3zzzzzz"
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    .Text = "zzzzzz"
    .Replacement.Text = "^13"
    .Execute Replace:=wdReplaceAll
    End With
    End Sub
    </code>
    The macro uses a Find/Replace with wildcards. The text <code>zzzzzz</code> is just an arbitrary string that doesn't occur in the document. It acts as temporary placeholder.

  3. #3
    New Lounger
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word - For In Do Loop (Word 2003)

    Sorry... I be corn-fused on this one!

    What is it your code does... count lines? I actually need to do a couple of things here. Keeping all of existing code intact (it's needed for other operations)...

    First I need to know how many times to loop through section 2 of my code. I arbitarily based this on the following variable data (which I don't know how to code: (total line count / 2) + 1.

    Then I need to loop through section 2 of the code N times based on the varibale value. I don't know how to express a FOR NEXT loop in VBA.

    Thanks

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word - For In Do Loop (Word 2003)

    The code I posted doesn't count anything, it just merges pairs of lines with " - " in between, since I thought that was what you wanted to do.

    To count lines first and then loop through them seems unnecessarily convoluted to me.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word - For In Do Loop (Word 2003)

    Have you tried the code I posted on (a copy of) your document?

  6. #6
    New Lounger
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word - For In Do Loop (Word 2003)

    Ok, great, BUT... Consider this example:

    WHAT I START WITH:

    4. Uncle Pen
    Ricky Skaggs

    5. Cabin On The Hill
    Flatt and Scruggs

    WHAT I WANT:

    4 Ricky Skaggs - Uncle Pen
    5 Flatt and Scruggs - Cabin On The Hill

    As you can see, each even line specifically gets inserted just before the "." in the preceding odd line. Then the "." is replaced by -{sp} (or is it {sp}-?). At any rate, I want each even line inserted at a very specific locale. Does your code do this?

  7. #7
    New Lounger
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word - For In Do Loop (Word 2003)

    I just tried your code.... it didn't do a thing to the example list I provided herein.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word - For In Do Loop (Word 2003)

    Does this do what you want?
    <code>
    Sub Replace2()
    With ActiveDocument.Content.Find
    .Text = "(*)(. )(*)(^13)(*)(^13^13)"
    .Replacement.Text = "1 5 - 36"
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    End With
    End Sub
    </code>
    There should be a blank line below the last pair.

  9. #9
    New Lounger
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word - For In Do Loop (Word 2003)

    I must be doing something wrong, Hans. First I copy your code to the Clipboard, then I paste it in the VBA editor. Some raw data is in a Word DOC. I then run your macro, but nothing, absolutely nothing, happens to the raw data. ????

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word - For In Do Loop (Word 2003)

    Could you attach a small sample document?

  11. #11
    New Lounger
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word - For In Do Loop (Word 2003)

    Sure. This is exactly how I'll get raw data, with extraneous spaces and all. Note how record #1 differs ever so slightly (no spaces before its number).

    And the end result should be like so:


    1 Peg Leg Sam - Early in the Morning
    2 Roosevelt Skyes - Music Is My Business
    ...and so forth.

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word - For In Do Loop (Word 2003)

    I have attached your document with a modified version of the macro. It cleans up the superfluous spaces before merging the lines.

  13. #13
    New Lounger
    Join Date
    Nov 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word - For In Do Loop (Word 2003)

    Great. I'll check it out in a moment...

    (a few moments later)

    A-ha! Perfect! Once again, you've proven

Posting Permissions

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