View Single Post
 
Old 12-05-2017, 01:47 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

So not a simple form at all? The common sense approach would be simply to have three forms, but who is going to be completing this form?

if you want the form contents all to be contained in a single form then you will need to create a template to contain the macro code and the three alternative 'forms' as autotext entries.

You would then need a bookmark in the body of the template to take the autotext content.

Create a userform to gather the data and the form choice and then a pair of macros to apply the required autotext entry to the bookmark and to write the data collected to bookmarks contained in the inserted autotext entries. e.g. as follows. This only has one data bookmark. You can have as many as you need and call the function for each. If the bookmark doesn't exist the macro to fill that bookmark doesn't do anything.

Code:
Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim orng As Range
    With ActiveDocument
        Set orng = .Bookmarks(strbmName).Range
        Set orng = oTemplate.AutoTextEntries(strAutotext).Insert _
                   (Where:=orng, RichText:=True)
        .Bookmarks.Add Name:=strbmName, Range:=orng
    End With
lbl_Exit:
    Exit Sub
End Sub

Public Sub FillBM(strbmName As String, strValue As String)
'Graham Mayor - http://www.gmayor.com
Dim orng As Range
    With ActiveDocument
        On Error GoTo lbl_Exit
        Set orng = .Bookmarks(strbmName).Range
        orng.Text = strValue
        orng.Bookmarks.Add strbmName
    End With
lbl_Exit:
    Set orng = Nothing
    Exit Sub
End Sub
At its very simplest see the attached template which contains a simple userform, three autotexts and the above macros. Create a new document from the template and make some choices.
Attached Files
File Type: dotm Form Example.dotm (41.0 KB, 12 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote