How to create a generic letter template in Word?

Good day!
Have the letter prepared as follows:00c95881efb9450daa22c12607aa97fe.png

You need to make a template which would have the ability to:
  1. Fill in all yellow fields
  2. Of the insertion unit # 1(red rectangle) up to 3 times in a row
  3. Exception of block # 2 (blue rectangle) i.e. to remove from the template letters in cases of absence of annexes to the letter

With all this to block access to edit the main (black) text
Question: How does this implement? I think that VBA is quite to cope with it, I think as the script to run the machine, i.e. that the user did not press additional buttons of type "test document" to run the script.

Please throw off links to information on this topic, I'd appreciate it.
July 9th 19 at 13:09
2 answers
July 9th 19 at 13:11
  1. You can mark the yellow text and make the change directly in Word (in example 1, replacing in the entire text)
    or to use a tool to Merge and take data from an Excel file (without VBA). When merging links can fail + final document should Unite (as the current record) so that when a document field is not updated (Merge is not recommended).
    Option Explicit
    Sub Replacement_tags()
     Dim Content_Find As Find ' The Find Object
     Set Content_Find = ActiveDocument.Content.Find
     With Content_Find ' Find the marker
     .ClearFormatting: .Replacement.ClearFormatting ' Clear format
     .MatchWildcards = False ' IMPORTANT! To disable Wildcards
     .Text = "[Cap]": .Replacement.Text = "the Head of Department..."
     .Execute Forward:=True, Replace:=wdReplaceAll ' Replace all markers [Cap]
     'The trick: move the cursor to the end of the replaced text
     .Execute FindText:=.Replacement.Text, _
     Forward:=True, Wrap:=wdFindStop, Replace:=wdReplaceNone
     .Parent.Select ' select the text to move the cursor
     'Selection.Collapse Direction:=wdCollapseEnd ' the cursor at the end was found
     End With
    End Sub
  2. You can just replace the duplicate text:
    .Replacement.Text = "the Head of Department..." & String(3, Chr(13)) & "to the Head of Department..."
    or enter the number of duplicates in the variable Text before replacement is:
    'Chr(13) code of carriage return. In other languages, the more familiar sequence "\r"
     CapText = "the Head of Department..."
     CapСount = InputBox("Enter the number of 'Caps'", , 1)
     If Not CInt(Val(CapСount)) = CapСount Then _
     MsgBox "Enter a whole number", vbCritical: Exit Sub _
     Else Text = CapText
     While CapСount > 1
     Text = Text & String(3, Chr(13)) & CapText
     CapСount = CapСount - 1
  3. The table can also be tightened via the Merge (or rather there is created the OLE object, which is buggy more than the normal fields, mostly the retention of the Excel file where the data comes from). The software to create the table:
    'Use the trick from p. 1 if you plan to create a table place marker
     Set tblNew = ActiveDocument.Tables.Add(Selection.Range, 3, 2)
     With tblNew
     .Columns(1).PreferredWidth = CentimetersToPoints(4) ' Column 1, Width is 4 cm
     .Borders(wdBorderTop).LineStyle = wdLineStyleNone ' the edge of the table, for example
     .Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
     .Borders(wdBorderVertical).LineStyle = wdLineStyleDot
     For i = 1 To 2
     .Cell(1, i).Range.Text = "Line 1; #" & i
     Next i
     .Cell(1, 1).Range.InsertAfter Chr(13) & "Line 2" ' Add text
     .Cell(tblNew.Rows.count tblNew.Columns.count).Range.InsertAfter "Last cell"
     End With
  4. Below the main text it was impossible to edit, the easiest way to create a Word template with macro support, the basic procedure (startup) to place in a module the ThisDocument:
    Private Sub Document_Open()
     Replacement_tags ' the Procedure of claim 1
    End Sub
    Data to populate the template can be placed in a separate file.

How to use the debugger:
  1. Open MS Word, the text to write to token [Cap]
  2. To open VBA in the application (Alt+F11)
  3. Select the menu Insert -> Module
  4. Insert procedure (from p. 1)
  5. Press F5 (start a procedure with a cursor)
July 9th 19 at 13:13
Easy to cope with the described problems of the given product
He recently did a project in the designer. Part of the document template is generated from the data in the record, the rest is extruded from blanks-templates (templates in word)which are attached in turn as attachments to the record. Thus a rich document generator.

Templates blanks also tend to select data within the document:
Much easier (but not effective) tool Merge use (and free) than on the lists to click ;) - Hans56 commented on July 9th 19 at 13:16
: thank you for the option, didn't know - Retta commented on July 9th 19 at 13:19
It is also totally free.
In fact, given for example the Rune is quite complex. All of what he says - is done in the constructor is very simple and fast, without writing a single line of code. - Justice_Beer commented on July 9th 19 at 13:22
Cool if so. Even if link left for example ;) What is the complexity of the task set? - Hans56 commented on July 9th 19 at 13:25

Find more questions by tags MacrosVisual BasicWord