View Single Post
 
Old 08-23-2020, 03:40 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote