How to insert text field in word code#
To use this example, run the code from the ThisDocument or ThisAddIn class in your project. The following example shows the complete code. However, when it comes to inserting and manipulating text, the Range object offers you more control. You can also use the TypeBackspace method of the Selection object, which mimics the functionality of the Backspace key on your keyboard. If the selection is not an insertion point or a block of selected text, then the code in the Else block does nothing. Object direction = ĬurrentSelection.Collapse(ref direction) ĬurrentSelection.TypeText("Inserting before a text block. TypeText("Inserting before a text block. If it is, the code uses the Collapse method of the selection to collapse the selection to an insertion point at the start of the selected block of text. If it is, then another If block tests to see whether the ReplaceSelection option is turned on. The code in the ElseIf block tests to see whether the selection is a normal selection. If (currentSelection.Type = )ĬurrentSelection.TypeText("Inserting at insertion point.
Test to see if selection is an insertion point. ' Test to see if selection is an insertion point. If it is, the code inserts a sentence using TypeText, and then a paragraph mark using the TypeParagraph method. Test to see whether the current selection is an insertion point.
Turn off the Overtype option if it is turned on. Word.Selection currentSelection = Application.Selection Dim currentSelection As Word.Selection = Application.Selection To insert text using the TypeText methodĭeclare a Selection object variable. If the Overtype option is activated, then any text next to the cursor is overwritten. The code in the following procedure declares a Selection object variable, and turns off the Overtype option if it is turned on. TypeText behaves differently depending on the options set on the user's computer. The TypeText method inserts text at the selection. Replace those characters with the string New Text. Dim rng As Word.Range = Me.(Start:=0, End:=12) The following code example can be used in a VSTO Add-in. Dim rng As Word.Range = Me.Range(Start:=0, End:=12) The following code example can be used in a document-level customization. To replace text in a rangeĬreate a Range object that consists of the first 12 characters in the document. If the specified range contains text, all text in the range is replaced with the inserted text. Select the Range object, which has expanded from one character to the length of the inserted text. Dim rng As Word.Range = Me.(Start:=0, End:=0) Word.Range rng = this.Range(ref start, ref end) Dim rng As Word.Range = Me.Range(Start:=0, End:=0) Specify a range at the beginning of a document and insert the text New Text. Use the Text property of a Range object to insert text in a document. Office Add-ins have a small footprint compared to VSTO Add-ins and solutions, and you can build them by using almost any web programming technology, such as HTML5, JavaScript, CSS3, and XML. Interested in developing solutions that extend the Office experience across multiple platforms? Check out the new Office Add-ins model.