none
How to insert a SHIFT ENTER at the end of the line in word using VBA and also macro should skip if it find ENTER at the end of the line

Answers

  • The following macro works on the document content you provided. No guarantees that it will work in a document that contains tables or other types of content than just text.

    Sub NewLineEndEveryLine()
        Dim rngDocStart As word.Range
        Dim rngDocEnd As word.Range
        
        Set rngDocStart = ActiveDocument.Content
        rngDocStart.Collapse wdCollapseStart
        Set rngDocEnd = ActiveDocument.Content
        rngDocEnd.Collapse wdCollapseEnd
        'Set Selection at the beginning of the document
        rngDocStart.Select
        'Move Selection to the end of the line
        Selection.EndKey Unit:=wdLine
        'As long as we're not at the end of the document
        Do Until Selection.Range Is rngDocEnd
            'If the character at the end of the line is
            'NOT a new paragraph mark...
            If Selection.Text <> Chr(13) Then
                'Type a new line
                Selection.TypeText Chr(11)
            End If
            'Move the Selection to the next line
            If Selection.MoveStart(wdCharacter, 1) <> 0 Then
                'If the move was successful, to to the end of the line
                Selection.EndKey Unit:=wdLine
            Else
                'Otherwise exit the loop
                Exit Do
            End If
        Loop
    End Sub


    Cindy Meister, VSTO/Word MVP, my blog


    Sunday, June 23, 2013 3:15 PM

All replies

  • Could you please provide more information?

    1. Version of Word

    2. The purpose of the needing to do this

    3. The start of the question basically says: do this at the end of the line where the selection is; the end of the question seems to indicate you want to do this throughout the document. Which is correct?


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, June 22, 2013 10:12 AM
  • Hi

    Thanks for your response.

    1. Version of word is 2007

    2. I have some data in word document which needs to be given shift enter at the end of line.

    3. I need this macro for entire document because if i  just record the macro to insert "SHIFT ENTER", macro is not working and it is just getting struck at the end of paragraph because there is already a ENTER.

    Regards,

    Hari

    Sunday, June 23, 2013 10:34 AM
  • Hi Hari

    Thank you for the additional details. I have one more question, just to make sure we don't spend a lot of time looking at code for the wrong thing...

    This data is already in the document? And it's already breaking to the next line in the right place? How is it doing that if there's no Enter or Shift+Enter at the end of these lines?

    Or is your macro also writing the data into the document and you need to know how to put a Shift+Enter?


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, June 23, 2013 10:50 AM
  • Hi,

    Data is already in word and i have explained in below example

    original data.

    As a young boy, I was afraid to go beyond the end of my street in Richmond,
    London; I’d often make it as far as the big red post box, and no further. I fondly
    remember long hot summers playing in the local park, and I remember the first time
    that I camped out with the local Boy Scouts; for the first time I was away from my
    parents. I stayed up late, I woke early, and I watched the dawn rise for the first time,
    over a still and silent forest

    i want the format like

    1. At the end of each line "Shift Enter"(There is no Enter or Shift Enter already since only space exists)

    2. At the End of paragraph a enter will be there automatically no need to change anything.

    Thanks for your response again if you need additional information i will try to provide based on my understanding.

    Regards,

    Hari


    Sunday, June 23, 2013 12:04 PM
  • The following macro works on the document content you provided. No guarantees that it will work in a document that contains tables or other types of content than just text.

    Sub NewLineEndEveryLine()
        Dim rngDocStart As word.Range
        Dim rngDocEnd As word.Range
        
        Set rngDocStart = ActiveDocument.Content
        rngDocStart.Collapse wdCollapseStart
        Set rngDocEnd = ActiveDocument.Content
        rngDocEnd.Collapse wdCollapseEnd
        'Set Selection at the beginning of the document
        rngDocStart.Select
        'Move Selection to the end of the line
        Selection.EndKey Unit:=wdLine
        'As long as we're not at the end of the document
        Do Until Selection.Range Is rngDocEnd
            'If the character at the end of the line is
            'NOT a new paragraph mark...
            If Selection.Text <> Chr(13) Then
                'Type a new line
                Selection.TypeText Chr(11)
            End If
            'Move the Selection to the next line
            If Selection.MoveStart(wdCharacter, 1) <> 0 Then
                'If the move was successful, to to the end of the line
                Selection.EndKey Unit:=wdLine
            Else
                'Otherwise exit the loop
                Exit Do
            End If
        Loop
    End Sub


    Cindy Meister, VSTO/Word MVP, my blog


    Sunday, June 23, 2013 3:15 PM
  • Hi Cindy,

    Thank you so much and this is how i need it exactly to run the macro

    If possible can u please make a small change in macro.

    1. If at the end of the every line already a "SHIFT ENTER" is available then macro skips that particular line it will be a great help for me.

    Thank you once again,

    Regards

    Hari.


    Monday, June 24, 2013 6:22 AM
  • Sub SplitCellText()
        Dim rngCell As word.Range
        Dim rngSel As word.Range
        Dim rngBeforeSel As word.Range
        Dim rngAfterSel As word.Range
        
        Set rngSel = Selection.Range
        If rngSel.Information(wdWithInTable) Then
            Set rngCell = Selection.Range.Cells(1).Range
            Set rngBeforeSel = rngCell.Duplicate
            rngBeforeSel.End = rngSel.Start
            Set rngAfterSel = rngCell.Duplicate
            rngAfterSel.Start = rngSel.Start
            rngAfterSel.End = rngCell.End - 1
            Debug.Print rngAfterSel.Text
        End If
    End Sub
    
    Sub NewLineEndEveryLine()
        Dim rngDocStart As word.Range
        Dim rngDocEnd As word.Range
        
        Set rngDocStart = ActiveDocument.Content
        rngDocStart.Collapse wdCollapseStart
        Set rngDocEnd = ActiveDocument.Content
        rngDocEnd.Collapse wdCollapseEnd
        'Set Selectionection at the beginning of the document
        rngDocStart.Select
        'Move Selectionection to the end of the line
        Selection.EndKey Unit:=wdLine
        'As long as we're not at the end of the document
        Do Until Selection.Range Is rngDocEnd
            'If the character at the end of the line is
            'NOT a new paragraph mark...
            If Selection.Text <> Chr(13) And Selection.Text <> Chr(11) Then
                'Type a new line
                Selection.TypeText Chr(11)
            End If
            'Move the Selectionection to the next line
            If Selection.MoveStart(wdCharacter, 1) <> 0 Then
                'If the move was successful, to to the end of the line
                Selection.EndKey Unit:=wdLine
            Else
                'Otherwise exit the loop
                Exit Do
            End If
        Loop
    End Sub

    I'm happy to have been able to help you, but please note that this forum is NOT a place to get "free macros". The purpose of the forums on MSDN is to help developers write their own code. If you have no experience in writing macros the better place to get assistance is in the "Communities":
      http://answers.microsoft.com/en-us/office/forum/customize?tm=1372066605528

    Cindy Meister, VSTO/Word MVP, my blog

    Monday, June 24, 2013 9:37 AM
  • Hi,

    Thank you so much and I got inspired seeing your script and felt I should also learn how to write script for word and excel can u please suggest me some websites to learn.

    Regards,

    Hari

    Monday, June 24, 2013 11:38 AM
  • Hi Hari,

    This may give you a hand:

    Getting Started with VBA in Word 2010: http://msdn.microsoft.com/en-us/library/office/ff604039(v=office.14).aspx

    Thanks,


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Sunday, June 30, 2013 12:17 PM