#1
|
|||
|
|||
Macro for multiple RTF files
Hi,
Can anyone help me writing macro for changing margins from 2,5 to 1,2 (left, right. top and bottom) and to delete last 5 lines of text and save it (in multiple files at once)? I have to format like so over 900 .rtf files and I can't find any code which would work for me. Is it possible to do so or i have to open each file, run macro, save file and so on with each next one? I'm totally green if it comes to macros so I would appriceate any help. (everytime i try codes posted on this forum there are errors and none is working, so i'm creating new thread) Last edited by papapaleo; 07-28-2017 at 02:56 AM. Reason: explenation |
#2
|
||||
|
||||
'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 |
Tags |
macro multiple rtf |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Run a macro on multiple docx. files | Peter Carter | Word VBA | 27 | 12-15-2022 04:10 PM |
Macro to Pull Data From Multiple Files | Jess709 | Excel Programming | 30 | 05-20-2015 05:26 PM |
Macro for Combining Multiple Word Files Together Into One Document | rsrasc | Word VBA | 2 | 10-31-2014 07:58 AM |
looking for macro for multiple files | bolk | Word | 3 | 05-03-2011 05:46 AM |
macro to pull data from multiple files | psrs0810 | Excel | 2 | 10-25-2010 01:49 PM |