none
Macro to save autotext? RRS feed

  • Question

  • I want to make a macro that will allow me to toggle on or off a textbox. In the textbox is some information about the document's author that gets filled out by bookmarks. The bookmarks will be deleted once the document gets created from the template so it's important to record what gets written in the textbox so it's still there when it gets toggled off. I figure the best way to do this is to save the textbox as autotext attached to the document being written. I'm not sure how to write a macro though that will save something to autotext. Id there a way to do this?
    Wednesday, March 16, 2011 2:12 PM

Answers

  • Be aware that this code saves the AutoText entry in the template currently attached to the document. This will work well on the computer where the AutoText was saved; but if you send the document to another computer without also sending the template, then the AutoText will not be available.

    If you need a solution that saves the information (though not as an AutoText entry) within the document file itself, a better solution is to place the text from the textbox into a document variable. For an add-in that will help you to manipulate document variables easily, visit http://gregmaxey.mvps.org/ContentControl_Variable_Bookmark_and_Document_Property_Tools_Add_In.htm.


    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org
    Monday, March 21, 2011 12:29 AM
  • Hi CPVitale,

    Here is the essential VBA code.

        Dim atEntry As AutoTextEntry
        Dim atName As String: atName = "YourTextBox"

        For Each atEntry In _
           ActiveDocument.AttachedTemplate.AutoTextEntries
           If atEntry.Name = atName Then
               atEntry.Delete
               Exit For
            End If
        Next atEntry

    Add your code to select the textbox then ... 

        ActiveDocument.AttachedTemplate.AutoTextEntries.Add _
              Range:=Selection.Range, Name:=atName

    Hope this helps


    All the best, Rich
    Sunday, March 20, 2011 7:59 PM

All replies

  • Hi CPVitale,

    Thanks for posting in the MSDN Forum.

    According to your description, you want to create a macro to fill the information to the bookmarks. And when you create this document from a Word template the bookmarks will be deleted. Is it right?

    Would you please tell me whether you would like to set the bookmarks in the Word template?  Or you have recently set these bookmarks in the Word template, but when you create a new Word document bookmarks will lost its information and you when to keep the information in the bookmarks.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 18, 2011 5:31 AM
    Moderator
  • Tom,

    Basically on the right track. I have two bookmarks in a textbox that have been saved to the template. After a document gets created from a template, the author's information is placed where the bookmarks are and then the bookmarks are deleted.

    What I want to do is to have a macro that will take that filled in textbox, save it to autotext and then delete it. Then I will have another macro that will recall the textbox with its information from autotext and place back in the document. That macro, I already know how to do.

    My problem is how do you write something in VBA 6 that will save something to autotext.

    Friday, March 18, 2011 7:47 PM
  • Hi CPVitale,

    Here is the essential VBA code.

        Dim atEntry As AutoTextEntry
        Dim atName As String: atName = "YourTextBox"

        For Each atEntry In _
           ActiveDocument.AttachedTemplate.AutoTextEntries
           If atEntry.Name = atName Then
               atEntry.Delete
               Exit For
            End If
        Next atEntry

    Add your code to select the textbox then ... 

        ActiveDocument.AttachedTemplate.AutoTextEntries.Add _
              Range:=Selection.Range, Name:=atName

    Hope this helps


    All the best, Rich
    Sunday, March 20, 2011 7:59 PM
  • Be aware that this code saves the AutoText entry in the template currently attached to the document. This will work well on the computer where the AutoText was saved; but if you send the document to another computer without also sending the template, then the AutoText will not be available.

    If you need a solution that saves the information (though not as an AutoText entry) within the document file itself, a better solution is to place the text from the textbox into a document variable. For an add-in that will help you to manipulate document variables easily, visit http://gregmaxey.mvps.org/ContentControl_Variable_Bookmark_and_Document_Property_Tools_Add_In.htm.


    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org
    Monday, March 21, 2011 12:29 AM