Documents


Documents.Add

Creating a new document based on a different template
The new document will become the active document

Application.Documents.Add Template:="C:\Temp\Template.dot" 
                          NewTemplate:=
                          DocumentType:=wdNewDocumentType.wdNewBlankDocument, _
                          Visible:=True)

Template - The name of the template to be used for the new document. The default is the Normal template
NewTemplate - Whether to open the document as a template. The default is False
DocumentType - The type of new document to create. The default is NewBlankDocument
Visible - Whether the new document is visible. The default is True


Creating a blank document based on the Normal template

Documents.Add 
Set objDocument = Documents.Add

This method returns the document that was created.


Creating a copy of the active document

Application.Documents.Add Template:=ActiveDocument.FullName 

Document File Name

This line will return the filename of the active document.

Application.ActiveDocument.Name = Document1.doc 
ActiveDocument.Name = Document1.doc
Application.Documents.Item(2).Name

If the active document has not been saved yet then this returns the filename with no extension

ActiveDocument.Name = Document2 

You can check if a document has been saved or not by checking if the following are the same

If (ActiveDocument.Name = ActiveDocument.FullName) Then 
'document has not been saved yet
End If

Document Folder

This line returns the folder path of where the document is saved

Application.ActiveDocument.Path 
ActiveDocument.Path

If the active document has not been saved then this is blank ???


Document Folder and FileName

This returns the full folder path and the filename

ActiveDocument.FullName 

This line is equivalent to the following

ActiveDocument.Path & ActiveDocument.PathSeparator & ActiveDocument.Name 

Looping Through Open Documents


Dim icount As Integer 
For icount = 1 To Application.Documents.Count
   If (Application.Documents(icount).Name - "Document1") Then
   End If
Next icount

A lot of windows related properties are actually properties of the Pane object which is associated with the window.
This line will create an error if there is no document currently open

If (Application.Documents.Count > 1) Then 
   sName = ActiveDocument.Name
End If

Set objRange = ActiveDocument.Content 
Set objRange = ActiveDocument.Range


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