How to Insert Text in Word Document Using VBA

--

Share post:

VBA (Visual Basic for Applications) is a programming language that lets you automate tasks in Microsoft Office applications, including Word. With VBA, you can create macros, functions, and procedures that can manipulate objects in Word documents, such as text, paragraphs, tables, and images.

One of the frequently performed tasks with VBA is to insert text in a Word . There are several ways to do this, depending on the position and of the text you want to insert. In this article, we will discuss some commonly used methods to insert text in a Word document using a VBA.

A. Using Object Selection

A Selection object is a VBA object that represents the current selection in a Word document. You can use this object to enter text at the current cursor position, or at the position you specify by using the Select or GoTo methods.

To enter text by using the Selection object, you can use either the Text property or the TypeText method. The Text property returns or sets the currently selected text, while the TypeText method types the text as if you were typing it from the keyboard.

Here is a sample VBA code that uses the Selection object to insert the text “Hello World” at the beginning of a Word document:

Sub InsertTextWithSelection()
    ActiveDocument.Activate
    Selection.HomeKey Unit:=wdStory
    Selection.Text = "Hello World"
End Sub

Here is a sample VBA code that uses the Selection object to insert the text “Hello World” at the end of a Word document:

Sub InsertTextWithSelection()
    ActiveDocument.Activate
    Selection.EndKey Unit:=wdStory
    Selection.TypeText Text:="Hello World"
End Sub

B. Using Range Objects

A Range object is a VBA object that represents a continuous part of a Word document. You can use this object to enter text at a specific location in the document or replace existing text with new text.

To insert text by using a Range object, you can use the Text property or the InsertBefore or InsertAfter methods. The Text property returns or sets text within a specified range, while the InsertBefore and InsertAfter methods insert text before or after a specified range.

Here is a sample VBA code that uses a Range object to insert text “Hello World” at the beginning of a Word document:

Sub InsertTextWithRange()
    ActiveDocument.Activate
    Dim rng As Range
    Set rng = ActiveDocument.Range(Start:=0, End:=0)
    rng.Text = "Hello World"
End Sub

Here is a sample VBA code that uses a Range object to insert the text “Hello World” at the end of a Word document:

Sub InsertTextWithRange()
    ActiveDocument.Activate
    Dim rng As Range
    Set rng = ActiveDocument.Range(Start:=ActiveDocument.Content.End - 1, End:=ActiveDocument.Content.End)
    rng.InsertAfter Text:="Hello World"
End Sub

C. Using Document Objects

A Document object is a VBA object that represents an open Word document. You can use this object to insert text in a Word document by using the Content or Paragraphs methods.

The Content method returns a Range object that represents the entire contents of a Word document, while the Paragraphs method returns a collection of objects that represent each paragraph in the Word document.

Here is a sample VBA code that uses a Document object to insert the text “Hello World” at the beginning of a Word document:

Sub InsertTextWithDocument()
    ActiveDocument.Activate
    ActiveDocument.Content.Text = "Hello World"
End Sub

Here is a sample VBA code that uses a Document object to insert the text “Hello World” at the end of a Word document:

Sub InsertTextWithDocument()
    ActiveDocument.Activate
    ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.InsertAfter Text:="Hello World"
End Sub

Conclusion

In this article, we have discussed several ways to insert text in a Word document using VBA. You can choose the method that best suits your needs, depending on the position and format of the text you want to insert. We have also provided some sample VBA codes that you can try yourself or modify to your liking.

Hope this article will be useful for those of you who want to learn more about how to utilize VBA to automate tasks in Word. Good luck!

RELATED ARTICLES

How to Create Attractive Text with Text Decoration in MS Word

Do you want to create text that grabs the reader's attention by using various effects such as underlines,...

How to Compare Two Documents in Microsoft Word Easily and Quickly

Have you ever wanted to compare two documents in Microsoft Word to see the differences or similarities between...

How to Zoom in or out of Microsoft Word Documents Easily and Quickly

Have you ever found it difficult to read or edit a Microsoft Word document because the text size...

How to Make a Professional Proposal with Microsoft Word

A proposal is a document that contains a plan of activities, research, or projects that a person or...