View Single Post
 
Old 07-28-2017, 04:56 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

'Line' is a vague concept in Word. There are no 'lines' in a Word document. Assuming that you mean 'paragraphs' i.e. each 'line' is terminated with the normally hidden paragraph character ¶, then the following should work (assuming that the margin measurement is in centimeters, which seems probable). I strongly urge you to test this on a folder with only a couple of files, and if that works, run the macro on copies of your files.

As each document must be opened in order to edit it, then saved and closed, the process is going to take a while to run.

Code:
Sub BatchProcessRTF()
'Graham Mayor - http://www.gmayor.com - Last updated - 28 Jul 2017
Dim oDoc As Document
Dim oRng As Range
Dim strPath As String
Dim strFile As String
Dim iCount As Integer
Dim fDialog As FileDialog
    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With fDialog
        .Title = "Select folder to process and click OK"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then
            MsgBox "Cancelled By User", , "List Folder Contents"
            GoTo lbl_Exit
        End If
        strPath = fDialog.SelectedItems.Item(1)
        If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
    End With
    If Documents.Count > 0 Then
        Documents.Close SaveChanges:=wdPromptToSaveChanges
    End If
    strFile = Dir$(strPath & "*.rtf")
    While strFile <> ""
        Set oDoc = Documents.Open(FileName:=strPath & strFile, AddToRecentFiles:=False)
        With oDoc
            Set oRng = .Range
            iCount = oRng.Paragraphs.Count
            If iCount > 5 Then
                With .PageSetup
                    .TopMargin = 34
                    .BottomMargin = 34
                    .LeftMargin = 34
                    .RightMargin = 34
                End With
                oRng.Collapse 0
                oRng.Start = oDoc.Range.Paragraphs(iCount - 5).Range.Start
                oRng.Text = ""
            End If
            .Close SaveChanges:=wdSaveChanges
        End With
        DoEvents
        strFile = Dir$()
    Wend
    MsgBox "Processing complete"
lbl_Exit:
    Set oDoc = Nothing
    Set oRng = Nothing
    Set fDialog = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote