Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-18-2023, 03:15 PM
dugdugdug dugdugdug is offline Delete text within two bookmarks Windows 10 Delete text within two bookmarks Office 2019
Novice
Delete text within two bookmarks
 
Join Date: Jan 2023
Posts: 2
dugdugdug is on a distinguished road
Default Delete text within two bookmarks

Assuming I have the following text:



Code:
Hello,

"BOOKMARK_START"

What are you doing

'BOOKMARK_END"

Call me
I want to delete the text within two bookmarks in Word to end up with:

Code:
Hello,

Call me
I found this code:

Code:
Dim pRange
Dim dRange
Dim myrange

pRange = ActiveDocument.Bookmarks("BOOKMARK_START").Range.Start
dRange = ActiveDocument.Bookmarks("BOOKMARK_END").Range.End
Set myrange = ActiveDocument.Range(Start:=pRange, End:=dRange)
myrange.Cut
What happens when the code is run is BOOKMARK_START and BOOKMARK_END point to the same place, (which is BOOKMARK_START), ie:

Code:
Hello,

BOOKMARK_START / BOOKMARK_END

Call me
Next time I insert something into BOOKMARK_START (and then try to delete it), it won't recognise BOOKARK_END (because it's the same location as BOOKMARK_START).

How can I get round this?
Reply With Quote
  #2  
Old 01-18-2023, 04:23 PM
Guessed's Avatar
Guessed Guessed is offline Delete text within two bookmarks Windows 10 Delete text within two bookmarks Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,164
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Using Bookmarks is quite clumsy because you have little control on where those bookmarks start/end and the 'end' may be in front of the 'start'. Once you have removed the intervening content you will find it difficult to prise the two locations apart again.

A simpler way of defining a location for content is to use a Content Control and write to that.
Code:
ActiveDocument.SelectContentControlsByTitle("Info")(1).Range.Text = "Hi Mum"
Note that when you remove all content of that CC, it will display the placeholder text which may or may not suit your purposes. You can either edit the placeholder text or put a single space in the placeholder to avoid this problem.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 01-18-2023, 10:28 PM
gmayor's Avatar
gmayor gmayor is offline Delete text within two bookmarks Windows 10 Delete text within two bookmarks Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
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 ofgmayor has much to be proud of
Default

Quote:
Note that when you remove all content of that CC, it will display the placeholder text which may or may not suit your purposes. You can either edit the placeholder text or put a single space in the placeholder to avoid this problem
If you fill the content control with zero width space ChrW(8203) the control will completely disappear, but will still be present to be refilled with something else.
Code:
Sub Macro1
    FillCC "MyCCTitle", ChrW(8203), True

End Sub
Code:
Public Sub FillCC(strCCTitle As String, strValue As String, bLock As Boolean)
'Graham Mayor - https://www.gmayor.com - Last updated - 03 Sep 2021
Dim oCC As ContentControl
    On Error GoTo lbl_Exit
    For Each oCC In ActiveDocument.ContentControls
        With oCC
            If .Title = strCCTitle Then
                .LockContents = False
                .Range.Text = strValue
                .LockContentControl = True
                .LockContents = bLock
                Exit For
            End If
        End With
    Next oCC
lbl_Exit:
    Set oCC = Nothing
    Exit Sub
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
  #4  
Old 01-19-2023, 03:50 AM
dugdugdug dugdugdug is offline Delete text within two bookmarks Windows 10 Delete text within two bookmarks Office 2019
Novice
Delete text within two bookmarks
 
Join Date: Jan 2023
Posts: 2
dugdugdug is on a distinguished road
Default

Thanks for the suggestions.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete text within two bookmarks Using If Then with Bookmarks to specify text RedZed1100 Word VBA 11 08-26-2020 04:15 PM
Replacing text held in all bookmarks THH4929 Word VBA 6 06-02-2018 04:29 AM
Delete Selected Bookmarks sks27 Word VBA 3 06-03-2016 02:05 AM
Delete text within two bookmarks Delete Bookmarks in Footnotes brent chadwick Word VBA 3 12-24-2015 01:40 PM
Delete text within two bookmarks Form updating Bookmarks - writes to the bookmarks multiple times PeterPlys Word VBA 13 01-14-2015 06:41 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:48 PM.


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