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

  • Declare END_OF_STORY and MOVE_SELECTION as variables
  • Assign MOVE_SELECTION =0 and END_OF_STORY = 6
  • Create the object of Microsoft Word
  • Using MS word object, Open the existing word document by providing the complete path
  • Make the MS Word visible
  • Create a Selection object with the help of WordObject.
  • Move the Selection to the end of the document.
  • Append the text in  the Word Document 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

Create the object of Microsoft Word

Set objWord = CreateObject(“Word.Application”)

Using MS word object, Open the existing word document by providing the complete path

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

Make the MS Word Visible

objWord.Visible = True

Create a Selection object with the help of WordObject.

Set objSelection = objWord.Selection

Move the Selection to the end of the document.

objSelection.EndKey END_OF_STORY, MOVE_SELECTION

Append the text in  the Word Document using SelectionObject

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

Complete 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 combine certain Word documents and saved as a new file based on checkboxes selected on Excel. For example, if there were 3 checkboxes on Excel and each checkbox had a corresponding Word file with content then based on what checkboxes are selected by the user their corresponding Word files would be merged to one new file. Any suggestions would be greatly appreciated.

Leave a Reply

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

%d bloggers like this: