Saturday, March 6, 2010

VBScript - Appending text to an existing word document at end

VBScript - Appending text to an existing word document

'opening an existing word document and appending text to it [at the end](tried with word 2007)

Const END_OF_STORY = 6
Const MOVE_SELECTION = 0

set word = createobject("word.application")
word.visible=true
Word.Documents.Open "C:\Documents and Settings\sachin\My Documents\abc.docx"

Set objectSelection = Word.Selection
objectSelection.EndKey END_OF_STORY, MOVE_SELECTION
objectSelection.TypeParagraph()
objectSelection.TypeText "This is sample text"
objectSelection.TypeParagraph()

Another way:

Const END_OF_STORY = 6
Const MOVE_SELECTION = 0

Set word = CreateObject("Word.Application")
word.Visible = True

Set worddoc = word.Documents.Open ("C:\Documents and Settings\sachin\My Documents\abc.docx")
Set objectselection = word.Selection
objectselection.EndKey END_OF_STORY, MOVE_SELECTION

objectselection.TypeParagraph()

objectselection.TypeText "This is a sample text."


Also See:

Opening a new word document
Opening an existing word document
Opening a new word document and writing text to it
Opening an existing word document and appending text to it at the beginning
Saving a new word file
Saving an existing word file
Writing formatted text to a word file
Formatting all content, formatting specific paragraphs or finding and formatting specific text in a word document
Inserting images/pictures to a word file
Creating table in a word document, adding text to cells, adding rows to table
Printing a word file