Delete two lines above bookmark - keep bookmark VBA

Sarahmueller

New Member
Joined
May 17, 2020
Messages
24
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello,

I hope you all are well.

I want to write a macro to delete two lines above a bookmark in a word document. So far so good, but the bookmark itself should not be deleted.

I have come up with the following code:

VBA Code:
Sub DeletetwoLinesAboveBookmark1()

    Dim r As Word.Range
    Set r = ActiveDocument.Bookmarks("Bookmark1").Range
    r.MoveStart wdSentence, -2
    r.Delete

End Sub


But this macro also deletes the bookmark.

Do you have an idea how to solve this problem?

Best regards and a have nice day,

Sarah
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try:
VBA Code:
Sub DeletetwoLinesAboveBookmark1()
Dim r As Word.Range
Set r = ActiveDocument.Bookmarks("Bookmark1").Range
r.Collapse wdCollapseStart
r.MoveStart wdSentence, -2
r.Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top