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

VBA-Excel: Appending Text to an Existing Word Document — at the End

To append the text at the end of the Microsoft Word Doc­u­ment using Microsoft Excel, you need to fol­low the steps below:

  • Declare END_OF_STORY and MOVE_SELECTION as variables
  • Assign MOVE_SELECTION =0 and END_OF_STORY = 6
  • Cre­ate the object of Microsoft Word
  • Using MS word object, Open the exist­ing word doc­u­ment by pro­vid­ing the com­plete path
  • Make the MS Word visible
  • Cre­ate a Selec­tion object with the help of WordObject.
  • Move the Selec­tion to the end of the document.
  • Append the text in  the Word Doc­u­ment using SelectionObject


Declare END_OF_STORY and MOVE_SELECTION as variables

Dim END_OF_STORY

Dim MOVE_SELECTION

Assign MOVE_SELECTION =0 and END_OF_STORY = 6

END_OF_STORY = 6

MOVE_SELECTION = 0

Cre­ate the object of Microsoft Word

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

Using MS word object, Open the exist­ing word doc­u­ment by pro­vid­ing the com­plete path

Set obj­Doc = objWord.Documents.Open(“D:\OpenMe.docx”)

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

Move the Selec­tion to the end of the document.

objSelection.EndKey END_OF_STORY, MOVE_SELECTION

Append the text in  the Word Doc­u­ment using SelectionObject

objS­e­lec­tion.Type­Text (“Whattt..I am at the End of the Doc­u­ment. Not Fair :(“ & vbCrLf)

Com­plete Code:

Function FnAppendAtEND()

   Dim objWord

   Dim objDoc

   Dim objSelection

  Dim END_OF_STORY

    Dim MOVE_SELECTION    

   END_OF_STORY = 6

    MOVE_SELECTION = 0    

  Set objWord = CreateObject("Word.Application")    

  Set objDoc = objWord.Documents.Open("D:\OpenMe.docx")

  objWord.Visible = True

  Set objSelection = objWord.Selection

  objSelection.Font.Bold = True    

  objSelection.EndKey END_OF_STORY, MOVE_SELECTION    

  objSelection.Font.Size = "25"

  objSelection.Font.Color = RGB(12, 200, 0)

  objSelection.TypeText ("Whattt..I am at the End of the Document. Not Fair :(" & vbCrLf)            

End Function
MS Word -Append Text at the End

MS Word –Append Text at the End

 

 

You may also like...

4 Responses

  1. sam says:

    Is there some VBA code that can com­bine cer­tain Word doc­u­ments and saved as a new file based on check­boxes selected on Excel. For exam­ple, if there were 3 check­boxes on Excel and each check­box had a cor­re­spond­ing Word file with con­tent then based on what check­boxes are selected by the user their cor­re­spond­ing Word files would be merged to one new file. Any sug­ges­tions would be greatly appreciated.

Leave a Reply

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

%d bloggers like this: