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

VBA-Excel: Writing Text to Word document

For writ­ing text in Microsoft Word Doc­u­ment using Microsoft Excel, you need to fol­low the steps below:

  • Cre­ate the object of Microsoft Word
  • Add doc­u­ments to the Word
  • 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.


 

Cre­ate the object of Microsoft Word

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

Add doc­u­ments to the Word

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

 

Com­plete Code:

FunctionFnWriteToWordDoc()
   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 ("This is my text in Word Document using Excel")

End Function

 

Word- Write using Excel

Word– Write using Excel

 

You may also like...

8 Responses

  1. Bob Korndorffer says:

    I copied and pasted your code below a pre­ced­ing sub in the mod­ule for the par­tic­u­lar work­book and sheet I am inter­ested in insert­ing some text. It will not accept it. I get the message

    Com­pile error:
    Expected: =

    I have had very lit­tle expe­ri­ence writ­ing func­tions and I’m not sure why this task fits as a func­tion. Please be patient with this rookie.

    • Bob Korndorffer says:

      Oops! Just noticed this is to write from Excel to Word. I want to write from Word VBA to Word. Sorry. I’ll google on and hope to find my answer.

  2. Kevin Brawner says:

    Could you give more com­pre­hen­sive infor­ma­tion.
    After this point, how do you for­mat the text, write more text on new lines, things of that nature that you would do in word but can be strange to do from VBA.

  3. Sumit Jain says:

    could you please be more spe­cific like what other oper­a­tions you are talk­ing abt,
    Cer­tainly there are oth­ers things you can do , please read
    http://excel-macro.tutorialhorizon.com/category/automate/working-with-microsoft-word/
    if you are talk­ing about other things, please mention.

    Thanks
    Sumit

  4. Kevin Brawner says:

    I am using excel to gen­er­ate a report of sorts. From spe­cific inputs I want to be able to cre­ate a fully for­mat­ted word doc­u­ment with sev­eral pages of text. This is to include a header and footer, titles, lists with both num­ber­ing and let­ter­ing, and dif­fer­ent align­ments and text types, bold, under­line, etc.

    Right now I have about one page.
    I am using .Insert­Break Type:=wdLineBreak in order to go to the next line
    and .Type­Para­graph when I want to skip line.
    I am hav­ing some issues with align­ment being writ­ten over and how to use it only on spe­cific sec­tions.
    I am using .ParagraphFormat.Alignment = wdAlign­Para­graph­Cen­ter etc.

    Again, I am doing this so that excel can check cer­tain cells to get out­put cal­cu­la­tion val­ues, and so that cer­tain lines of text will only be writ­ten given cer­tain conditions.

  5. Raushan Kumar says:

    Hi All,

    i want some more infor­ma­tion on this.…
    want to copy the con­tent from one string to another string of a word doc­u­ment and paste into other word document…

    • SJ says:

      could you please pro­vide some exam­ples to make it more clear??

      • Raushan Kumar says:

        exam­ple:
        doc1.docx -
        Cre­ate the object of Microsoft Word
        Add doc­u­ments to the Word
        Make the MS Word vis­i­ble
        i need to copy from “object” to “Make” and paste into to doc2.docx doc­u­ment… it means need to search a string and copy­ing the con­tent after that string to next search String.

Leave a Reply

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

%d bloggers like this: