How to Copy Data From Excel to Word Using VBA Excel

To copy data from Excel to a Word file using VBA we need to access the word application using Excel VBA. In this tutorial we will learn how to open a word application, add a document and copy-paste data from excel to it.

 

In this article, we will use the Early Binding method to create an object of word application instead of using the late binding. You can read about it here in detail.

The Process Of Copying Excel Data To A Word File Using Excel VBA

To copy data from excel to a word file using VBA, we first need to open the Word Application of course. Then add a document to it (if you want a new document). Copy data from excel file. Select the paragraph on the doc and paste on it.Finally save and close the document. Each of these steps can be done easily from Excel. You will not need to interact with the Word Document.

Let's get started with an example without getting any further into theory. Because Iron Man said, "Sometimes you gotta run before you can walk".

Example : Paste Excel Data To Word Using VBA

The below code is an example of how you can copy some range of excel and paste it into a new word document and save it on the drive to use later

'VBA Code To Write to Copy data from Excel to A Document

Sub ExcelToWord()

   'Using Early Binding

    Dim wordApp As Word.Application

    Dim mydoc As Word.Document

        

    

    'Creating a new instance of word only if there no other instances

    Set wordApp = New Word.Application

    

    

    'Making word App Visible

    wordApp.Visible = True

   

    'Creating a new document

    Set mydoc = wordApp.Documents.Add()


    

    'copying the content from excel sheet


    ThisWorkbook.Worksheets("sheet1").Range("A1:g20").Copy


    'Pasting on the document


    mydoc.Paragraphs(1).Range.PasteExcelTable _

                               LinkedToExcel:=False, _

                               WordFormatting:=False, 

                               RTF:=False

    'saving the document

    mydoc.SaveAs2 "MyDoc"

    

    'closing the document

    mydoc.Close

   

    'Emptying the Clipboard 

    CutCopyMode = False

   

End Sub

Explanation of the Code:

 

Well I have explained each step in the code itself using comments but let's have some word about the lines we have used in this sub.

 

  'Created variables of word application and document type

    Dim wordApp As Word.Application

    Dim mydoc As Word.Document

 

Here we have declared two variables of the required types. We can do this because  we have already added the reference to the word application. You can do this by going to tools in menu. Find references option and then look for the word reference.

 

    'Creating a new instance of word only if there no other instances

    Set wordApp = New Word.Application

    

    

    'Making word App Visible

    wordApp.Visible = True

   

    'Creating a new document

    Set mydoc = wordApp.Documents.Add()

 

In the first line above, we are intentiating the wordApp variable with an object of type Word.App using the New keyword. This will open the Word Application.

 

In the second line we are making the word application visible so that we can work with it.

 

In the next line, we add a new document to the word application using Word.Documents.Add() function. This is stored in the mydoc variable.

 

    'copying the content from excel sheet

    ThisWorkbook.Worksheets("sheet1").Range("A1:G20").Copy

 

Here we are simply copying a range from excel. You must have done it before. It is stored on the clipboard.

 

    'Pasting on the document

 

    mydoc.Paragraphs(1).Range.PasteExcelTable _

                               LinkedToExcel:=False, _

                               WordFormatting:=False, 

                               RTF:=False

 

We are using the PasteExcelTable method of Paragraph.Range class of mydoc to paste data from clipboard.

 

 'saving the document

    mydoc.SaveAs2 "MyDoc"

    

    'closing the document

    mydoc.Close

   

    'Emptying the Clipboard 

    CutCopyMode = False

 

We are saving the document with the name MyDoc. Then we close the document using the Close function. Finally we release the clipboard to be used by others.

So yeah guys, this is how you can create a new Word document and copy Excel data to it using VBA. I have not gone into details as it would make the article exhaustingly long. This was only to learn how you can paste to a word document through Excel. I hope it helped you understand the process. If you have any questions regarding this article, you can contact me through the comments section below.

Related Articles:

Getting Started With Excel VBA UserForms| I will explain how to create a form in excel, how to use VBA toolbox, how to handle user inputs and finally how to store the user inputs. We will go through these topics using one example and step by step guide.

VBA variables in Excel| VBA stands for Visual Basic for Applications. It is a programming language from Microsoft. It is used with Microsoft Office applications such as MSExcel, MS-Word and MS-Access whereas VBA variables are specific keywords.

Excel VBA Variable Scope| In all the programming languages, we have variable access specifiers that define from where a defined variable can be accessed. Excel VBA is no Exception. VBA too has scope specifiers.

ByRef and ByVal Arguments | When an argument is passed as a ByRef argument to a different sub or function, the reference of the actual variable is sent. Any changes made into the copy of the variable, will reflect in the original argument.

Delete sheets without confirmation prompts using VBA in Microsoft Excel | Since you are deleting sheets using VBA, you know what you are doing. You would like to tell Excel not to show this warning and delete the damn sheet.

Add And Save New Workbook Using VBA In Microsoft Excel 2016| In this code, we first created a reference to a workbook object. And then we initialized it with a new workbook object. The benefit of this approach is that you can do operations on this new workbook easily. Like saving, closing, deleting, etc

Display A Message On The Excel VBA Status Bar| The status bar in excel can be used as a code monitor. When your VBA code is lengthy and you do several tasks using VBA, you often disable the screen update so that you don’t see that screen flickering.

Turn Off Warning Messages Using VBA In Microsoft Excel 2016| This code not only disables VBA alerts but also increases the time efficiency of the code. Let’s see how.

Popular Articles:

50 Excel Shortcuts to Increase Your Productivity | Get faster at your task. These 50 shortcuts will make you work even faster on Excel.

The VLOOKUP Function in Excel | This is one of the most used and popular functions of excel that is used to lookup value from different ranges and sheets. 

COUNTIF in Excel 2016 | Count values with conditions using this amazing function. You don't need to filter your data to count specific values. Countif function is essential to prepare your dashboard.

How to use SUMIF Function in Excel | This is another dashboard essential function. This helps you sum up values on specific conditions.

Leave a Reply

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

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.

Visit Us On TwitterVisit Us On FacebookVisit Us On Youtube