![]() |
|
#7
|
||||
|
||||
|
As a learning experience, creating loops is a useful skill but your examples are not saving a great deal of code. The outer/inner loops don't do anything other than ensuring the actual inside code is run 4 times to do something that only requires a single pass. An example to make use of both loops would be to include both in the bookmark name eg
Set oBMRng = ActiveDocument.Bookmarks("MyBookmark" & BMcnt & lngIndex).Range Another technique worth learning that could be applied to this problem is to call a function and pass values in to the function. I haven't used the loop and Select Case because you are doing something different for each of the loop values. This code also shows how to avoid an error if a bookmark doesn't exist. Code:
Sub BookmarkUpdate2()
ResetBookmarks sName:="MyBookmark1", sValue:="First"
ResetBookmarks sName:="MyBookmark2", sValue:="Second"
ResetBookmarks sName:="MyBookmark3", sValue:="Third"
ResetBookmarks sName:="MyBookmark4", sValue:=""
End Sub
Function ResetBookmarks(sName As String, sValue As String)
Dim oRng As Range
With ActiveDocument
If .Bookmarks.Exists(sName) Then
Set oRng = .Bookmarks(sName)
oRng.Text = sValue
.Bookmarks.Add sName, oRng
Else
MsgBox "Bookmark does not exist: " & sName
End If
End With
End Function
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Replacing text held in all bookmarks | THH4929 | Word VBA | 6 | 06-02-2018 04:29 AM |
How to insert bookmarks for content or Text of ContentControl
|
lvganesh | Word VBA | 5 | 12-12-2017 11:27 PM |
Deleting only all bookmarks contents not the bookmarks
|
adilprodigy | Word VBA | 1 | 10-11-2017 01:31 PM |
| Macro to hide/unhide text to call variable bookmarks | Dr. Z | Word VBA | 2 | 05-27-2017 08:20 PM |
Form updating Bookmarks - writes to the bookmarks multiple times
|
PeterPlys | Word VBA | 13 | 01-14-2015 06:41 AM |