Class Documents (Word VBA)

A collection of all the Document objects that are currently open in Word. To use a Documents class variable it first needs to be instantiated, for example


Dim docs as Documents
Set docs = Documents

For Each

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


Dim doc As Document
For Each doc In Documents
	
Next doc

Add

Returns a Document object that represents a new, empty document added to the collection of open documents.

Add (Template, NewTemplate, DocumentType, Visible)


Documents.Add

Arguments

Optional arguments

The following arguments are optional

Template (String) - The name of the template to be used for the new document. If this argument is omitted, the Normal template is used.

NewTemplate (Boolean) - True to open the document as a template. The default value is False.

DocumentType (WdNewDocumentType) - Can be one of the following WdNewDocumentType constants: wdNewBlankDocument, wdNewEmailMessage, wdNewFrameset, or wdNewWebPage. The default constant is wdNewBlankDocument.


Possible values are

wdNewBlankDocument Blank document.
wdNewEmailMessage Email message.
wdNewFrameset Frameset.
wdNewWebPage Web page.
wdNewXMLDocument XML document.

Visible (Boolean) - True to open the document in a visible window. If this value is False, Microsoft Word opens the document but sets the Visible property of the document window to False. The default value is True.

AddBlogDocument

Returns a Document object that represents a new blog document that Microsoft Word publishes to the account described by the first three parameters.

This method creates a new document, and it also registers the specified blog account with Word if it is not already registered. In addition, if the PostID parameter is specified, the new document is populated with the contents of the post specified by the value of the PostID parameter, from the provider's Web site.

AddBlogDocument (ProviderID, PostURL, BlogName, PostID)


Dim strProviderID As String: strProviderID = 
Dim strPostURL As String: strPostURL = 
Dim strBlogName As String: strBlogName =  
Dim docAddBlogDocument As Document
Set docAddBlogDocument = Documents.AddBlogDocument(ProviderID:=strProviderID, PostURL:=strPostURL, BlogName:=strBlogName)

Arguments

The following arguments are required:

ProviderID (String) - A GUID that is the unique value a provider uses when they register themselves with Word.

PostURL (String) - The URL that is used to add posts to the blog.

BlogName (String) - A display name for the blog that will be used in Word.

Optional arguments

The following argument is optional

PostID (String) - The ID for an existing post with which to populate the document created by using the AddBlogDocument method.

CanCheckOut

True if Microsoft Word can check out a specified document from a server.

To take advantage of the collaboration features built into Word, documents must be stored on a Microsoft SharePoint Portal Server.

CanCheckOut (FileName)

FileName: The server path and name of the document.


Sub CheckInOut(docCheckOut As String) 
 If Documents.CanCheckOut(docCheckOut) = True Then 
 Documents.CheckOut docCheckOut 
 Else 
 MsgBox "You are unable to check out this document at this time." 
 End If 
End Sub

CheckOut

Copies a specified document from a server to a local computer for editing.

To take advantage of the collaboration features built into Word, documents must be stored on a Microsoft SharePoint Portal Server.

CheckOut (FileName)

FileName: The name of the file to check out.


Sub CheckInOut(docCheckOut As String) 
 If Documents.CanCheckOut(docCheckOut) = True Then 
 Documents.CheckOut docCheckOut 
 Else 
 MsgBox "You are unable to check out this document at this time." 
 End If 
End Sub

Close

Closes the specified documents.

Close (SaveChanges, OriginalFormat, RouteDocument)


Documents.Close

Arguments

Optional arguments

The following arguments are optional

SaveChanges (WdSaveOptions) - Specifies the save action for the document. Can be one of the following WdSaveOptions constants: wdDoNotSaveChanges, wdPromptToSaveChanges, or wdSaveChanges.

Possible return values are wdDoNotSaveChanges - Do not save pending changes, wdPromptToSaveChanges - Prompt the user to save pending changes, wdSaveChanges - Save pending changes automatically without prompting the user.

OriginalFormat (WdOriginalFormat) - Specifies the save format for the document. Can be one of the following WdOriginalFormat constants: wdOriginalDocumentFormat, wdPromptUser, or wdWordDocument.

Possible return values are wdOriginalDocumentFormat - Original document format, wdPromptUser - Prompt user to select a document format, wdWordDocument - Microsoft Word document format.

RouteDocument (Boolean) - True to route the document to the next recipient. If the document doesn't have a routing slip attached, this argument is ignored.

Count

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


Dim lngCount As Long
lngCount = Documents.Count

Item

Returns an individual Document object in a collection.

Item (Index)

Index: One-based index of the object to be returned (Long), or the name of the object (String).


Sub DocumentItem() 
 If Documents.Count >= 1 Then 
 MsgBox Documents.Item(1).Name 
 End If 
End Sub

Open

Opens the specified document and adds it to the Documents collection. Returns a Document object.

Open (FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenAndRepair, DocumentDirection, NoEncodingDialog, XMLTransform)


