Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:07:55+00:00 2026-06-13T16:07:55+00:00

I am trying to write a vba word macro that searches all open documents,

  • 0

I am trying to write a vba word macro that searches all open documents, finds all occurrences of the text “DocumentEnd9999”, and deletes everything below that text in each document.

Sub deletion()

Dim endTerm As String
endTerm = "DocumentEnd9999"

'Loop Dim
Dim n, c As Integer
n = Application.Documents.Count

For c = 1 To n
    Set myRange = Application.Documents(c).StoryRanges
    For Each myRange In ActiveDocument.StoryRanges
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = endTerm
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
        End With
        Selection.Find.Execute
        Selection.Extend
        Selection.Find.ClearFormatting
        With myRange.Find
            myRange.Characters.Last.Select
            .Forward = True
            .Wrap = wdFindAsk
        End With
        Application.DisplayAlerts = False
        Selection.Find.Execute
        Selection.Delete
    Next myRange
Next c

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:07:57+00:00Added an answer on June 13, 2026 at 4:07 pm

    The following code should do what you’re looking for in the main body of the document. Not sure exactly why you’re using StoryRanges. I’m not too familiar with that collection, so I didn’t include it.

    Sub deletion()
    
        Dim endTerm As String
        endTerm = "DocumentEnd9999"
    
        Dim n, c As Integer
        n = Application.Documents.Count
        c = 1
    
        Dim r As Range
    
        Windows(c).Activate
    
        Selection.Find.ClearFormatting
            With Selection.Find
                .Text = endTerm
                .Forward = True
                .Wrap = wdFindContinue
            End With
    
        Do
    
            Selection.Find.Execute    
    
            Set r = ActiveDocument.Range(Selection.Range.Start, ActiveDocument.Content.End)
            r.Delete
    
            c = c + 1
    
            On Error Resume Next
            Windows(c).Activate
    
        Loop Until c > n
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a VBA macro in Word that will extract shapes and
I'm trying to write a VBA script that finds all embedded (.docx) files within
I'm trying to write an Excel macro using VBA to automate importing CSV text
I am trying to write a VBA scriptin Microsoft Access that will interface with
I'm trying to write a VBA function that restricts its inputs. It takes three
I am trying to write an excel macro/VBA code to adjust a column's contents
Trying to write app for service technicians that will display open service calls within
I am trying to write a VBA script which imports all of the Excel
I'm trying to use Excel VBA to write to a text file. I'm doing
I'm trying to make a macro in VBA for Delmia. That macro need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.