Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post

    Paragraph first and Last Word - Not Inserting Correctly

    Hi folks,

    Good Saturday to all


    I am trying to change the first and last word of each paragraph - ie inserting it,can some one please have a look at what is wrong with below code.

    I simply want to insert as first word LR643 and the last word should be END for each paragraph.


    Code:
    Sub InsertFirstAndLastWord()
    
    Dim ParaFirstWord As String
    
    Dim LastWord As String
    
        Dim oPara As Word.Paragraph
    
        oParaFirstWord = "LR643"
        oParaLastWord = "END"
    
        For Each oPara In ActiveDocument.Paragraphs
    
        oPara.Range.Words(1).Text = oParaFirstWord
        oPara.Range.Words.Last = oParaLastWord
        
       
        
    Next
    
    
    End Sub
    It does not insert for the second paragraph and brings it up to the previous. Am I meant to collapse something

    thanks for your time

    pb

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    You could use:
    Code:
    Sub InsertFirstAndLastWord()
    Const StrFirst As String = "LR643 "
    Const StrLast As String = " END"
    Dim oPara As Word.Paragraph
    For Each oPara In ActiveDocument.Paragraphs
      With oPara.Range
        .InsertBefore StrFirst
        .Characters.Last.InsertBefore StrLast
      End With
    Next
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

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

    pb89 (2016-02-20)

  4. #3
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post

    Smile

    Hi Paul,

    that worked a treat

    .Characters.Last.InsertBefore StrLast

    thanks and cheers

    pb

Posting Permissions

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