Documents.Open Method
| Word Developer Reference |
Syntax
expression.Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenConflictDocument, OpenAndRepair, DocumentDirection, NoEncodingDialog)
expression Required. A variable that represents a Documents collection.
Parameters
| Name | Required/Optional | Data Type | Description |
|---|---|---|---|
| FileName | Required | Variant | The name of the document (paths are accepted). |
| ConfirmConversions | Optional | Variant | True to display the Convert File dialog box if the file isn't in Microsoft Word format. |
| ReadOnly | Optional | Variant | 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 | Optional | Variant | True to add the file name to the list of recently used files at the bottom of the File menu. |
| PasswordDocument | Optional | Variant | The password for opening the document. |
| PasswordTemplate | Optional | Variant | The password for opening the template. |
| Revert | Optional | Variant | 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 | Optional | Variant | The password for saving changes to the document. |
| WritePasswordTemplate | Optional | Variant | The password for saving changes to the template. |
| Format | Optional | Variant | The file converter to be used to open the document. Can be one of the WdOpenFormat constants. The default value is wdOpenFormatAuto. |
| Format | Required | Variant | The file converter to be used to open the document. Can be one of the following WdOpenFormat constants. To specify an external file format, apply the OpenFormat property to a FileConverter object to determine the value to use with this argument. |
| Encoding | Optional | Variant | 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 | Optional | Variant | True if the document is opened in a visible window. The default value is True. |
| OpenConflictDocument | Optional | Variant | Specifies whether to open the conflict file for a document with an offline conflict. |
| OpenAndRepair | Optional | Variant | True to repair the document to prevent document corruption. |
| DocumentDirection | Optional | WdDocumentDirection | Indicates the horizontal flow of text in a document. The default value is wdLeftToRight. |
| NoEncodingDialog | Optional | Variant | True to skip displaying the Encoding dialog box that Word displays if the text encoding cannot be recognized. The default value is False. |
Return Value
Document
Security
Avoid using hard-coded passwords in your applications. If a password is required in a procedure, request the password from the user, store it in a variable, and then use the variable in your code. For recommended best practices on how to do this, see Security Notes for Microsoft Office Solution Developers.Example
This example opens MyDoc.doc as a read-only document.
| Visual Basic for Applications |
|---|
|
This example opens Test.wp using the WordPerfect 6.x file converter.
| Visual Basic for Applications |
|---|
|
Community Content
Thomas Lee
Format is incorrectly listed twice
Format is shown twice here, once as optional and once as required:
FormatOptional - Variant - The file converter to be used to open the document. Can be one of the WdOpenFormat constants. The default value is wdOpenFormatAuto.
FormatRequired - Variant - The file converter to be used to open the document. Can be one of the following WdOpenFormat constants. To specify an external file format, apply the OpenFormat property to a FileConverter object to determine the value to use with this argument.
FormatOptional - Variant - The file converter to be used to open the document. Can be one of the WdOpenFormat constants. The default value is wdOpenFormatAuto.
FormatRequired - Variant - The file converter to be used to open the document. Can be one of the following WdOpenFormat constants. To specify an external file format, apply the OpenFormat property to a FileConverter object to determine the value to use with this argument.
Nilpo
VBScript Example
blnOpenReadOnly = True
Documents.Open("C:\MyFiles\MyDoc.doc",, blnOpenReadOnly)
RWatson
C# implementation
Can anyone explain why this method requires "ref object" for every parameter? Similar actions in Excel are not done this way. This needlessly complicates any coding and significantly reduces reusability.
C# would go something like this:
Is this a hold over from a super old version? If so, can we get an Open2 that is clean?
C# would go something like this:
using Word = Microsoft.Office.Interop.Word;Why is this needed? Excel's open is not all ref. Clearly I'm not going to check to see if my password was changed. This is needlessly over complicated.
using System.Reflection;
...
object objMissing = Missing.Value;
object objFilename = @"C:\YourFile.doc";
object objConfirmConversions = false;
object objReadOnly = false;
object objAddToRecentFiles = false;
object objPasswordDocument = Missing.Value;
... [Insert object creations for every used parameter and probably an objMissing = Missing.value as a generic ] ...
Word.Application wTemp = new Microsoft.Office.Interop.Word.Application();
Word.Document dInstructions = wTemp.Documents.Open(objFilename, objConfirmConversions, objReadOnly,
objAddToRecentFiles, objPasswordDocument, objPasswordTemplate, objRevert,
objWritePasswordDocument, objWritePasswordTemplate, objFormat, objENcoding,
objVisible, objOpenAndRepair, objDocumentDirection, objNoEncodingDialog, objXMLTransform);
Is this a hold over from a super old version? If so, can we get an Open2 that is clean?