Class Subdocuments (Word VBA)

A collection of Subdocument objects that represent the subdocuments in a range or document. To use a Subdocuments class variable it first needs to be instantiated, for example


Dim sbds as Subdocuments
Set sbds = ActiveDocument.Subdocuments

For Each

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


Dim sbd As Subdocument
For Each sbd In ActiveDocument.Subdocuments
	
Next sbd

AddFromFile

Adds the specified subdocument to the master document at the start of the selection and returns a Subdocument object.

If the active view isn't either outline view or master document view, an error occurs.

AddFromFile (Name, ConfirmConversions, ReadOnly, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate)


ActiveDocument.ActiveWindow.View.Type = wdMasterView 
ActiveDocument.Subdocuments.AddFromFile _ 
 Name:="C:\Subdoc.doc"

Arguments

The following argument is required

Name (String) - The file name of the subdocument to be inserted into the master document.

Optional arguments

The following arguments are optional

ConfirmConversions (Boolean) - True to confirm file conversion in the Convert File dialog box if the file isn't in Word format.

ReadOnly (Boolean) - True to insert the subdocument as a read-only document.

PasswordDocument (String) - The password required to open the subdocument if it is password protected.

PasswordTemplate (String) - The password required to open the template attached to the subdocument if the template is password protected.

Revert (Boolean) - Controls what happens if Name is the file name of an open document. True to insert the saved version of the subdocument. False to insert the open version of the subdocument, which may contain unsaved changes.

WritePasswordDocument (Document) - The password required to save changes to the document file if it is write-protected.

WritePasswordTemplate (String) - The password required to save changes to the template attached to the subdocument if the template is write-protected.

AddFromRange

Creates one or more subdocuments from the text in the specified range and returns a SubDocument object.

The Range argument must begin with one of the built-in heading level styles (for example, Heading 1). Subdocuments are created at each paragraph formatted with the same heading format used at the beginning of the range. Subdocument files are saved when the master document is saved and are automatically named using text from the first line in the file.

AddFromRange (Range)

Range: The range used to create one or more subdocuments.


ActiveDocument.ActiveWindow.View.Type = wdMasterView 
ActiveDocument.SubDocuments.AddFromRange Range:=Selection.Range

Count

Returns a Long that represents the number of subdocuments in the collection.


Dim lngCount As Long
lngCount = ActiveDocument.Subdocuments.Count

Delete

Deletes the collection of subdocuments.


ActiveDocument.Subdocuments.Delete

Expanded

True if the subdocuments in the specified document are expanded.


If ActiveDocument.Subdocuments.Count >= 1 Then 
 ActiveDocument.Subdocuments.Expanded = True 
End If

Item

Returns an individual Subdocument object in a collection.

Item (Index)

Index: The individual object to be returned. Can be a Long indicating the ordinal position of the individual object.


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

Merge

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

Merge (FirstSubdocument, LastSubdocument)


If ActiveDocument.Subdocuments.Count >= 2 Then 
 Set aDoc = ActiveDocument 
 aDoc.Subdocuments.Merge _ 
 FirstSubdocument:=aDoc.Subdocuments(1), _ 
 LastSubdocument:=aDoc.Subdocuments(2) 
End If

Arguments

Optional arguments

The following arguments are optional

FirstSubdocument (String) - The path and file name of the original document you want to merge revisions with.

LastSubdocument (String) - The last subdocument in a range of subdocuments to be merged.

Select

Selects the specified subdocument.

After using this method, use the Selection object to work with the selected items. For more information, see Working with the Selection Object.


ActiveDocument.Subdocuments.Select