VBA-Excel: Create and Save the Word document

For cre­at­ing and sav­ing the Microsoft Word Doc­u­ment using Microsoft Excel, you need to fol­low the steps below:

  • Cre­ate the object of Microsoft Word
  • Cre­ate a doc­u­ment object and add doc­u­ments to it
  • Make the MS Word visible
  • Cre­ate a Selec­tion object with the help of WordObject.
  • Use this Selec­tion object to type the text into the WordDocument.
  • Save the document

 

Cre­ate the object of Microsoft Word

Set obj­Word = CreateObject(“Word.Application”)

Cre­ate a doc­u­ment object and add doc­u­ments to it

Set obj­Doc = objWord.Documents.Add

Make the MS Word Visible

objWord.Visible = True

Cre­ate a Selec­tion object with the help of WordObject.

Set objS­e­lec­tion = objWord.Selection

Use this Selec­tion object to type the text into the WordDocument.

objSelection.TypeText (“This is my text in Word Doc­u­ment using Excel”)

Save the Document

objDoc.SaveAs (“D:\MyFirstSave”)

 

Com­plete Code:

Function FnWriteToWordDoc()

   Dim objWord

   Dim objDoc

   Dim objSelection

   Set objWord = CreateObject("Word.Application")

   Set objDoc = objWord.Documents.Add    

   objWord.Visible = True

   Set objSelection = objWord.Selection

   objSelection.TypeText ("Saving this file after this text")

   objDoc.SaveAs ("D:\MyFirstSave")

End Function 

Also Read About

Happy Macro­ing :)

Sumit Jain

 





			

4 thoughts on “VBA-Excel: Create and Save the Word document

    • Its a pure vb script. If u write the entire code in text file and make it as .vbs file then also it will work. Which set of state­ment is not work­ing for u?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>