Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

VBA-Excel: Create and Save the Word document

For creating and saving the Microsoft Word Document using Microsoft Excel, you need to follow the steps below:

  • Create the object of Microsoft Word
  • Create a document object and add documents to it
  • Make the MS Word visible
  • Create a Selection object with the help of WordObject.
  • Use this Selection object to type the text into the WordDocument.
  • Save the document

 

Create the object of Microsoft Word

Set objWord = CreateObject(“Word.Application”)

Create a document object and add documents to it

Set objDoc = objWord.Documents.Add

Make the MS Word Visible

objWord.Visible = True

Create a Selection object with the help of WordObject.

Set objSelection = objWord.Selection

Use this Selection object to type the text into the WordDocument.

objSelection.TypeText (“This is my text in Word Document using Excel”)

Save the Document

objDoc.SaveAs (“D:\MyFirstSave”)

 

Complete 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 

 


													

You may also like...

9 Responses

  1. Bicky Ealias says:

    Nice one sumit..

  2. Jigar says:

    How you do the same using Visual studio 2013
    In visual basic there set statement doesnt work

    • sumitjain says:

      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 statement is not working for u?

  3. Ada says:

    Hi friends, how is all, and what you would like to say on the topic of this post, in my view its actually awesome in support of me.

  4. Srinivasan says:

    So good
    What about release of objects
    Set objdoc=nothing

  5. Vikas says:

    very good tutorial !!!
    It there any way to open an existing word document and embed other word, excel or PDF file to the existing word file at the end of text.
    Thanks

  1. September 6, 2014

    […] Example here VBA-Excel: Create and Save the Word document […]

Leave a Reply

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

%d bloggers like this: