Friday, March 7, 2008

VBScript to automatically insert strings into Word document, print and close it

Make sure you have the file "document.doc" and it contains the bookmarks as you can see (Text1-7). The VBScript will open the document, insert all strings into the bookmarks, print and close it without saving so you can re-use it.

strLocation = wscript.arguments(0)

strLastname = wscript.arguments(1)
strFirstname = wscript.arguments(2)
strDepartment = wscript.arguments(3)
strUserID = wscript.arguments(4)
strPassword = wscript.arguments(5)
strUmraServicePath = wscript.arguments(6)

SourceDocPath = strUmraServicePath & "\document.doc"
set ObjWord = CreateObject("Word.Application")
RetVal = ObjWord.Documents.Open(SourceDocPath)
ObjWord.Visible = False
ObjWord.ScreenUpdating = False

ObjWord.ActiveDocument.Bookmarks("Text1").Select
ObjWord.Selection.Text = strLocation
ObjWord.ActiveDocument.Bookmarks("Text2").Select
ObjWord.Selection.Text = strLastName
ObjWord.ActiveDocument.Bookmarks("Text3").Select
ObjWord.Selection.Text = strFirstName
ObjWord.ActiveDocument.Bookmarks("Text4").Select
ObjWord.Selection.Text = strDepartment
ObjWord.ActiveDocument.Bookmarks("Text5").Select
ObjWord.Selection.Text = Now
ObjWord.ActiveDocument.Bookmarks("Text6").Select
ObjWord.Selection.Text = strUserID
ObjWord.ActiveDocument.Bookmarks("Text7").Select
ObjWord.Selection.Text = strPassword

objWord.ActiveDocument.PrintOut False
objWord.ActiveDocument.Close 0
objWord.Quit
set objWord = Nothing

0 comments: