Log in

View Full Version : [SOLVED:] Creating a Range Object between Bookmarks



Mavila
11-18-2019, 11:24 PM
Ever get that feeling after laying off coding for a while you forget how to do it?

Trying to Set myRange = text between two bookmarks. I've tried several variations of this and can't get it to work without throwing an error of one sort or another:



Set myRng = ActiveDocument.Range(Start:=ActiveDocument.Range.Bookmarks(bkmkTOCStartNm). start, End:=ActiveDocument.Range.Bookmarks(bkmkTOCEndNm).end)


Not sure what I'm doing wrong here.

Mavila
11-19-2019, 07:51 PM
OK, I think it's because the bookmarks are in a header.

gmayor
11-19-2019, 09:38 PM
That's part of it :)


Set myRng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).RangemyRng.Start = myRng.Bookmarks("bkmkTOCStartNm").Range.End
myRng.End = ActiveDocument.Bookmarks("bkmkTOCEndNm").Range.Start

Mavila
11-21-2019, 06:09 PM
This works (thanks, gmayor):



Dim myRng As Range
Set myRng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
myRng.Start = myRng.Bookmarks("T1").Range.End
myRng.End = ActiveDocument.Bookmarks("T2").Range.Start
myRng.Select