Sub OpenDoc() 
 Documents.Open FileName:="C:\MyFiles\MyDoc.doc", ReadOnly:=True 
End Sub

Arguments

The following argument is required

FileName (String) - The name of the document (paths are accepted).

Optional arguments

The following arguments are optional

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

ReadOnly (Boolean) - True to open the document as read-only. This argument doesn't override the read-only recommended setting on a saved document. For example, if a document has been saved with read-only recommended turned on, setting the ReadOnly argument to False will not cause the file to be opened as read/write.

AddToRecentFiles (Boolean) - True to add the file name to the list of recently used files at the bottom of the File menu.

PasswordDocument (String) - The password for opening the document.

PasswordTemplate (String) - The password for opening the template.

Revert (Boolean) - Controls what happens if FileName is the name of an open document. True to discard any unsaved changes to the open document and reopen the file. False to activate the open document.

WritePasswordDocument (String) - The password for saving changes to the document.

WritePasswordTemplate (String) - The password for saving changes to the template.

Format (FileConverter) - The file converter to be used to open the document. Can be one of the WdOpenFormat constants. The default value is wdOpenFormatAuto. To specify an external file format, apply the OpenFormat property to a FileConverter object to determine the value to use with this argument.

Encoding (String) - The document encoding (code page or character set) to be used by Microsoft Word when you view the saved document. Can be any valid MsoEncoding constant. For the list of valid MsoEncoding constants, see the Object Browser in the Visual Basic Editor. The default value is the system code page.

Visible (Boolean) - True if the document is opened in a visible window. The default value is True.

OpenAndRepair (Boolean) - True to repair the document to prevent document corruption.

DocumentDirection (WdDocumentDirection) - Indicates the horizontal flow of text in a document. The default value is wdLeftToRight.

Possible return values are wdLeftToRight - Left to right, wdRightToLeft - Right to left.

NoEncodingDialog (Boolean) - True to skip displaying the Encoding dialog box that Word displays if the text encoding cannot be recognized. The default value is False.

XMLTransform - String

OpenNoRepairDialog

Opens the specified document and adds it to the Documents collection.

OpenNoRepairDialog (FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenAndRepair, DocumentDirection, NoEncodingDialog, XMLTransform)


Sub OpenDoc() 
 Documents.OpenNoRepairDialog FileName:="C:\MyFiles\MyDoc.doc", ReadOnly:=True 
End Sub

Arguments

The following argument is required

FileName (String) - The name of the document (paths are accepted).

Optional arguments

The following arguments are optional

ConfirmConversions (Boolean) - True to display the Convert File dialog box if the file is not in Microsoft Word format.

ReadOnly (Boolean) - True to open the document as read-only. This argument does not override the read-only recommended setting on a saved document. For example, if a document has been saved with read-only recommended turned on, setting the ReadOnly argument to False will not cause the file to be opened as read/write.

AddToRecentFiles (Boolean) - True to add the file name to the list of recently used files at the bottom of the File menu.

PasswordDocument (String) - The password for opening the document.

PasswordTemplate (String) - The password for opening the template.

Revert (Boolean) - Controls what happens if FileName is the name of an open document. True to discard any unsaved changes to the open document and reopen the file. False to activate the open document.

WritePasswordDocument (String) - The password for saving changes to the document.

WritePasswordTemplate (String) - The password for saving changes to the template.

Format (WdOpenFormat) - The file converter to be used to open the document. Can be one of the WdOpenFormat constants. The default is wdOpenFormatAuto.

Here you can find possible values for WdOpenFormat

Encoding (String) - The document encoding (code page or character set) to be used by Word when you view the saved document. Can be any valid MsoEncoding constant. For the list of valid MsoEncoding constants, see the Object Browser in the Visual Basic Editor. The default is the system code page.

Visible (Boolean) - True if the document is opened in a visible window. The default is True.

OpenAndRepair (Boolean) - True to repair the document to prevent document corruption.

DocumentDirection (WdDocumentDirection) - Indicates the horizontal flow of text in a document. Can be any valid WdDocumentDirection constant. The default is wdLeftToRight.

Possible return values are wdLeftToRight - Left to right, wdRightToLeft - Right to left.

NoEncodingDialog (Boolean) - True to skip displaying the Encoding dialog box that Word displays if the text encoding cannot be recognized. The default is False.

XMLTransform (String) - Specifies a transform to use.

Save

Saves all the documents in the Documents collection.

If a document has not been saved before, the Save As dialog box prompts the user for a file name.

Save (NoPrompt, OriginalFormat)


Documents.Save

Arguments

Optional arguments

The following arguments are optional

NoPrompt (Boolean) - True to have Word automatically save all documents. False to have Word prompt the user to save each document that has changed since it was last saved.

OriginalFormat (WdOriginalFormat) - Specifies the way the documents are saved. Can be one of the WdOriginalFormat constants.

Possible return values are wdOriginalDocumentFormat - Original document format, wdPromptUser - Prompt user to select a document format, wdWordDocument - Microsoft Word document format.