Microsoft Word Copy entire document to another document VBA example

Microsoft Word Copy entire document to another document VBA example

Copy

Is a method exposed by various objects like Selection, Range, Document which copies selection in clipboard memory. Let’s write code to copy/paste entire document contents to another document.

Code Example

Sub CopyPasteContent()
    'bind document reference
    Dim cDocument As Document
    Set cDocument = ActiveDocument
    
    'Select contents
    cDocument.Content.Select
    
    'bind selection
    Dim oSelection As Selection
    Set oSelection = Selection
    
    Dim tDocument As Document
    'Check selection type
    If oSelection.Type = wdSelectionNormal Then
        'Copy content
        oSelection.Copy
        'Paste contents in new document and hold the reference
        Set tDocument = Documents.Add
        tDocument.Content.Paste
        'Activate the target document
        tDocument.Activate
    End If
    tDocument.SaveAs2 "d:\mytest.docx"
    
    'Memory cleanup
    Set tDocument = Nothing
    Set oSelection = Nothing
End Sub

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

Leave a Reply

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