![]() |
|
|
|
#1
|
||||
|
||||
|
Quote:
The following macro will process all Headers in all Sections of all Documents in both the chosen folder and its sub-folders. Code:
Option Explicit
Dim FSO As Object, oFolder As Object, StrFolds As String, wdDocSrc As Document, wdDocTgt As Document
Sub Main()
Application.ScreenUpdating = False
Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long
TopLevelFolder = GetFolder
If TopLevelFolder = "" Then Exit Sub
StrFolds = vbCr & TopLevelFolder
If FSO Is Nothing Then
Set FSO = CreateObject("Scripting.FileSystemObject")
End If
Set wdDocSrc = ActiveDocument
'Get the sub-folder structure
Set TheFolders = FSO.GetFolder(TopLevelFolder).SubFolders
For Each aFolder In TheFolders
RecurseWriteFolderName (aFolder)
Next
'Process the documents in each folder
For i = 1 To UBound(Split(StrFolds, vbCr))
Call UpdateDocuments(CStr(Split(StrFolds, vbCr)(i)))
Next
Set wdDocSrc = Nothing: Set wdDocTgt = Nothing
Application.ScreenUpdating = True
End Sub
Sub RecurseWriteFolderName(aFolder)
Dim SubFolders As Variant, SubFolder As Variant
Set SubFolders = FSO.GetFolder(aFolder).SubFolders
StrFolds = StrFolds & vbCr & CStr(aFolder)
On Error Resume Next
For Each SubFolder In SubFolders
RecurseWriteFolderName (SubFolder)
Next
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Sub UpdateDocuments(oFolder As String)
Dim strInFolder As String, strFile As String, Sctn As Section, HdFt As HeaderFooter
strInFolder = oFolder
strFile = Dir(strInFolder & "\*.doc", vbNormal)
While strFile <> ""
Set wdDocTgt = Documents.Open(FileName:=strInFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDocTgt
For Each Sctn In .Sections
For Each HdFt In Sctn.Headers
With HdFt
If .Exists Then .Range.Find.Execute FindText:="[^13]{3,}", ReplaceWith:="^p^p", MatchWildcards:=True, Replace:=wdReplaceAll
End With
Next
Next
'Save and close the document
.Close SaveChanges:=True
End With
strFile = Dir()
Wend
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#2
|
|||
|
|||
|
Mark and Paul thank you for your assistance in this matter.
It is greatly appreaciated. Interestingly, neither of the macros worked, initially. One, likely important facet I failed to mention (as I did not know it would make a difference) and is the likely culprit for the failure to work as desired was that the 3 paragraphs that I am attempting to replace with 2 are at the end of the header. However, both macros worked when I made substitutions (^p^p for ^p^p^p and ^p for ^p^p in Mark's code and "[^13]{2,}", ReplaceWith:="^p" in Paul's code). That is when I replaced two paragraph marks with one. However, that does not technically solve my initial problem as some of the documents may have only 2 paragraph marks in the header and if I use the adulterated code above it would mess those documents up. So what I should have asked from the beginning is: Is there a means to check for 3 paragraphs when they are the last 3 entries in a header? |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| a macro to replace paragraph mark with a space applies effect on paragraph marks after the selection | drrr | Word VBA | 2 | 08-24-2021 03:05 AM |
What is this paragraph mark called
|
booneyrex | Word | 8 | 03-04-2021 04:15 AM |
Indention below paragraph mark...
|
kikola | Word VBA | 13 | 05-26-2020 06:21 AM |
Please help with header and footer removal
|
pwangdel | Word | 3 | 11-03-2011 06:10 AM |
Final paragraph mark
|
Caroline | Word | 2 | 02-22-2011 10:39 AM |