How do I optimize iterating through large documents?

I’m trying to write a VBA macro in Microsoft Word that removes all the watermarks in the document. The first step is easy, I just need to remove all the front wrapped text. Sometimes, the watermarks are in text boxes that are in the top and bottom of the page and mixed in with other text, so I need to remove the repeating text while preserving the existing text. I wrote this macro which does the job, but takes multiple minutes to run on documents that are potentially over one hundred pages long.

Sub CompleteRemover()
Dim doc As Document
Dim shp As Shape
Dim shpRange As Range
Dim textDict As Object
Dim pageDict As Object
Dim key As Variant
Dim text As String
Dim pageCollection As Object
Dim shapesToProcess As Collection
Dim shpInfo As Variant
Dim i As Long
Dim pageCount As Long

' Create dictionary objects to store text counts and page occurrences
Set textDict = CreateObject("Scripting.Dictionary")
Set pageDict = CreateObject("Scripting.Dictionary")
Set shapesToProcess = New Collection

' Reference the active document
Set doc = ActiveDocument

' Disable screen updating to improve performance
Application.ScreenUpdating = False

' Get the total number of pages in the document
pageCount = doc.ComputeStatistics(wdStatisticPages)

' Loop through shapes in reverse order to avoid indexing issues when deleting shapes
For i = doc.Shapes.Count To 1 Step -1
    Set shp = doc.Shapes(i)
    On Error Resume Next

    ' Check for watermarks
    If shp.WrapFormat.Type = wdWrapFront Then
        shp.Delete
    End If

    ' Check for text boxes in headers/footers
    If shp.Type = msoTextBox And shp.TextFrame.HasText Then
        Set shpRange = shp.Anchor.Paragraphs(1).Range
        Dim pageIndex As Long
        pageIndex = shpRange.Information(wdActiveEndPageNumber)
        text = Left(shp.TextFrame.textRange.text, 255) ' Limit to 255 characters

        ' Store shape information in a collection
        shapesToProcess.Add Array(shp, text, pageIndex)

        If textDict.Exists(text) Then
            textDict(text) = textDict(text) + 1
            Set pageCollection = pageDict(text)
            If Not pageCollection.Exists(pageIndex) Then
                pageCollection.Add pageIndex, True
            End If
        Else
            textDict.Add text, 1
            Set pageCollection = CreateObject("Scripting.Dictionary")
            pageCollection.Add pageIndex, True
            pageDict.Add text, pageCollection
        End If
    End If
    On Error GoTo 0
Next i

' Second pass: Remove the watermark text from the textboxes with repeating text on every page
For Each key In textDict.Keys
    If pageDict(key).Count = pageCount Then
        For Each shpInfo In shapesToProcess
            Set shp = shpInfo(0)
            text = shpInfo(1)
            If InStr(text, key) > 0 Then
                With shp.TextFrame.textRange.Find
                    .text = key
                    .Replacement.text = ""
                    .Forward = True
                    .Wrap = wdFindStop
                    .Format = True
                    .MatchCase = False
                    .MatchWholeWord = False
                    .MatchWildcards = False
                    .MatchSoundsLike = False
                    .MatchAllWordForms = False
                    .Execute Replace:=wdReplaceAll
                End With
            End If
        Next shpInfo
    End If
Next key

' Re-enable screen updating
Application.ScreenUpdating = True

' Display completion message
MsgBox "Watermarks Removed."
End Sub

I tried limiting what the program checks, such as only checking the last two or three text boxes to see if they’re front wrapped, or only checking certain margins in the page for the watermarks, but it ended up breaking the code.

New contributor

Ttop133 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật