Creating bookmarks via VBA

A

andreas

Dear experts:

I wonder whether the following is possible

I got five text entries, each in its own paragraph

text1
text2
text3
text4
text5

With all 5 paragraphs selected is it possible to create 5 five
bookmarks automatically via VBA with the following names:

text1 (bookmark name: product_de)
text2 (bookmark name: product_en)
text3 (bookmark name: product_es)
text4 (bookmark name: product_fr)
text5 (bookmark name: product_it)

I hope this is feasible and not beyond the scope of this forum.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
 
M

macropod

Hi andreas,

You could use code like:
Sub Demo()
Dim StrBkMk As String, i, x As Integer
StrBkMk = "product_de,product_en,product_es,product_fr,product_it"
x = 5
With Selection
If .Paragraphs.Count < 5 Then x = .Paragraphs.Count
For i = 1 To x
.Paragraphs(i).Range.Bookmarks.Add (Split(StrBkMk, ",")(i - 1))
Next
End With
End Sub
 
D

Doug Robbins - Word MVP

The following code will do it:

Dim i As Long
Dim bmnames As Variant
bmnames = Split("product_de,product_en,product_es,product_fr,product_it",
",")
With Selection.Range
If .Paragraphs.Count = 5 Then
For i = 1 To .Paragraphs.Count
ActiveDocument.Bookmarks.Add bmnames(i - 1),
..Paragraphs(i).Range
Next i
Else
MsgBox "Please select only five paragraphs."
End If
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
A

andreas

The following code will do it:

Dim i As Long
Dim bmnames As Variant
bmnames = Split("product_de,product_en,product_es,product_fr,product_it",
",")
With Selection.Range
    If .Paragraphs.Count = 5 Then
        For i = 1 To .Paragraphs.Count
            ActiveDocument.Bookmarks.Add bmnames(i - 1),
.Paragraphs(i).Range
        Next i
    Else
        MsgBox "Please select only five paragraphs."
    End If
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com












- Show quoted text -

Hi Macropod and Doug,

thank you very much for your great help. Both code work almost as
desired. Is it possible to exclude the paragraph marks in the
selection?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
D

Doug Robbins - Word MVP

Use:

Dim i As Long
Dim bmnames As Variant
Dim target As Range
bmnames = Split("product_de,product_en,product_es,product_fr,product_it",
",")
With Selection.Range
If .Paragraphs.Count = 5 Then
For i = 1 To .Paragraphs.Count
Set target = .Paragraphs(i).Range
target.End = target.End - 1
ActiveDocument.Bookmarks.Add bmnames(i - 1), target
Next i
Else
MsgBox "Please select only five paragraphs."
End If
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
M

macropod

Hi andreas,

For my code, change to:
Sub Demo()
Dim StrBkMk As String, i, x As Integer, RngTmp As Range
StrBkMk = "product_de,product_en,product_es,product_fr,product_it"
x = 5
With Selection
If .Paragraphs.Count < 5 Then x = .Paragraphs.Count
For i = 1 To x
Set RngTmp = .Paragraphs(i).Range
With RngTmp
.End = .End - 1
.Bookmarks.Add (Split(StrBkMk, ",")(i - 1))
End With
Next
End With
End Sub

A key difference you'll note between my code and Doug's is that mine will work with any number of selected paragraphs, bookmarking
only as many as are eligible, whereas Doug's requires exactly 5 paragraphs to be selected.

--
Cheers
macropod
[Microsoft MVP - Word]


The following code will do it:

Dim i As Long
Dim bmnames As Variant
bmnames = Split("product_de,product_en,product_es,product_fr,product_it",
",")
With Selection.Range
If .Paragraphs.Count = 5 Then
For i = 1 To .Paragraphs.Count
ActiveDocument.Bookmarks.Add bmnames(i - 1),
.Paragraphs(i).Range
Next i
Else
MsgBox "Please select only five paragraphs."
End If
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com












- Show quoted text -

Hi Macropod and Doug,

thank you very much for your great help. Both code work almost as
desired. Is it possible to exclude the paragraph marks in the
selection?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
A

andreas

Use:

Dim i As Long
Dim bmnames As Variant
Dim target As Range
bmnames = Split("product_de,product_en,product_es,product_fr,product_it",
",")
With Selection.Range
    If .Paragraphs.Count = 5 Then
        For i = 1 To .Paragraphs.Count
            Set target = .Paragraphs(i).Range
            target.End = target.End - 1
            ActiveDocument.Bookmarks.Add bmnames(i - 1), target
        Next i
    Else
        MsgBox "Please select only five paragraphs."
    End If
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com









- Show quoted text -

Hi Dough and Macropod,

terrific help from both of you. I really appreciate your support.
Thank you very much!
Regards, Andreas
 
A

andreas

Hi andreas,

For my code, change to:
Sub Demo()
Dim StrBkMk As String, i, x As Integer, RngTmp As Range
StrBkMk = "product_de,product_en,product_es,product_fr,product_it"
x = 5
With Selection
  If .Paragraphs.Count < 5 Then x = .Paragraphs.Count
  For i = 1 To x
    Set RngTmp = .Paragraphs(i).Range
    With RngTmp
      .End = .End - 1
      .Bookmarks.Add (Split(StrBkMk, ",")(i - 1))
    End With
  Next
End With
End Sub

A key difference you'll note between my code and Doug's is that mine willwork with any number of selected paragraphs, bookmarking
only as many as are eligible, whereas Doug's requires exactly 5 paragraphs to be selected.

--
Cheers
macropod
[Microsoft MVP - Word]


The following code will do it:
Dim i As Long
Dim bmnames As Variant
bmnames = Split("product_de,product_en,product_es,product_fr,product_it",
",")
With Selection.Range
If .Paragraphs.Count = 5 Then
For i = 1 To .Paragraphs.Count
ActiveDocument.Bookmarks.Add bmnames(i - 1),
.Paragraphs(i).Range
Next i
Else
MsgBox "Please select only five paragraphs."
End If
End With
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
- Show quoted text -

Hi Macropod and Doug,

thank you very much for your great help. Both code work almost as
desired. Is it possible to exclude the paragraph marks in the
selection?

Help is much appreciated.  Thank you very much in advance. Regards,
Andreas- Hide quoted text -

- Show quoted text -

Hi Macropod,

.... and thank you very much for bringing the difference between the
two codes to my attention.
In my special case I can make use of both codes.
Thank you. Regards, Andreas
 
F

Fumei2 via OfficeKB.com

And (as usual) a possible alternative...

Sub MakeBM()
Dim oPara As Paragraph
Dim r As Range
Dim strBM_Name As String

For Each oPara In Selection.Paragraphs
strBM_Name = InputBox("Complete the bookmark name using " & _
"two characters. " & _
"Example: en" & vbCrLf & vbCrLf & _
"Bookmark will be: product_en")
Set r = oPara.Range
r.MoveEnd Unit:=wdCharacter, Count:=-1
ActiveDocument.Bookmarks.Add Name:=strBM_Name, _
Range:=r
Next
End Sub

Some advantages.

1. By using For Each, it does not matter the number of paragraphs selected.

2. You enter the bookmark suffix ("en", "de") dynamically, rather than
having it hard-coded.

Gerry
Hi andreas,
[quoted text clipped - 94 lines]
- Show quoted text -

Hi Macropod,

... and thank you very much for bringing the difference between the
two codes to my attention.
In my special case I can make use of both codes.
Thank you. Regards, Andreas
 
F

Fumei2 via OfficeKB.com

Plus...........you could add further logic so you do not even have to be
fussy about what is Selected, OR to be able to make larger selections.

Sub MakeBM()
Dim oPara As Paragraph
Dim r As Range
Dim strBM_Name As String

For Each oPara In Selection.Paragraphs
If Left(oPara.Range.Text, 4) = "text" Then
strBM_Name = InputBox("Complete the bookmark name using " & _
"two characters. " & _
"Example: en" & vbCrLf & vbCrLf & _
"Bookmark will be: product_en")
Set r = oPara.Range
r.MoveEnd Unit:=wdCharacter, Count:=-1
ActiveDocument.Bookmarks.Add Name:=strBM_Name, _
Range:=r
End If
Next
End Sub

So, if you have:

text1
text2
Yadda blah Blah
Some other text you want to ignore
And even more
text3
text4
text5

The paragraphs starting with "text" get the bookmarks, the other do not. You
can select the whole thing, but only the ones you want get the bookmarks.
Obviously, this logic could be expand in many directions. Perhaps the length
of the paragraphs, or other identifiable text strings, or a date field, or....



Gerry said:
And (as usual) a possible alternative...

Sub MakeBM()
Dim oPara As Paragraph
Dim r As Range
Dim strBM_Name As String

For Each oPara In Selection.Paragraphs
strBM_Name = InputBox("Complete the bookmark name using " & _
"two characters. " & _
"Example: en" & vbCrLf & vbCrLf & _
"Bookmark will be: product_en")
Set r = oPara.Range
r.MoveEnd Unit:=wdCharacter, Count:=-1
ActiveDocument.Bookmarks.Add Name:=strBM_Name, _
Range:=r
Next
End Sub

Some advantages.

1. By using For Each, it does not matter the number of paragraphs selected.

2. You enter the bookmark suffix ("en", "de") dynamically, rather than
having it hard-coded.

Gerry
[quoted text clipped - 8 lines]
In my special case I can make use of both codes.
Thank you. Regards, Andreas
 

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