return the text from a numbered title (list)

W

ward

Hello,

I would like to be able to *easily* retrieve the text from
a numbered list paragraph (title), which is also a
bookmark.
E.g. from the following line

2.3.1 Dummy title

I would like to retrieve "Dummy title". The bookmark would
contain [Dummy title].
This seems rather simple by using something like:

Dim bm As Bookmark
For Each bm In ActiveDocument.Bookmarks
Debug.Print bm.Name, bm.Range.Text
Next

However, the paragraph might contain a manual page break
(because a user has entered one), which causes the
bookmark to start just before this manual page break.
This, of course, will make the bm.range.text property also
return the page break (which i don't want). In the debug
screen it's visualised as a little square, but when the
string is pasted somewhere in word, it does contain the
pagebreak.

Does anybody knows how to *easily* get just the text?

Ward

Ps: I've found a workaround to get what i want (see
below), but it's way to complicated. I figure it should be
possible to do it more simple.


'Check if the paragraph is preceded by a 'manual page break
'because this should be removed from the range
If Left(Rng.Paragraphs(1).Range, 1) = Chr$(12) Then
If mDoDebug Then Debug.Print "Pagebreak before"
Rng.SetRange Start:=Rng.Start + 1, End:=Rng.End
End If

'Do not include 'enter' at the end of the line
Rng.SetRange Start:=Rng.Start, End:=Rng.End - 1

'save range for later handling
Set mTargetParRange = Rng

'Return header/paragraph text
RtrnParagraphText = Trim(Rng.Text)
 
P

Peter Hewett

Hi Ward

Try this, this is probably the easiest and quickest way to clean up your
string. I've done it this way since your code does not actually change the
document text:

Public Sub GetCleanBMText()
Const cBookmarkName As String = "BMName"

Dim rngBMText As Word.Range
Dim strCleanText As String

' Get the bookmarked text
Set rngBMText = ActiveDocument.Bookmarks(cBookmarkName).Range

' Replace the any FormFeed characters in the text
strCleanText = Replace(rngBMText.Text, vbFormFeed, vbNullString)
End Sub

You obviously need to change the bookmark name, but the variable
"strCleanText" contains a clean string you can work with.

HTH + Cheers - Peter
 
W

ward

Thanks Peter!

Exactly what I needed. I've added an extra replace
procedure to trap line feeds ('enter') as wel:

strCleanText= Replace(Replace(rngBMText.Text, vbFormFeed,
vbNullString), vbCr, vbNullString)

Thanks,
Ward
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top