Opening Documents

You can use the Open method of the Documents collection to open an existing document.
This will open a document and will add it to the Documents collection.
The Format argument corresponds to the file converter to be used to open the document.

Application.Documents.Open FileName:="C:\Temp\document.doc", _ 
                           ConfirmConversions:=False, _
                           ReadOnly:=False, _
                           AddToRecentFiles:=True, _
                           PasswordDocument:="", _
                           PasswordTemplate:="", _
                           Revert:=
                           WritePasswordDocument:="", _
                           WritePasswordTemplate:="", _
                           Format:=wdOpenFormat.wdOpenFormatAuto
                           Encoding:=msoEncoding.msoEncodingWestern
                           Visible:=True, _
                           OpenConflictDocument:=
                           OpenAndRepair:=False, _
                           DocumentDirection:=wdDocumentDirection.wdLeftToRight, _
                           NoEncodingDialog:=False, _
                           XMLTransform:=

FileName -
ConfirmConversions - Pass true to display the Convert File dialog box if the filename passed to Open is not in Word format.
ReadOnly - Pass true to open the document as read-only. If the document is already set to read-only on disk, passing false will not affect the read-only status of the document. The default is False.
AddToRecentFiles - Pass true to add a filename to the list of recently used files. The default is True.
PasswordDocument - Pass a string representing the password for opening the document if the document is password protected
PasswordTemplate - Pass a string representing the password for opening the template if the template is password protected
Revert - If the document you are opening with the Open method is already open in Word, pass true to discard any unsaved changes in the already open document. Pass False to activate the already-open document.
WritePasswordDocument - Pass a string representing the password for saving changes to the document if the document is password protected.
WritePasswordTemplate - Pass a string representing the password for saving changes to the template if the template is password protected.
Format - Pass an wdOpenFormat enumeration specifying the file conversion to be used when opening the file.
Encoding - Pass an Office.MsoEncoding enumeration specifying the code page or character set to be used when you open the document.
Visible - Pass True to open the document in a visible window. The default is True.
OpenConflictDocument - Pass true to open the conflict file for a document that has offline conflicts.
OpenAndRepair - Pass true to try and repair a corrupted document.
DocumentDirection - A member of wdDocumentDirection enumeration specifying the horizontal flow of text in the document.
NoEncodingDialog - Pass true to prevent Word from displaying the encoding dialog box if the text encoding of the document cannot be determined.


The Open method can also return a document object referring to the document that has just been opened.

Dim objDocument As Document 
objDocument = Application.Documents.Open FileName:="C:\Temp\document.doc"

The Documents property is global so the Application property is optional.

objDocument = Application.Documents.Open FileName:="C:\Temp\document.doc" 

Current Folder

If you do not provide a folder path then the document is assumed to be in the current folder.

Documents.Open(FileName:="document.doc") 

However it is good practice to always specify the full folder path though.

Documents.Open(FileName:="C:\Temp\document.doc" 

Confirm Conversions

If this argument is set to True the Convert File dialog box will be displayed if the file isn't in Microsoft Word format.


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.


Revert

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.


Encoding

The document encoding (code page or character set) to be used by Microsoft Word when you view the saved document.
The default value is the system code page.


Visible

In Word 2000 if you open a Document with the Visible argument set to False the document is not added to the Documents or the Windows collections.

objDocument = Application.Documents.Open FileName:="C:\Temp\document.doc", _ 
                                         Visible:=False

No Encoding

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


Using a system independent path separator

Windows uses the path separator "\"
Macintosh uses the path seperator ":"

Application.PathSeperator 

This returns the correct path seperator for the current file system.



Application.Options.DefaultOpenFormat = wdOpenFormat.wdOpenFormatDocument 


You can return an open Document object using the Documents collection.

Dim objDocument As Document 
   Set objDocument = Documents("filename.doc")
   Set objDocument = Documents(1)

Because the index number can change when you open and close documents it is always safer to use the actual filename.



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext