Option Explicit
'12345678901234567890123456789012345bopoh13@ya67890123456789012345678toster56789
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
.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
Wend
'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
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.
- Open MS Word, the text to write to token [Cap]
- To open VBA in the application (Alt+F11)
- Select the menu Insert -> Module
- Insert procedure (from p. 1)
- Press F5 (start a procedure with a cursor)
Find more questions by tags MacrosVisual BasicWord
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