
'Move selectio to the end of the documentĪ = wdOutlineView
#VBA SUB INSERT TEXT IN WORD DOCUMENT CODE#
Upon activating the Outline View the document view turns as shown below: Code example Public Sub InsertSubDocument()
Locate Outline button under Views group as shown below to activate Outline view. Note: to perform AddFromFile method the current document must be in Outline Mode as shown below else code will throw error. AddFromFileįollowing is the file which I saved as “ example.docx” and will insert into current document using code: Microsoft Word VBA (Visual Basic for Applications) Subdocuments object which exposes all available Subdocuments within a document as collection. Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.Sub document can be coming from external file or a range (coming from different section with the same document). Set rngFirstParagraph = ActiveDocument.Paragraphs(1).Range The following example deletes the first paragraph in the active document and inserts new text. You can also use the Delete method or the Selection object or the Range object to delete existing text, and then insert new text using the InsertAfter method or the InertBefore method. The following instruction changes the first word in the active document by setting the Text](./././api/) property of a Range object to "The ". You can change existing text by changing the contents of a range. For more information, see Working with the Selection object or Working with Range objects. Use the GetRange method to redefine an existing Selection object or Range object. RngParagraphs.MoveEnd Unit:=wdParagraph, Count:=2 Set rngParagraphs = ActiveDocument.Paragraphs(1).Range The following example uses the MoveEnd](./././api/) method of the Range object to extend the range to include the first three paragraphs in the active document. The MoveLeft, MoveRight, MoveUp, and Move Down methods can also be used to extend a Selection object. The following example uses the MoveEnd](./././api/) method of the Selection object to extend the end of the selection to include three additional words. The following example cancels the range to its ending point (after the first word) and adds new text. Selection.Collapse Direction:=wdCollapseStart
The following example collapses the selection to an insertion point at the beginning of the selection. Use the Collapse method to collapse a Selection object or a Range object to its beginning or ending point.
If Selection.Type = wdSelectionIP Then MsgBox "Nothing is selected" The following example displays a message if the selection is an insertion point. The Type](./././api/) property of the Selection](./././api/) object returns information about the type of selection. Returning text from a document Selecting text in a document Inserting text in a document Manipulating a portion of a document Determine whether text is selected This topic includes Visual Basic examples related to the tasks identified in the following sections.ĭetermine whether text is selected Collapse a selection or range Extend a selection or range Redefine a selection or range Change textįor information about, and examples of, other editing tasks, see the following topics: