Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    May 2003
    Location
    San Diego, California, USA
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Add Autotext to a loaded template (MS Word 2000 / VBA)

    I am trying to write a macro to add an AutoText entry to a loaded template.

    I can set up a dialog box to input the text and the autotext name

    e.g tbName and tbEntry

    Templates("C:Program FilesMicrosoft OfficeOfficeStartuptemplatename.dot").AutoTextEntr ies.Add Name:=tbname, _
    Range:=Selection.Range

    should work fine, but I am having difficulty setting selection.Range = the text variable tbEntry

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Add Autotext to a loaded template (MS Word 2000 / VBA)

    <P ID="edit" class=small>(Edited by jscher2000 on 06-Oct-03 13:02. Added a little note)</P>I believe you need to use a real Range in the document because Word stores the style along with the entry. However, after storing some range, any range, you can override the actual text with a plain string. For example:

    Sub OverwriteATEntry()
    Dim ate As AutoTextEntry
    Set ate = NormalTemplate.AutoTextEntries.Add("ateTest", _
    ActiveDocument.Range(0, 0)) 'picks up style of first paragraph
    ate.Value = "Original content overridden!"
    Set ate = Nothing
    End Sub

    Hope this helps.

    Added: ate.StyleName is a read-only property indicating the style of the original range. If you want the AutoTextEntry to be available in a particular context (e.g., header or footer, etc.), you might need to take extra care in how you set the "fake range" in the Add method.

  3. #3
    Lounger
    Join Date
    May 2003
    Location
    San Diego, California, USA
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Autotext to a loaded template (MS Word 2000 / VBA)

    Thanks for the reply.

    I am not sure that I understand. I want to save the style with the AutoText entry. The problem that I am having is defining the text variable for the Textbox as a range so that it can be added to the Autotext using the Add method.

    Somethink like:

    Set myRange = tbEntry.Text

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Add Autotext to a loaded template (MS Word 2000 / VBA)

    A Range needs to be a reference to a piece of an actual document. You cannot set a Range to a string of text that is not in the document (at least, not as far as I know!). That's why I used a "two-step" process in my code: Add with a real range, then substitute in the text.

  5. #5
    Lounger
    Join Date
    May 2003
    Location
    San Diego, California, USA
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Autotext to a loaded template (MS Word 2000 / VBA)

    I see what you are saying. I will give it a try!

  6. #6
    Lounger
    Join Date
    May 2003
    Location
    San Diego, California, USA
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Add Autotext to a loaded template (MS Word 2000 / VBA)

    Very clever! Thanks for getting me this far!

    Now as I think that you indicated, I need to apply a style to the added Autotext entry.

    i.e. I want to save the added AutoText entry with the Style "InsideAddress" rather than Normal.

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Add Autotext to a loaded template (MS Word 2000 / VBA)

    Well... this isn't a scientific test, but it appears that assigning a value clears the SyleName back to Normal. Try some variations and see if you can get it to stick. If push comes to shove, you could insert your text into the document, Add it as AutoText, then delete it. Messier, for sure, but perhaps more reliable.

    Sub OverwriteATEntry3()
    Dim rng As Range, ate As AutoTextEntry
    Set rng = ActiveDocument.Paragraphs(1).Range
    With rng
    rng.Style = "Body Text"
    Set ate = NormalTemplate.AutoTextEntries.Add("ateTest", rng)
    Debug.Print ate.StyleName ' Shows Body Text
    ate.Value = "Original content overridden!"
    Debug.Print ate.StyleName ' Shows Normal
    rng.Parent.Undo ' Revert style
    Debug.Print ate.StyleName ' Shows Normal
    End With
    Set ate = Nothing
    Set rng = Nothing
    End Sub

Posting Permissions

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