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 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 

 


													

You may also like...

9 Responses

  1. Bicky Ealias says:

    Nice one sumit..

  2. Jigar says:

    How you do the same using Visual stu­dio 2013
    In visual basic there set state­ment 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 state­ment is not work­ing 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 actu­ally awe­some in sup­port of me.

  4. Srinivasan says:

    So good
    What about release of objects
    Set objdoc=nothing

  5. Vikas says:

    very good tuto­r­ial !!!
    It there any way to open an exist­ing word doc­u­ment and embed other word, excel or PDF file to the exist­ing word file at the end of text.
    Thanks

  1. September 6, 2014

    […] Exam­ple here VBA-Excel: Cre­ate and Save the Word document […]

Leave a Reply

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

%d bloggers like this: