PDA

View Full Version : VBA-Bookmark



Limette
11-18-2008, 04:09 PM
Huhu,

I want to make Bookmarks automatically...
this Bookmark should make out a starter( word : "Begin") ...and a "End"

Can you help me ?

Thanks a lot

Nadine

lucas
11-18-2008, 04:32 PM
Could you give a little more detail of what you are trying to do please?

lucas
11-18-2008, 04:47 PM
This will put a bookmark named "temp" at the selection. If you select a word or sentence it will be included in the bookmark. As I said, I don't really understand what you are trying to do.

ActiveDocument.Bookmarks.Add Name:="temp", Range:=Selection.Range

Limette
11-19-2008, 04:12 AM
Ok, i try again :D

<text>
"start"
<text>
<text>
"end"
<text>

...i need a bookmard from keyword "start" to keyword "end"...

do you understand my request ? :)

lucas
11-19-2008, 08:45 AM
There has to be something that sets the text that you want to bookmark aside from the rest of the text.

Some distinguishing characteristic.

What makes the text you want to bookmark different from the rest of the text in the document?

fumei
11-19-2008, 11:25 AM
"do you understand my request ?"

No, at least I do not.

1. You have:

<text>
"start"
<text>
<text>
"end"
<text>

OK, does that actually mean:

"other text here, blah blah" your <text>
"start"
"other text here, blah blah" your <text>
"other text here, blah blah" your <text>
"end"
"other text here, blah blah" your <text>

In other words, does <text> mean actual text?

2. "...i need a bookmard from keyword "start" to keyword "end"..."

OK, what does that mean? Does it mean the bookmark will be:

"start"
<text>
<text>
"end"

"from keyword "start" to keyword "end"

Or, does it mean:

<text>
"start"
<text>
<text>
"end"
<text>

Also "from keyword "start" to keyword "end". In other words, does "from" include the keywords?

I know that sounds picky, but you have to be very specific, and explicit, because we do not know (nor does VBA) what you mean, or intend. It only does, exactly, what you tell it do.

Limette
11-20-2008, 01:32 AM
Hi Hi,


it makes me sad, that my request is so difficult to understand :dunno

I meant that way:

"other text here, blah blah" your <text>
"start"
"other text here, blah blah" your <text>
"other text here, blah blah" your <text>
"end"
"other text here, blah blah" your <text>

In other words, does <text> mean actual text?


meanwhile i have found a solution:



Function fkt_Search2(strStart As String, strEnd As String, sTextmarkenname As String, Optional bInclude As Boolean = False)
Dim rng As Range
Dim rngText As Range
Dim iNr As Integer: iNr = 0
' set range
Set rng = ActiveDocument.Range
' set range
Set rngText = ActiveDocument.Range(0, 0)
rngText.Collapse wdCollapseStart
' search loop
With rng.Find
.Format = False
.Text = strStart
' search for "begin"-Tag
.Execute
Do While .Found = True
iNr = iNr + 1
' create "found" for "begin"-Tag
rngText.SetRange rng.Start, rng.End
' reducing searchrange
rng.SetRange rng.End, ActiveDocument.Range.End
' search for "end"-tag
.Execute FindText:=strEnd, Forward:=True
' abort if noch "end"-tag
If .Found = False Then Exit Function
' "found" range extend to "end"-tag
rngText.SetRange rngText.Start, rng.End
If bInclude = True Then
' with tags
' example: set bookmark
rngText.Select
rngText.Bookmarks.Add sTextmarkenname & CStr(iNr)
ElseIf bInclude = False Then
' without tags
' example: set book
rngText.SetRange rngText.Start + Len(strStart), rngText.End - Len(strEnd)
rngText.Select
rngText.Bookmarks.Add sTextmarkenname & CStr(iNr)
End If
' reducing search-range to "end"-tag
rng.Collapse wdCollapseEnd
' search "begin"-tag
.Execute FindText:=strStart, Forward:=True
Loop
'x = iNr
rng.Collapse wdCollapseEnd
End With
End Function

Sub subSearchDT()
fkt_Search2 "begin", "end", "bookmark", True
End Sub


cu & thanks guys :beerchug:

fumei
11-20-2008, 10:58 AM
"it makes me sad, that my request is so difficult to understand"

Sorry that it makes you sad, but it was not difficult to understand at all. What you are trying to do is fairly common. The problem was you did not say what it was! We are not mind readers. If there is difficulty in understanding, more often it is because someone did not explain well, or did not say at all.

I am glad you found a solution. It is a good one.

If I may, can I suggest some things? I am doing this simply to help, and you can ignore it as you wish.
Set rngText = ActiveDocument.Range(0, 0)
rngText.Collapse wdCollapseStart
You do not need the Collapse, as you have already set rngText as (0,0). It is collapsed.

You do not need the extra instructions by using .Found, like this:
.Format = False
.Text = strStart
' search for "begin"-Tag
.Execute
Do While .Found = True

You can use the methods of .Execute itself, like this:

With rng.Find
Do While .Execute(FindText:=strStart, Forward:=True) _
= True

It will only execute if Found is True.

The following is essentially a logic statement, even though you have then as two separate instructions.
If .Found = False Then Exit Function
' "found" range extend to "end"-tag
rngText.SetRange rngText.Start, rng.End

It would be better to have them complete the logic statement as a block, like this:

If .Found = False Then
Exit Function
Else
rngText.SetRange rngText.Start, rng.End
End If
In the following code, you do not need the ElseIf, as Else means the same thing logically. Further, unless you actually want to have the visual screen selecting and selecting (with rngText.Select), you dot need to select anything. You can do it using the Range itself, which is the point of using Ranges.

If bInclude = True Then
' with tags
' example: set bookmark
rngText.Select
rngText.Bookmarks.Add sTextmarkenname & CStr(iNr)
ElseIf bInclude = False Then
' without tags
' example: set book
rngText.SetRange rngText.Start + Len(strStart), _
rngText.End - Len(strEnd)
rngText.Select
rngText.Bookmarks.Add sTextmarkenname & CStr(iNr)
End If

can be written as:

If bInclude = True Then
' with tags
' example: set bookmark
rngText.Bookmarks.Add Name:=sTextmarkenname & CStr(j), _
Range:=rngText
Else 'this MEANS bInclude = False
' without tags
' example: set book
rngText.SetRange rngText.Start + Len(strStart), _
rngText.End - Len(strEnd)
rngText.Bookmarks.Add Name:=sTextmarkenname & CStr(j), _
Range:=rngText
End If



All in all though, you found a good solution. Here is my version - a little tighter, and does not use Selection at all. Nothing is selected, as you do not need to.

Function Gerry_Search2(strStart As String, _
strEnd As String, _
sTextmarkenname As String, _
Optional bInclude As Boolean = False)
Dim rng As Range
Dim rngText As Range
Dim j As Long
' set range
Set rng = ActiveDocument.Range
' set range
Set rngText = ActiveDocument.Range(0, 0)

' search loop
With rng.Find
Do While .Execute(FindText:=strStart, Forward:=True) _
= True
j = j + 1
' create "found" for "start"-Tag
rngText.SetRange rng.Start, rng.End
' reducing searchrange
rng.SetRange rng.End, ActiveDocument.Range.End
' search for "end"-tag
.Execute FindText:=strEnd, Forward:=True
' abort if no "end"-tag found
If .Found = False Then
Exit Function
Else
rngText.SetRange rngText.Start, rng.End
End If

If bInclude = True Then
' with tags
' example: set bookmark
rngText.Bookmarks.Add Name:=sTextmarkenname & CStr(j), _
Range:=rngText
Else
' without tags
' example: set book
rngText.SetRange rngText.Start & Len(strStart), _
rngText.End - Len(strEnd)
rngText.Bookmarks.Add Name:=sTextmarkenname & CStr(j), _
Range:=rngText
End If
' reducing search-range to "end"-tag
rng.Collapse wdCollapseEnd
' search "begin"-tag
Loop
rng.Collapse wdCollapseEnd
End With
End Function

Demo attached with THREE blocks of text that fit the logic of the Sub searchDT. Click "Demo subSearchDT" on the top toolbar.

fumei
11-20-2008, 11:20 AM
I have to ask though...why are you doing this as a Function?

Dave
11-22-2008, 09:23 PM
Very nice Jerry.. NO selection and I like the CStr. I'm going to have to learn abit more about the use of Execute. Could you maybe add a bit more comment about how to call your function it so I can test without the demo? Thanks. Dave

fumei
11-24-2008, 03:25 PM
Not until you answer my question.