Be the first user to complete this post
|
Add to List |
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
Also Read:
- VBA-Excel: Open and Print the Word Document
- Excel-VBA : Open a MS Word Document using Excel File using Explorer Window.
- VBA-Excel: Edit And Save an Existing Word Document
- VBA-Excel: Add/Insert multiple Images/Pictures from a folder in Word Document
- VBA-Excel: Create a new Word Document