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

Re: Change Bookmarked Text - Keep Bookmark? by Jay

Jay
Tue Oct 31 12:53:33 CST 2006

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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

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



Re: Change Bookmarked Text - Keep Bookmark? by RPJ

RPJ
Tue Oct 31 13:05:02 CST 2006

Thanks for this, Jay.
I will give it go tomorrow when I get back to work.

RPJ

"Jay Freedman" wrote:

> 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 FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
> all may benefit.
>
> RPJ wrote:
> > 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
>
>
>

Re: Change Bookmarked Text - Keep Bookmark? by Doug

Doug
Tue Oct 31 13:08:04 CST 2006

See the article "Working with Bookmarks in VBA" at:

http://www.word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm


--
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

"RPJ" <RPJ@discussions.microsoft.com> wrote in message
news:453790B2-314D-4F36-933B-9D9540C88F00@microsoft.com...
> 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