From: macropod on
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]


"andreas" <andreas.hermle(a)gmx.de> wrote in message news:be0b4cfb-7823-4762-a720-0cc7bb48a5f1(a)j21g2000yqh.googlegroups.com...
On Apr 5, 11:24 am, "Doug Robbins - Word MVP"
<d...(a)REMOVECAPSmvps.org> wrote:
> 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
>
> "andreas" <andreas.her...(a)gmx.de> wrote in message
>
> news:6f22bcf7-3972-4c0e-b8e3-4322dece4be5(a)u22g2000yqf.googlegroups.com...
>
>
>
> > 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- Hide quoted text -
>
> - 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

From: andreas on
On Apr 5, 11:55 pm, "Doug Robbins - Word MVP"
<d...(a)REMOVECAPSmvps.org> wrote:
> 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
>
> "andreas" <andreas.her...(a)gmx.de> wrote in message
>
> news:be0b4cfb-7823-4762-a720-0cc7bb48a5f1(a)j21g2000yqh.googlegroups.com...
>
>
>
> > On Apr 5, 11:24 am, "Doug Robbins - Word MVP"
> > <d...(a)REMOVECAPSmvps.org> wrote:
> >> 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
>
> >> "andreas" <andreas.her...(a)gmx.de> wrote in message
>
> >>news:6f22bcf7-3972-4c0e-b8e3-4322dece4be5(a)u22g2000yqf.googlegroups.com....
>
> >> > 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- Hide quoted text -
>
> >> - 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 Dough and Macropod,

terrific help from both of you. I really appreciate your support.
Thank you very much!
Regards, Andreas
From: andreas on
On Apr 6, 12:04 am, "macropod" <macro...(a)invalid.invalid> wrote:
> 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]
>
> "andreas" <andreas.her...(a)gmx.de> wrote in messagenews:be0b4cfb-7823-4762-a720-0cc7bb48a5f1(a)j21g2000yqh.googlegroups.com...
>
> On Apr 5, 11:24 am, "Doug Robbins - Word MVP"
>
>
>
>
>
> <d...(a)REMOVECAPSmvps.org> wrote:
> > 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
>
> > "andreas" <andreas.her...(a)gmx.de> wrote in message
>
> >news:6f22bcf7-3972-4c0e-b8e3-4322dece4be5(a)u22g2000yqf.googlegroups.com....
>
> > > 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- Hide quoted text -
>
> > - 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
From: Fumei2 via OfficeKB.com on
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

andreas wrote:
>> 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

--
Gerry

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1

From: Fumei2 via OfficeKB.com on
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
Fumei2 wrote:
>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 - 8 lines]
>>In my special case I can make use of both codes.
>>Thank you. Regards, Andreas
>

--
Gerry

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1