# Solved: Looping Line by Line in Word Macro



## zeda (Jun 18, 2010)

I have Word 2003. I do a lot of literature reviews, copying and pasting from .pdf files. When I do this, the text is pasted into word only filling a partial page. I want the text to completely fill a line within the margins. So I manually go to the end of a sentence, hit the space bar once, the delete once to delete the new line/paragraph marker. Sometimes I have to do that twice. Then I go to the next sentence and repeat the steps until it is all completed. It seems this would be a good use of a macro. I can record a macro and go through these steps once but not looping through every line until the end. Can someone please help?

Zeda


----------



## zeda (Jun 18, 2010)

If it helps, here is an example of what the text looks like and what I want to achieve.

Thanks

Zeda


----------



## AD_Taylor (Jan 16, 2010)

Hi Zeda,

Give this a go:


```
Sub LoopingText()
'Will move to the end of each line in the document and move the text to match

 'Declare variables
Dim outputStr As String

 Dim currLine As String

 Dim endChar As String

 Dim numOfLines As Integer

 'Count the number of non blank lines in current document
numOfLines = ActiveDocument.BuiltInDocumentProperties("NUMBER OF LINES")

 'Move to start of document
Selection.HomeKey Unit:=wdStory

 'Start the loop - looping once for each line
For x1 = 1 To numOfLines

          'Move to start of line
        Selection.HomeKey Unit:=wdLine

                     'Select entire line and copy into variable currLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        currLine = Selection.Range.Text

                 'Remove final character (line break) from currLine
        currLine = Left(currLine, (Len(currLine) - 1))

         'Check to see if character currently on end of outputStr is "-" (dash)
        endChar = Right(outputStr, 1)

                 If endChar = "-" Then
        'If preceding line included a dash at the end

                                    'Add current line to the variable outputStr
            'Because of dash no space should be included
            outputStr = outputStr & currLine

                                      Else
        'If no dash on preceding line

                         If x1 = 1 Then

                             'Add the current line to the variable outputStr
                'Since this is the first run through don't add a space
                outputStr = outputStr & currLine

                             Else

                     'Add the current line to the variable outputStr
                'No dash on the preceding line so add a space as well
                outputStr = outputStr & " " & currLine

                     End If

                     End If

         'Move down one line
    Selection.MoveDown Unit:=wdLine, Count:=1

     'Move to the next part of the loop
Next x1

 'Finally 'paste' outputStr onto the document
ActiveDocument.Range = outputStr

 End Sub
```
 I've added comments to the code (any line preceded by an apostrophe) so hopefully you should be able to make sense of what each part is doing.

Instead of deleting the page breaks and adding spaces, I choose to go through the document and build a combined overall string based on each line.

Bear in mind if you want to do this for only part of the current document you'll need to copy it to a new document. Otherwise, the macro will run through the entire text of that document.

Hope this helps!
Adam

EDIT: Some of the indentation has gone a bit all over the place when I copied it in but the macro should still work absolutely fine.


----------



## zeda (Jun 18, 2010)

Amazing. That works so well. Thanks so much. You have saved me so much time in getting this work done.


----------



## Jande (Aug 3, 2011)

AD_Taylor said:


> Hi Zeda,
> 
> Give this a go:
> 
> ...


Could provide more help, this macro is awesome, but the main problem is that delete all lines before and after heading main text... I want it to delete lines between basic style(text) but keep lines between heading 1 and basic style. Is there any way to do that? I have a doc and when run macro, deletes all lines empty, even those where are between heading 1 and main text, and produces a document in style of heading 1. I want to delete lines only between main text of the document( aka between Heading 1 and Heading 2 or Heading 1 and Heading 1, until the end of document)? Is there any way that run the macro for specific section of a document?


----------

