Change Bookmarked Text - Keep Bookmark?

R

RPJ

As a complete VBA novice, I want to overwrite some Bookmarked text with
specific AutoText and retain the Bookmark (so I can repeat this Macro if/when
rqrd).

I have manged so far to generate the following simple code:

Sub CHOOSE_TEXT()
'
' SLT Macro
'
Selection.GoTo What:=wdGoToBookmark, Name:="LTXT"
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With ActiveDocument
If .FormFields("Dropdown1").Result = "Specification No." Then
Application.DisplayAutoCompleteTips = True
NormalTemplate.AutoTextEntries("SLT").Insert Where:=Selection.Range, _
RichText:=True
Else
Application.DisplayAutoCompleteTips = True
NormalTemplate.AutoTextEntries("QLT").Insert Where:=Selection.Range, _
RichText:=True
End If
End With
End Sub

The above Macro is run on exit from a dropdown box in a form within an
earlier protected section of the document.

UNFORTUNATELY if the Bookmark "LTXT" is text then the macro only works once
as it obliterates the Bookmark along with the text.

IF the bookmark "LTXT" is just a position within the document then the macro
will happily re-run - BUT it doesn't replace text (it just pushes it down the
document, which is not acceptable).

CAN ANYONE HELP, PLEASE?

Rgds,

RPJ
 
J

Jay Freedman

The general principle is explained at
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.
Essentially, you replace the text inside the bookmark, and that destroys the
bookmark itself, so then you re-add the bookmark to cover the range.

In your code below, everything from Selection.GoTo through the first End
With is worthless and should be removed. (The stupid macro recorder throws
in all sorts of garbage.) You also need code to remove the Forms protection
before you do anything with the bookmark, and code to replace the protection
afterward.

Try this code instead:

Sub CHOOSE_TEXT()
Dim myRange As Range
Set myRange = ActiveDocument.Bookmarks("LTXT").Range

With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

If .FormFields("Dropdown1").Result = "Specification No." Then
NormalTemplate.AutoTextEntries("SLT").Insert Where:=myRange, _
RichText:=True
Else
NormalTemplate.AutoTextEntries("QLT").Insert Where:=myRange, _
RichText:=True
End If

.Bookmarks.Add Name:="LTXT", Range:=myRange

.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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