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 writing text in Microsoft Word Document using Microsoft Excel, you need to follow the steps below:

  • Create the object of Microsoft Word
  • Add documents to the Word
  • 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.


 

Create the object of Microsoft Word

Set objWord = CreateObject(“Word.Application”)

Add documents to the Word

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

 

Complete 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 preceding sub in the module for the particular workbook and sheet I am interested in inserting some text. It will not accept it. I get the message

    Compile error:
    Expected: =

    I have had very little experience writing functions and I’m not sure why this task fits as a function. 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 comprehensive information.
    After this point, how do you format 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 specific like what other operations you are talking abt,
    Certainly there are others things you can do , please read
    http://excel-macro.tutorialhorizon.com/category/automate/working-with-microsoft-word/
    if you are talking about other things, please mention.

    Thanks
    Sumit

  4. Kevin Brawner says:

    I am using excel to generate a report of sorts. From specific inputs I want to be able to create a fully formatted word document with several pages of text. This is to include a header and footer, titles, lists with both numbering and lettering, and different alignments and text types, bold, underline, etc.

    Right now I have about one page.
    I am using .InsertBreak Type:=wdLineBreak in order to go to the next line
    and .TypeParagraph when I want to skip line.
    I am having some issues with alignment being written over and how to use it only on specific sections.
    I am using .ParagraphFormat.Alignment = wdAlignParagraphCenter etc.

    Again, I am doing this so that excel can check certain cells to get output calculation values, and so that certain lines of text will only be written given certain conditions.

  5. Raushan Kumar says:

    Hi All,

    i want some more information on this….
    want to copy the content from one string to another string of a word document and paste into other word document…

    • SJ says:

      could you please provide some examples to make it more clear??

      • Raushan Kumar says:

        example:
        doc1.docx –
        Cre­ate the object of Microsoft Word
        Add doc­u­ments to the Word
        Make the MS Word visible
        i need to copy from “object” to “Make” and paste into to doc2.docx document… it means need to search a string and copying the content 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: