Format text between two words (1 Viewer)

Capitala

Member
Local time
Today, 22:05
Joined
Oct 21, 2021
Messages
73
Thanks for your usual support!
Now, I have a specific and distinct word (say: CAPSHO). I's use this word at the beginning of a text block AND and the end of that block.
I need a code that searches for that CAPSHO and make specific format between the two words, then removes first and last (CAPSHO), and afterwards, searching the next one till the end of document.
Ex. Hi, CAPSHO I'm writing to you regarding CAPSHO
Sought Result: Hi, I'm writing to you regarding
Thanks & Best Regards
 
Use Instr() to get start and end positions for your text.
Remove them all at the end with Replace().
 
Most of the code to find text and format it is in here. The only issue you need to add is deleting the "tags" and using the tags to determine the formatting to apply. That should be pretty simple.

If you can provide a database it would be easy to demo.
Just FYI, this question was posted in the MS Word forum.
 
Just FYI, this question was posted in the MS Word forum.
Ah, I missed that completely. :(
However both those functions appear to be valid in Word?

Then there is this. https://learn.microsoft.com/en-us/office/vba/api/word.replacement

Then chatgpt offers (not tested, that is up to you) and best to have a different end tag as mentioned by @MajP
Code:
Sub FindTextBetweenWords()
    Dim doc As Document
    Dim rng As Range
    Dim searchTerm As String
    
    Set doc = ActiveDocument
    Set rng = doc.Content
    
    ' Change these words as needed
    Dim startWord As String: startWord = "StartWord"
    Dim endWord As String: endWord = "EndWord"
    
    ' Set the search pattern with wildcards
    searchTerm = startWord & "*" & endWord
    
    With rng.Find
        .ClearFormatting
        .Text = searchTerm
        .MatchWildcards = True
        .Forward = True
        .Wrap = wdFindStop
        
        Do While .Execute
            MsgBox "Found: " & rng.Text
            ' Move the start of the range to after the found text for next search
            rng.Collapse Direction:=wdCollapseEnd
        Loop
    End With
End Sub
 
Last edited:
1. Hi, it seems my description is ambiguous.
2. Here's more ....
3. Hi, CAPSHO I'm writing this message CAPSHO on behalf or Mr. xyz. This matter is CAPSHO significant issue for our organization CAPSHO.
4. The result should be like that:
5. Hi, I'm writing this message on behalf or Mr. xyz. This matter is significant issue for our organization .
6. Needed format is Bold and Underline format. as indicated above (point 5). The code should remove the keyword CAPSHO after formatting.
Sorry for any inconvenience
 
Code:
Dim strText As String
strText = "Hi, CAPSHO I'm writing this message CAPSHO on behalf or Mr. xyz. This matter is CAPSHO significant issue for our organization CAPSHO."
strText = Replace(strText, "CAPSHO", "")
 
Code:
Dim strText As String
strText = "Hi, CAPSHO I'm writing this message CAPSHO on behalf or Mr. xyz. This matter is CAPSHO significant issue for our organization CAPSHO."
strText = Replace(strText, "CAPSHO", "")
I think the OP also wants to format the parts between the keywords, like bold and underline them.
 
I edited my post to state I'd only done the replace part ...but clearly forgot to submit
 
If you had a different word for start and end, you could do this with a single line of code
 

Users who are viewing this thread

Back
Top Bottom