Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-30-2023, 08:56 AM
syl3786 syl3786 is offline Word macro - How to add bookmark between specified text? Windows 10 Word macro - How to add bookmark between specified text? Office 2019
Advanced Beginner
Word macro - How to add bookmark between specified text?
 
Join Date: Jan 2023
Posts: 68
syl3786 is on a distinguished road
Default Word macro - How to add bookmark between specified text?

I found a Word Macro from the Internet to find text, select it and then add as bookmark. I am trying to modify to fit for my application. I wish the Macro can find and select the text like follows:

The paragraphs I want the Macro to select (From the word "CEO" to the last word "together"):

CEO: Today is the first day......

Over the years......

We will work in a.....together.


Then select the above paragraphs and add as bookmark.

How to edit the Macro code to do that? Your help would be greatly appreciated.

Sub addbkmark()

Dim myRange As Range
Set myRange = ActiveDocument.Content

With myRange.Find
Do While .Execute(FindText:="XXX", MatchCase:=True) 'Find the text



myRange.InsertParagraphAfter 'insert a paragraph break after it
myRange.End = myRange.End + 1 'move the end of the range beyond the paragraph break
myRange.Collapse 0 'Collapse the range to its end

ActiveDocument.Bookmarks.Add Name:="mark", Range:=myRange 'add a bookmark at the range
myRange.Select 'optionally select it

Exit Do 'Stop looking for the text
Loop

End With
End Sub
Reply With Quote
  #2  
Old 01-30-2023, 10:06 PM
gmayor's Avatar
gmayor gmayor is offline Word macro - How to add bookmark between specified text? Windows 10 Word macro - How to add bookmark between specified text? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Without seeing the document:
Code:
Sub Macro1()
Dim orng As Range
    Set orng = ActiveDocument.Range
    With orng.Find
        Do While .Execute(findText:="CEO*together.", MatchWildcards:=True)
            orng.Bookmarks.Add "mark"
            Exit Sub
        Loop
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-31-2023, 12:33 AM
syl3786 syl3786 is offline Word macro - How to add bookmark between specified text? Windows 10 Word macro - How to add bookmark between specified text? Office 2019
Advanced Beginner
Word macro - How to add bookmark between specified text?
 
Join Date: Jan 2023
Posts: 68
syl3786 is on a distinguished road
Default

Many thanks for your help! The macro runs well! May I know if this Macro can add bookmarks for many times and name the bookmark automatically? For example, my document contains lots of "CEO:XXX" and his/her speech end with a full stop. I wish the Macro can automatically select them one by one and add as bookmark. Or is it too demanding for the Microsoft Word to do so?
Reply With Quote
  #4  
Old 01-31-2023, 02:49 AM
gmayor's Avatar
gmayor gmayor is offline Word macro - How to add bookmark between specified text? Windows 10 Word macro - How to add bookmark between specified text? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It gets more complicated without access to the document, however the following may work for you. It bookmarks from CEO: to the next full stop.
Code:
Sub Macro1()
Dim orng As Range
Dim i As Long
    Set orng = ActiveDocument.Range
    With orng.Find
        i = 1
        Do While .Execute(findText:="CEO:*.", MatchWildcards:=True)
            orng.Bookmarks.Add "mark" & i
            i = i + 1
            orng.Collapse 0
        Loop
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 02-01-2023, 07:31 AM
syl3786 syl3786 is offline Word macro - How to add bookmark between specified text? Windows 10 Word macro - How to add bookmark between specified text? Office 2019
Advanced Beginner
Word macro - How to add bookmark between specified text?
 
Join Date: Jan 2023
Posts: 68
syl3786 is on a distinguished road
Default

This works well! Words are not enough to express my gratitude!
Reply With Quote
  #6  
Old 02-01-2023, 10:09 PM
gmayor's Avatar
gmayor gmayor is offline Word macro - How to add bookmark between specified text? Windows 10 Word macro - How to add bookmark between specified text? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If words are not enough, contributions to my web site (address in the signature block) are always welcome.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to insert different sets of text at bookmark depending on sequence of selected check boxes chipper09 Word VBA 0 06-21-2018 01:49 PM
Macro to transfer data from Word to another Word document with bookmark Jovan Yong Word VBA 3 04-17-2018 04:27 AM
macro to save as pdf with name from bookmark pamtupac Word VBA 3 06-06-2017 08:23 PM
Word macro - How to add bookmark between specified text? Macro to return a word count of text contained within a bookmark tomsrv Word VBA 13 08-24-2016 02:26 AM
Word macro - How to add bookmark between specified text? Word 2003 - IncludeText Does Not Include Bookmark Text if in a Form Text Control skarden Word 1 12-12-2011 10:39 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:20 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft