Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Search and Replace within Autotext Collection (Word 2003 onwards)

    Okay, I've done my best but I've got stuck again. I *think* this should mostly work, except I can't work out the last bit, how to resave the amended entry back into the collection - sorry! I've included everything here, so I'm sure I won't have forgotten anything vital because it's not included! Obviously the info is from a form, giving the 'find' text and 'replace' text.

    Option Explicit

    Public Tmp As Template
    Public AtE As AutoTextEntry
    Public txtFnd As String
    Public txtRpl As String
    Public colAutoText As Collection

    Private Sub cmdOK_Click()
    Dim txtName As String
    Set Tmp = ActiveDocument.AttachedTemplate
    txtFnd = Me.txtFinder
    txtRpl = Me.txtReplacer
    txtName = Tmp.AutoTextEntries(1).Name
    Dim i As Integer

    Me.hide
    Application.ScreenUpdating = False

    For Each AtE In Tmp

    Application.DisplayAutoCompleteTips = True
    AtE.Insert Where:=Selection.range, Richtext:=True
    Selection.Find.Execute findtext:=txtFnd, replacewith:=txtRpl
    Selection.WholeStory
    Selection.Copy
    colAutoText.Add AtE.Name

    Next

    Application.ScreenUpdating = True
    End Sub

    I would greatly appreciate any help to finish this, please - this and one other thing have to be finished tonight, and it's already nearly one in the morning ... <img src=/S/sad.gif border=0 alt=sad width=15 height=15>
    Beryl M


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and Replace within Autotext Collection (Word 2003 onwards)

    The line

    For Each AtE In Tmp

    makes no sense - an AutoTextEntry is not a property of a template.

    You don't use the variables txtName and i.

    You don't specify what the Find.Execute action should do.

    You add AtE to the colAutoText collection but you don't add it to the AutoTextEntries collection if that was your intention.

    Try this version:

    Private Sub cmdOK_Click()
    Dim Tmp As Template
    Dim AtE As AutoTextEntry
    Dim txtFnd As String
    Dim txtRpl As String
    Dim rng As Range

    Set Tmp = ActiveDocument.AttachedTemplate
    txtFnd = Me.txtFinder
    txtRpl = Me.txtReplacer

    Me.Hide
    Application.ScreenUpdating = False

    For Each AtE In Tmp.AutoTextEntries
    Set rng = AtE.Insert(Where:=Selection.Range, RichText:=True)
    rng.Find.Execute FindText:=txtFnd, ReplaceWith:=txtRpl, Replace:=wdReplaceAll
    Tmp.AutoTextEntries.Add AtE.Name, rng
    rng.Delete
    Next

    Application.ScreenUpdating = True
    End Sub

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Search and Replace within Autotext Collection (Word 2003 onwards)

    <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15> ... Every time I think I'm beginning to get a handle on this business, I find out how badly wrong I am ... again!

    Thanks, Hans!

    By the way, is there a good reference somewhere that explains exactly what propertyLet and propertyGet do? The original author of this template used these extensively and I'm not sure how they work.

    Many thanks again!
    Beryl M


  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and Replace within Autotext Collection (Word 2003 onwards)

    See for example Classes In VBA.

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Search and Replace within Autotext Collection (Word 2003 onwards)

    Thanks, Hans.
    Beryl M


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •