Microsoft Word Document Complete Reference Guide

Microsoft Word Document Complete Reference Guide

Document

A file containing useful information and can be opened with Microsoft Word Application represents a Word Document. The Document object is a member of the Documents collection. One application can open multiple word document. The Documents collection contains all the Document objects that are currently open in Microsoft Word Application.

Word Application

Word Document

Add Method

Public Sub AddNewDocument()
    'Declare object to hold a document
    Dim oDoc As Document
    
    'Add new document and hold reference
    Set oDoc = Documents.Add
    
    'Save a document
    oDoc.SaveAs2 "C:\vbaoverall.docx"
    
    'Memory cleanup
    Set oDoc = Nothing
End Sub

Save Method

Public Sub SaveDocumentExample()
    'Declare object to hold a document
    Dim oDoc As Document
    
    'Reference active document
    Set oDoc = ActiveDocument
    
    'Save document
    oDoc.Save
    
    'Memory cleanup
    Set oDoc = Nothing
End Sub

Close Method

Public Sub CloseDocumentExample()
    'Declare object to hold a document
    Dim oDoc As Document
    
    'Reference active document
    Set oDoc = ActiveDocument
    
    'Close document
    oDoc.Close SaveChanges:=True
    
    'Memory cleanup
    Set oDoc = Nothing
End Sub

AutoSaveOn Property

Public Sub AutoSaveDocumentExample()
    'Declare object to hold a document
    Dim oDoc As Document
    
    'Reference active document
    Set oDoc = ActiveDocument
    
    'Autosave document
    oDoc.AutoSaveOn = True
    
    'Memory cleanup
    Set oDoc = Nothing
End Sub

Export to PDF

Public Sub ExportToPDFDocumentExample()
    'Declare object to hold a document
    Dim oDoc As Document
    
    'Reference active document
    Set oDoc = ActiveDocument
    
    'Export document
    oDoc.ExportAsFixedFormat "C:\test.pdf", wdExportFormatPDF, True, wdExportOptimizeForPrint, wdExportAllDocument
    
    'Memory cleanup
    Set oDoc = Nothing
End Sub

Insert Paragraph

Public Sub InsertParaInDocument()
    'Get active document object
    Dim oDocument As Document
    Set oDocument = ActiveDocument
    
    'Activate document
    oDocument.Activate
    
    'Add a paragraph
    oDocument.Paragraphs.Add
    
    'Type text
    Selection.Text = "Hi there VBAOVERALL"
    
    'Save and close document
    oDocument.Close SaveChanges:=wdSaveChanges
    
    'Jumpt to next document
    Dim oNextDocument As Document
    Set oNextDocument = Documents(1)
    
    'Activate the document
    oNextDocument.Activate
    
    'Set page orientation
    oNextDocument.PageSetup.Orientation = wdOrientLandscape
    
    'Print document
    oNextDocument.PrintOut
End Sub

Output

Please leave your comments or queries under comment section also please do subscribe to out blogs to keep your self upto date.

One thought on “Microsoft Word Document Complete Reference Guide

Leave a Reply

Your email address will not be published. Required fields are marked *