Consulting

Results 1 to 5 of 5

Thread: Solved: create document to list all AutoText associated witha style

  1. #1
    VBAX Regular
    Joined
    Jun 2008
    Posts
    22
    Location

    Solved: create document to list all AutoText associated witha style

    I am trying to use vba to create a new document that lists all of the autotext entries formated:

    Autotext entry name

    Autotext entry
    ___________________________________________________________

    I have found the code below which lists all autocorrect entries but cannot seem to figure out how to do something similar with autotext. I would like the user to select a style and click a toolbar button to produce the associated autotext list.

    Code for autocorrect:

    [vba]Dim ACE As AutoCorrectEntry
    Documents.Add
    For Each ACE In Application.AutoCorrect.Entries
    Selection.TypeText ACE.Name & vbTab & ACE.Value & " " & vbCr
    Next
    'Format document for three columns.
    With ActiveDocument.PageSetup.TextColumns
    .SetCount NumColumns:=3
    .EvenlySpaced = True
    .LineBetween = True
    .Width = InchesToPoints(1.67)
    .Spacing = InchesToPoints(0.5)
    End With
    ActiveDocument.Paragraphs.TabStops(InchesToPoints(0.88)).Position = _
    InchesToPoints(0.88)[/vba] [UVBA].[/UVBA]
    ~Oorang


    Any help would be appreciated
    Thanks
    Dan

  2. #2
    As far as I'm aware autotext entries are associated with templates, not styles. You could start with this code:
    [VBA]Dim atEntry As AutoTextEntry

    For Each atEntry In ActiveDocument.AttachedTemplate.AutoTextEntries
    Selection.TypeText atEntry.Name & vbTab & atEntry.Value & " " & vbCr
    Next[/VBA]
    Regards

  3. #3
    VBAX Regular
    Joined
    Jun 2008
    Posts
    22
    Location

    Hmmmm

    I have limited knowledge but was under the impression that autotext entries were at least grouped by style. I can create a new style called "Dans Autotext" and highlight/select some text, apply that style and then when I want to narrow down my autotext list to just items associated with "Dans Autotext", simply place the cursor at the insertion point, change the style to "Dans Autotext" and it will limit the list to just those entries. I know this works as I am currently using the method.

    I want to have the user select "Dans Autotext" style and click a button which will produce just that list.

    Thanks for the code to produce the list, but it produces the entire list in the attach(ed) templates.

    Any other ideas?
    Dan

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "simply place the cursor at the insertion point, "

    This is redundant, as the cursor IS the insertion point. You can not really place the cursor at the insertion point, as again, it IS the insertion point. You can place the cursor (the insertion point) where you want to insert something.

    "change the style to "Dans Autotext" and it will limit the list to just those entries. "

    should be:

    change the style to "Dans Autotext" and it will limit the list to just those existing entries that use that style.

    However, unfortunately it is directional. AutoText can detect a style within the autotext entries, but selecting a style means ONLY that. A style is selected. You can do something like this though.

    A userform that has a ListBox and a Commandbutton. You can have the userform display from a button on the toolbar (or shortcut key, or whatever).

    When the userform is started, have it populate the listbox with all AutoText names that use your style (Dans List).

    User selects an item in the list, selects the button, and THAT AutoText is inserted at the Selection (the insertion point).[vba]

    Private Sub UserForm_Initialize()
    Dim atEntry As AutoTextEntry

    For Each atEntry In ActiveDocument.AttachedTemplate.AutoTextEntries
    If atEntry.StyleName = "Dans List" Then
    ListBox1.AddItem atEntry.Name
    End If
    Next
    End Sub

    Private Sub cmdInsert_Click()
    Dim strAT_Name As String
    strAT_Name = ListBox1.Value
    If Selection.Style = "Dans List" Then
    Selection.Collapse Direction:=wdCollapseEnd
    ActiveDocument.AttachedTemplate.AutoTextEntries(strAT_Name).Insert _
    Where:=Selection.Range
    Else
    MsgBox "The style at the selection does not " & _
    "the style of the selected AutoText."
    End If
    Unload Me
    End Sub

    [/vba]

    Note: There is an error trap in the commandbutton code that checks to see if the style at the Selection is, in fact, the same style as the AutoText. If not, it displays a message and exits.

    This could be adjusted a number of ways.

    1. You could have the userform populate the listbox with autotext that matches the current selection.style. The code above hard-codes "Dans List".

    2. You could change the selection.style to match.

    Otherwise, if the autotext is inserted - even though the AutoText has Dans List as the style - the inserted AutoText may not be that style. Although this depends greatly on whether the AutoText entry did, or did not, contain a terminating paragraph mark.

  5. #5
    VBAX Regular
    Joined
    Jun 2008
    Posts
    22
    Location

    Heres what I put together

    Thanks fumei,

    Your help got me through.... Here is what I ended up with. A form with a list box, a combobox and a few buttons.

    When the form opens the combobox is populated with styles. When a style is selected, the listbox loads with autotext entries that use that style.

    The button options are Print the items displayed in the listbox. Backup, which actually just builds a document that the user can save, and your insert button which inserts the autotext entry selected.

    This does about everything I had wanted, due to your kick start.

    Thanks So Much
    DC

    [vba]Private Sub cbBackup_Click()
    Dim aDialog As Dialog
    Documents.Add
    Set myRange = Selection.Range
    myRange.WholeStory
    myRange.Font.Name = "Arial"
    myRange.Font.Size = 11
    With ActiveDocument.PageSetup
    .Orientation = wdOrientPortrait
    .TopMargin = InchesToPoints(1)
    .BottomMargin = InchesToPoints(1)
    .LeftMargin = InchesToPoints(1.25)
    .RightMargin = InchesToPoints(1.25)
    .Gutter = InchesToPoints(0)
    .HeaderDistance = InchesToPoints(0.5)
    .FooterDistance = InchesToPoints(0.5)
    .PageWidth = InchesToPoints(8.5)
    .PageHeight = InchesToPoints(11)
    .FirstPageTray = wdPrinterDefaultBin
    .OtherPagesTray = wdPrinterDefaultBin
    .SectionStart = wdSectionNewPage
    .OddAndEvenPagesHeaderFooter = False
    .DifferentFirstPageHeaderFooter = False
    .VerticalAlignment = wdAlignVerticalTop
    .SuppressEndnotes = False
    .MirrorMargins = False
    .TwoPagesOnOne = False
    .GutterPos = wdGutterPosLeft
    End With

    Dim atEntry As AutoTextEntry

    For Each atEntry In Application.NormalTemplate.AutoTextEntries
    If atEntry.StyleName = ComboBox1.Value Then
    Selection.Font.Size = 14
    Selection.TypeText Text:=atEntry.Name
    Selection.Font.Size = 10
    Selection.TypeParagraph
    Selection.TypeText atEntry.Value
    Selection.TypeText "" & vbCrLf & ""
    Selection.TypeParagraph
    End If
    Next
    ActiveDocument.Activate
    Set aDialog = Application.Dialogs(wdDialogFileSaveAs)
    With aDialog
    .Name = ComboBox1.Value & " Backup"
    .Show
    End With
    Unload spt_AutotextGroup
    End Sub
    Private Sub cbClose_Click()
    Unload Me
    End Sub
    Private Sub cbPrint_Click()
    Dim aDialog As Dialog
    Documents.Add
    Set myRange = Selection.Range
    myRange.WholeStory
    myRange.Font.Name = "Arial"
    myRange.Font.Size = 11
    With ActiveDocument.PageSetup
    .Orientation = wdOrientPortrait
    .TopMargin = InchesToPoints(1)
    .BottomMargin = InchesToPoints(1)
    .LeftMargin = InchesToPoints(1.25)
    .RightMargin = InchesToPoints(1.25)
    .Gutter = InchesToPoints(0)
    .HeaderDistance = InchesToPoints(0.5)
    .FooterDistance = InchesToPoints(0.5)
    .PageWidth = InchesToPoints(8.5)
    .PageHeight = InchesToPoints(11)
    .FirstPageTray = wdPrinterDefaultBin
    .OtherPagesTray = wdPrinterDefaultBin
    .SectionStart = wdSectionNewPage
    .OddAndEvenPagesHeaderFooter = False
    .DifferentFirstPageHeaderFooter = False
    .VerticalAlignment = wdAlignVerticalTop
    .SuppressEndnotes = False
    .MirrorMargins = False
    .TwoPagesOnOne = False
    .GutterPos = wdGutterPosLeft
    End With

    Dim atEntry As AutoTextEntry

    For Each atEntry In Application.NormalTemplate.AutoTextEntries
    If atEntry.StyleName = ComboBox1.Value Then
    Selection.Font.Size = 14
    Selection.TypeText Text:=atEntry.Name
    Selection.Font.Size = 10
    Selection.TypeParagraph
    Selection.TypeText atEntry.Value
    Selection.TypeText "" & vbCrLf & ""
    Selection.TypeParagraph
    End If
    Next
    ActiveDocument.PrintOut
    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    End Sub
    Private Sub ComboBox1_Change()
    ListBox1.Clear
    Dim atEntry As AutoTextEntry
    For Each atEntry In Application.NormalTemplate.AutoTextEntries
    If atEntry.StyleName = ComboBox1.Value Then
    ListBox1.AddItem atEntry.Name
    End If
    Next
    End Sub
    Private Sub Userform_Initialize()
    Dim atStyle As Word.Style
    For Each atStyle In ActiveDocument.Styles
    If atStyle.InUse = True Then
    With ComboBox1
    .AddItem (atStyle.NameLocal)
    End With
    End If
    Next

    End Sub

    Private Sub cmdInsert_Click()
    Dim strAT_Name As String
    strAT_Name = ListBox1.Value
    If Selection.Style = ComboBox1.Value Then
    Selection.Collapse Direction:=wdCollapseEnd
    ActiveDocument.AttachedTemplate.AutoTextEntries(strAT_Name).Insert _
    Where:=Selection.Range
    Selection.TypeText " - " & ListBox1.Value
    Else
    MsgBox "The style at the selection does not " & _
    "the style of the selected AutoText."
    End If
    Unload Me
    End Sub
    [/vba]
    Last edited by dchapin; 06-26-2008 at 05:19 AM. Reason: spelling

Posting Permissions

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