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. To use a Subdocument class variable it first needs to be instantiated, for example
Dim sbd as Subdocument
Set sbd = ActiveDocument.Subdocuments(Index:=1)
For Each
Here is an example of processing the Subdocument items in a collection.
Dim subdoc As Subdocument
For Each subdoc In ActiveDocument.Subdocuments
subdoc.Range.Select
If subdoc.HasFile = True Then
MsgBox subdoc.Path & Application.PathSeparator & subdoc.Name
Else
MsgBox "This subdocument has not been saved."
End If
Next subdoc
Delete
Deletes the specified subdocument.
ActiveDocument.Subdocuments(1).Delete
HasFile
True if the specified subdocument has been saved to a file.
Dim subLoop As Subdocument
For Each subLoop In ActiveDocument.Subdocuments
subLoop.Range.Select
If subLoop.HasFile = True Then
MsgBox subLoop.Path & Application.PathSeparator _
& subLoop.Name
Else
MsgBox "This subdocument has not been saved."
End If
Next subLoop
Level
Returns the heading level used to create the subdocument.
i = 1
If ActiveDocument.Subdocuments.Count > = 1 Then
For each s in ActiveDocument.Subdocuments
MsgBox "The heading level for SubDoc " & i _
& " is " & s.Level
i = i + 1
Next s
Else
MsgBox "There are no subdocuments defined."
End If
Locked
True if a subdocument in a master document is locked.
If ActiveDocument.Subdocuments(1).Locked = True Then
ActiveDocument.Protect Type:=wdAllowOnlyComments
End If
Name
Returns the name of the specified object.
Dim strName As String
strName = ActiveDocument.Subdocuments(1).Name
Open
Opens the specified subdocument. Returns a Document object that represents the opened subdocument.
Dim doc As Document
Set doc = ActiveDocument.Subdocuments(1).Open()
Path
Returns the disk or Web path to the specified subdocument.
The path doesn't include a trailing character — for example, "C:\MSOffice" or "https://MyServer". Use the PathSeparator property to add the character that separates folders and drive letters. Use the Name property to return the file name without the path.
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.
For information about returning a range from a document, see the Range method.
Dim rngRange As Range
Set rngRange = ActiveDocument.Subdocuments(1).Range
Split
Divides an existing subdocument into two subdocuments at the same level in master document view or outline view.
The division is at the beginning of the specified range. An error occurs if the document isn't in either master document or outline view or if the range isn't at the beginning of a paragraph in a subdocument.
Split (Range)
Range: The range that, when the subdocument is split, becomes a separate subdocument.
Selection.Range.Subdocuments(1).Split Range:=Selection.Range