Ugent - VBA Autotext and Menu from Template

G

GJ

Deadline is in 12 hours and I am trying to build a template that:
-Has standard autotext entries stored in it.
-Has bookmarks as placeholders for inserting autotext entries
-Displays a custom menu when a document is based on the template
-Menu items will insert the relevant autotext entry immediately before the
matching bookmark in the document

I hacked code to create and display a menu. Everything works when the
autotext is in the normal.dot and the code is in the document. But I need
to use both the code/menu and autotext in the template. When a new
document is created from the template I have not succeeded in making the
menu display when a new document is created.


Thanks,
Gavin

Here is the code I have created to date (excluding code behind one simple
form) that just calls one of the subs, eg
s_insert_auto 5, "Objective","obj_end"


Sub autoexec()

'Dim new_doc As Boolean
'Dim objProperty As Office.DocumentProperty

'Check new_form property
s_delete_menu "&PAF"
s_create_menu "&PAF"

'new_doc = False

'For Each objProperty In ActiveDocument.CustomDocumentProperties
'    If objProperty.Name = "new_agreement" Then
'        new_doc = objProperty.Value
'        Exit For
'    End If
'Next

'If new_doc Then
'    objProperty.Value = False
'    s_openform
'End If

End Sub


Sub autoclose()

s_delete_menu "PAF"

End Sub


Sub s_insert_auto(i As Integer, s_auto As String, s_bookmark)

's_insert_auto 5, "Objective","obj_end"
's_insert_auto 3 "Competancy","comp_end"

Dim j As Integer

Selection.GoTo What:=wdGoToBookmark, Name:=s_bookmark
Selection.MoveLeft Count:=1

For j = 1 To i
   ' Selection.TypeParagraph
    Selection.TypeText Text:=s_auto
    Selection.Range.InsertAutoText
Next

End Sub


Sub s_obj()
s_insert_auto 1, "objective", "obj_end"
End Sub

Sub s_comp()
s_insert_auto 1, "competency", "comp_end"
End Sub

Sub s_openform()

new_doc_form.Show

End Sub

Sub s_new_paf()

    Documents.Add DocumentType:=wdNewBlankDocument

    With ActiveDocument.PageSetup
        .Orientation = wdOrientPortrait
        .TopMargin = CentimetersToPoints(2.03)
        .BottomMargin = CentimetersToPoints(2.03)
        .LeftMargin = CentimetersToPoints(2.5)
        .RightMargin = CentimetersToPoints(2.5)
        .Gutter = CentimetersToPoints(0)
        .HeaderDistance = CentimetersToPoints(1)
        .FooterDistance = CentimetersToPoints(1)
        .PageWidth = CentimetersToPoints(21.59)
        .PageHeight = CentimetersToPoints(27.94)
        .SectionStart = wdSectionNewPage
        .DifferentFirstPageHeaderFooter = False
        .VerticalAlignment = wdAlignVerticalTop
    End With

    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If
    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.TypeText Text:="paf_head"
    Selection.Range.InsertAutoText
    Selection.TypeBackspace

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Selection.TypeText Text:="paf_foot"
    Selection.Range.InsertAutoText
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    Selection.TypeText Text:="paf_body"
    Selection.Range.InsertAutoText

    'Call the form for multiple insertions
    s_openform

End Sub



Option Explicit
Public oMenu As CommandBarPopup
Public oButton As CommandBarButton
Public oBar As CommandBar

Sub s_create_menu(xMenu As String)

Dim i As Long, c As CommandBarControl

CustomizationContext = ThisDocument

'Delete any previous/leftover menu of the same name
Call s_delete_menu(xMenu)

'MAKE A NEW MENU:
Set oMenu = CommandBars("Menu Bar").Controls.Add(Type:=msoControlPopup)
With oMenu
    .Caption = xMenu
    .Visible = True
End With

Call AddMenuItem("&New PAF", "s_new_paf")
Call AddMenuItem("Add &Objective", "s_obj")
Call AddMenuItem("Add &Competency", "s_comp")
Call AddMenuItem("Add &Multiple", "s_openform")

ThisDocument.Saved = True

Set oMenu = Nothing

End Sub

Sub AddMenuItem(xCaption As String, xAction As String, Optional xBeginGroup
As Boolean = False)

Set oButton = oMenu.Controls.Add(Type:=msoControlButton)
With oButton
..Caption = xCaption
..Style = msoButtonCaption
..OnAction = xAction
..BeginGroup = xBeginGroup
End With
End Sub


Sub s_delete_menu(xMenu As String)
Dim i As Long, c As CommandBarControl

For i = CommandBars("Standard").Controls.Count To 1 Step -1
    If CommandBars("Standard").Controls(i).Caption = xMenu Then c.Delete
Next i

Set oBar = CommandBars("Menu Bar")

For Each c In oBar.Controls
If c.Caption = xMenu Then c.Delete
Next c
End Sub
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < GJ > écrivait :
In this message, < GJ > wrote:

|| Deadline is in 12 hours and I am trying to build a template that:
|| -Has standard autotext entries stored in it.
|| -Has bookmarks as placeholders for inserting autotext entries
|| -Displays a custom menu when a document is based on the template
|| -Menu items will insert the relevant autotext entry immediately before
the
|| matching bookmark in the document
||
|| I hacked code to create and display a menu. Everything works when the
|| autotext is in the normal.dot and the code is in the document. But I need
|| to use both the code/menu and autotext in the template. When a new
|| document is created from the template I have not succeeded in making the
|| menu display when a new document is created.
||
||
|| Thanks,
|| Gavin
||
|| Here is the code I have created to date (excluding code behind one simple
|| form) that just calls one of the subs, eg
|| s_insert_auto 5, "Objective","obj_end"
||
||
|| Sub autoexec()
||

Try changing the sub name to AutoNew

Autoexec fires when Word is launched, not when a new document based on a
template is created.

By the way, you can use the Document_New, Document_Close and Document_Open
events in the ThisDocument module of your template project.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top