VB script for creating .doc files

You will need to change the working directory value in work_dir variable for saving the files in directory on your machine.

——————————————————————

option explicit

dim word, filesys 'the Word application
dim doc 'the Word document
dim selection 'text selection
dim fs 'the File system
dim work_dir 'the directory for storing files
dim folder, folders 'used when creating the new file
dim i, j 'indexes for arrays

work_dir = "D:\kedar\vbscript\"

set word = createobject("word.application")
set fs = createobject("Scripting.FileSystemObject")

set doc = word.documents.add
set selection = word.selection
selection.typetext "An Automated Word Report"
selection.typeparagraph

doc.saveas(work_dir & "chr1.doc")

word.visible = True

word.Quit

set filesys=CreateObject("Scripting.FileSystemObject")

filesys.CopyFile "D:\kedar\vbscript\chr.doc", "D:\kedar\"

set fs = nothing
set word = nothing
set filesys = nothing

-----------------------------------------------------------------

You can also use for loop for creating saving multiple .doc files with different names as below:

---------------------------------------------------------------

option explicit

 dim word, filesys 'the Word application
 dim doc 'the Word document
 dim selection 'text selection
 dim fs 'the File system
 dim work_dir 'the directory for storing files
 dim folder, folders 'used when creating the new file
 dim i, j 'indexes for arrays
 dim filename

 work_dir = "D:\kedar\vbscript\" 
for i=1 to 2

         set word = createobject("word.application")
         set fs = createobject("Scripting.FileSystemObject")

         set doc = word.documents.add
         set selection = word.selection
         selection.typetext "An Automated Word Report"
         selection.typeparagraph

         filename = "chr" & i & ".doc"

         doc.saveas(work_dir & filename)

         word.visible = True

         word.Quit

         set filesys=CreateObject("Scripting.FileSystemObject")

         filesys.CopyFile work_dir & filename, "D:\kedar\"

 NEXT
 set fs = nothing
 set word = nothing
 set filesys = nothing
--------------------------------------------------------------
Advertisements

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: