|
-
Mar 5th, 2017, 12:09 PM
#1
Thread Starter
New Member
Running a macro with a for loop
Hello all,
I have several thousand Word files that have inconsistent formatting. I have a macro that runs on a single word file at a time. I would like to use VB code to use a for loop to run on all the Word files in the directory. The below is my macro.
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[^13]{2,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
For i = 1 To .Paragraphs.Count
With .Paragraphs(i)
If .Borders(wdBorderBottom).LineStyle <> wdLineStyleNone Then
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Range.InsertBefore "______________________" & _
"________________________________________" & _
"_________________________________________"
End If
End With
Next
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
-
Mar 5th, 2017, 03:36 PM
#2
Re: Running a macro with a for loop
you can use DIR to find all the word files in a folder, likr
Code:
mypath = "C:\temp\" 'change to your folder, note trailing \
fn = dir(mypath & "*.docx") ' change docx if required
do while len(fn) > 0
set doc = documents.open(mypath & fn)
with doc.range ' change this line from activedocument
' put the rest of your existing code in here for each document
doc.close true ' save changes
fn = dir ' get next file
loop
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 11th, 2017, 05:12 PM
#3
Thread Starter
New Member
Re: Running a macro with a for loop
So, I finally got to play with your code. It took me a second to put together what you meant with "change this line from activedocument". I figured, "A guy with THAT much rep cannot really be wrong with such a simple task".
Code:
Sub Demo()
mypath = "C:\Users\lonesoac0\Downloads\MedFiles\" 'change to your folder, note trailing \
fn = Dir(mypath & "*.docx") ' change docx if required
Do While Len(fn) > 0
Set doc = Documents.Open(mypath & fn)
Application.ScreenUpdating = False
Dim i As Long
With doc.Range ' change this line from activedocument
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[^13]{2,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
For i = 1 To .Paragraphs.Count
With .Paragraphs(i)
If .Borders(wdBorderBottom).LineStyle <> wdLineStyleNone Then
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Range.InsertBefore "______________________" & _
"________________________________________" & _
"_________________________________________"
End If
End With
Next
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
doc.Close True ' save changes
fn = Dir ' get next file
Loop
End Sub
-
Mar 11th, 2017, 05:47 PM
#4
Re: Running a macro with a for loop
A guy with THAT much rep cannot really be wrong
still easy to make error
probably no need to turn screen updating on and off, just turn off once at start, before loop
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|