Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Change Bookmarked Text - Keep Bookmark?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RPJ - 31 Oct 2006 17:40 GMT
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
Jay Freedman - 31 Oct 2006 19:53 GMT
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

Signature

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.

> As a complete VBA novice, I want to overwrite some Bookmarked text
> with specific AutoText and retain the Bookmark (so I can repeat this
[quoted text clipped - 48 lines]
>
> RPJ
RPJ - 31 Oct 2006 20:05 GMT
Thanks for this, Jay.
I will give it go tomorrow when I get back to work.

RPJ

> The general principle is explained at
> http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.
[quoted text clipped - 84 lines]
> >
> > RPJ
Doug Robbins - Word MVP - 31 Oct 2006 20:08 GMT
See the article "Working with Bookmarks in VBA" at:

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

Signature

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

> 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
[quoted text clipped - 52 lines]
>
> RPJ

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.