Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #7  
Old 08-23-2020, 03:40 PM
Guessed's Avatar
Guessed Guessed is offline Using If Then with Bookmarks to specify text Windows 10 Using If Then with Bookmarks to specify text 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
 



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
Using If Then with Bookmarks to specify text How to insert bookmarks for content or Text of ContentControl lvganesh Word VBA 5 12-12-2017 11:27 PM
Using If Then with Bookmarks to specify text 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
Using If Then with Bookmarks to specify text Form updating Bookmarks - writes to the bookmarks multiple times PeterPlys Word VBA 13 01-14-2015 06:41 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft