Thankyou so much :)
Yes your code worked.
& Yes, I had to update with your given function.
There are some (400+ files) Rich Text Format (.rtf) files which also need to have a watermark. As MS word by default opens Rich Text Format (.rtf) files in ms word so, I want to ask you there is a way for macro to do this with the code you provided:
Option Explicit
Dim FSO As Object, oFolder As Object, StrFolds As String
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
'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
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 strFldr As String, strFile As String, wdDoc As Document
Dim Rng As Range, Sctn As Section, HdFt As HeaderFooter, Shp As Shape
strFldr = oFolder
If strFldr = "" Then Exit Sub
strFile = Dir(strFldr & "\*.doc", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFldr & "" & strFile, AddToRecentFiles:=False, ReadOnly:=False, Visible:=False)
With wdDoc
'Loop through all headers
For Each Sctn In .Sections
For Each HdFt In Sctn.Headers
With HdFt
'Watermark headers in use
If .Exists = True Then
If Sctn.Index = 1 Then
Set Rng = .Range
Rng.Collapse wdCollapseStart
InsertMyBuildingBlock "CONFIDENTIAL 1", Rng
ElseIf .LinkToPrevious = False Then
Set Rng = .Range
Rng.Collapse wdCollapseStart
InsertMyBuildingBlock "CONFIDENTIAL 1", Rng
End If
End If
End With
Next
Next
'Save and close the document
.Close SaveChanges:=wdSaveChanges
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
End Sub
Function InsertMyBuildingBlock(BuildingBlockName As String, HeaderRange As Range)
Dim oTemplate As Template, oAddin As AddIn, bFound As Boolean, i As Long
bFound = False
Templates.LoadBuildingBlocks
For Each oTemplate In Templates
If InStr(1, oTemplate.Name, "Building Blocks") > 0 Then
For i = 1 To Templates(oTemplate.FullName).BuildingBlockEntries.Count
If Templates(oTemplate.FullName).BuildingBlockEntries(i).Name = BuildingBlockName Then
Templates(oTemplate.FullName).BuildingBlockEntries(BuildingBlockName).Insert Where:=HeaderRange, RichText:=True
'set the found flag to true
bFound = True
'Clean up and stop looking
Set oTemplate = Nothing
Exit For
End If
Next i
Exit For
End If
Next
If bFound = False Then 'so tell the user.
MsgBox "Entry not found", vbInformation, "Building Block " & Chr(145) & BuildingBlockName & Chr(146)
End If
End Function