Class Subdocument (Word VBA)

The class Subdocument represents a subdocument within a document or range. The Subdocument object is a member of the Subdocuments collection. The Subdocuments collection includes all the subdocuments in the a range or document.

The main procedures of class Subdocument are Delete, Open, Subdocuments.AddFromFile, Subdocuments.AddFromRange, Subdocuments.Delete and Subdocuments.Select


To use a Subdocument class variable it first needs to be instantiated, for example

Dim sbd as Subdocument
Set sbd = ActiveDocument.Subdocuments(Index:=1)

The following procedures can be used to set variables of type Subdocument: Subdocuments.Item, Subdocuments.AddFromFile, Subdocuments.AddFromRange, Document.Subdocuments and Range.Subdocuments

For Each

Here is an example of processing the Subdocument items in a collection.

Dim subdoc As Subdocument 
For Each subdoc In ActiveDocument.Subdocuments 
 If subdoc.HasFile = True Then 
 MsgBox subdoc.Path & Application.PathSeparator  & subdoc.Name 
 MsgBox "This subdocument has not been saved." 
 End If 
Next subdoc


The following procedures in this class have been grouped together and are described on a separate theme page

Add with its procedures Subdocuments.AddFromFile and Subdocuments.AddFromRange


These are the main methods of the Subdocument class

Delete - Deletes the specified subdocument.


Open - Opens the specified subdocument. Returns a Document object that represents the opened subdocument.

Dim doc As Document
Set doc = ActiveDocument.Subdocuments(1).Open()

Subdocuments.Delete - Deletes the collection of subdocuments.


Subdocuments.Select - Selects the specified subdocument.


Other Methods

Subdocuments.Merge - Merges the specified subdocuments of a master document into a single subdocument.



HasFile true if the specified subdocument has been saved to a file.

Dim booHasFile As Boolean
booHasFile = ActiveDocument.Subdocuments(1).HasFile

Level returns the heading level used to create the subdocument.

Dim lngLevel As Long
lngLevel = ActiveDocument.Subdocuments(1).Level

Locked true if a subdocument in a master document is locked.

ActiveDocument.Subdocuments(1).Locked = True

Name returns the name of the specified object.

Dim strName As String
strName = ActiveDocument.Subdocuments(1).Name

Parent returns an object that represents the parent object of the specified Subdocument object.

Dim objParent As Object
Set objParent = ActiveDocument.Subdocuments(1).Parent

Path returns the disk or Web path to the specified subdocument.

Dim strPath As String
strPath = ActiveDocument.Subdocuments(1).Path

Range returns a Range object that represents the portion of a document that's contained within the subdocument.

Dim rngRange As Range
Set rngRange = ActiveDocument.Subdocuments(1).Range

Subdocuments.Count returns a Long that represents the number of subdocuments in the collection.

Dim lngCount As Long
lngCount = ActiveDocument.Subdocuments.Count

Subdocuments.Expanded true if the subdocuments in the specified document are expanded.

ActiveDocument.Subdocuments.Expanded = True

Subdocuments.Parent returns an object that represents the parent object of the specified Subdocuments object.

Dim objParent As Object
Set objParent = ActiveDocument.Subdocuments.Parent