WSH scripts provides a simple interface to access Office application like Microsoft Word from a script. Below I like to demonstrate how to access Word from a VBScript program. The script shall launch Word, set a few properties and show Word application properties in a dialog box. To access the Word application object, you may use the following command in VBScript:
Set objWrd = WScript.CreateObject ("Word.Application")
This command retrieves an object reference to the Word Application object. Because we omit here a version number, any installed Word version (95/97/2000) will be invoked. If you like to invoke Word 97 for instance, you must add also the internal version number as:
Set objXL = WScript.CreateObject ("Word.Application.8")
But this comes with the risk, that your script fails, if Microsoft Word 2000 is installed. Afterwards you can use the object variable objWrd to access the Application object and its sub-objects from the script. The following sequence sets the properties of the Word window:
objWrd.WindowState = vbNormal ' Normal objWrd.Height = 300 ' height objWrd.Width = 400 ' width objWrd.Left = 40 ' X-Position objWrd.Top = 20 ' Y-Position objWrd.Visible = true ' show window
Then you may access other properties or create references to other sub-objects of the application object. The following two statements write something in the window's title and status bar.
' try to write something into the title- and statusbar objWrd.Caption = Title objWrd.Statusbar = "Date: " & Date
To quit Word, you my apply the Quit method to the Word Application object:
objWrd.Quit
The following code listing shows a sample which invokes word, shows some properties and terminates.
'************************************************ ' File: Word.vbs (WSH sample in VBScript) ' Author: Günter Born ' ' Launch Word, set a few properties and show ' Word application properties in a dialog box. ' ' A sample derived from my books: ' Inside Windows Scripting Host, MS Press Germany ' Windows Scripting Host Tutorial, an E-book available from my WSH Bazaar: ' ' Check out Born's Windows Scripting Host Bazaar at: ' http://www.borncity.de ' ' In no way shall the author be liable for any ' losses or damages resulting from the use of this ' program. Use AS-IS at your own risk. ' ' The code is the property of the author. You may ' use the code and modify it, as far as this header ' remains intact. Further updates and other samples ' may be found on my site mentioned above. '************************************************ Option Explicit Const vbNormal = 0 ' window styles Const vbMaximized = 1 Const vbMinimized = 2 DIM objWrd, objWb ' Word object variable DIM Title, Text Title = "WSH sample - by G. Born" ' create Word object Set objWrd = WScript.CreateObject ("Word.Application") ' set the Word window properties objWrd.WindowState = vbNormal ' Normal objWrd.Height = 200 ' Height objWrd.Width = 400 ' Width objWrd.Left = 100 ' X-Position objWrd.Top = 100 ' Y-Position objWrd.Visible = true ' show window ' try to write something into the title- and statusbar objWrd.Caption = Title objWrd.Statusbar = "Date: " & Date ' ask, whether the window shall be maximized/minimized If (MsgBox("Maximize window", _ vbQuestion + vbYesNo, _ Title) = vbYes) Then objWrd.WindowState = vbMaximized Else If (MsgBox("Minimize window", _ vbQuestion + vbYesNo, _ Title) = vbYes) Then _ objWrd.WindowState = vbMinimized End If ' Query Word properties Text = "Word properties" & vbCRLF & vbCRLF Text = Text & "window height " & vbTab & objWrd.Height & vbCRLF Text = Text & "window width " & vbTab & objWrd.Width & vbCRLF Text = Text & "window title " & vbTab & objWrd.Caption & vbCRLF Text = Text & vbCRLF & "Word terminates..." WScript.Echo Text objWrd.Quit ' quit Word Set objWrd = Nothing ' release object variable ' End |
Well, and here for all scripters who prefer JScript (I know, too much of my samples are kept in VBScript - but my books contain 95% of the samples both in VBScript and JScript) the JScript implementation - just straight forward.
/* File: Word.js (WSH sample in JScript) Author: Günter Born Launch Word, set a few properties and show Word application properties in a dialog box. A sample derived from my books: Inside Windows Scripting Host, MS Press Germany Windows Scripting Host Tutorial, an E-book avaiable on my WSH Bazaar: Check out Born's Windows Scripting Host Bazaar at: http://www.borncity.de In no way shall the author be liable for any losses or damages resulting from the use of this program. Use AS-IS at your own risk. The code is the property of the author. You may use the code and modify it, as far as this header remains intact. Further updates and other samples may be found on my site mentioned above. */ var vbOKCancel = 1; // declare variables var vbYesNo = 4; var vbYes = 6; var vbOK = 1; var vbInformation = 64; var vbQuestion = 32; var vbCancel = 2; var vbNormal = 0; // window styles var vbMinimized = 2; var vbMaximized = 1; var vbTab = String.fromCharCode(9); var vbCRLF = "\n"; var Title = "WSH sample - by G. Born"; // create Word object var objWrd = WScript.CreateObject ("Word.Application"); // set the Word window properties objWrd.WindowState = vbNormal; // Normal objWrd.Height = 200; // Height objWrd.Width = 400; // Width objWrd.Left = 100; // X-Position objWrd.Top = 100; // Y-Position objWrd.Visible = true; // show window // try to write something into the title- and statusbar objWrd.Caption = Title; objWrd.Statusbar = "Date: " + Date; // ask, whether the window shall be maximized/minimized var wsh = WScript.CreateObject ("WScript.Shell") if (wsh.Popup("Maximize window", 0, Title, vbQuestion + vbYesNo) == vbYes) { objWrd.WindowState = vbMaximized } else { if (wsh.Popup("Minimize window", 0, Title, vbQuestion + vbYesNo) == vbYes) objWrd.WindowState = vbMinimized; } // Query Word properties Text = "Word properties" + vbCRLF + vbCRLF; Text = Text + "window height " + vbTab + objWrd.Height + vbCRLF; Text = Text + "window width " + vbTab + objWrd.Width + vbCRLF; Text = Text + "window title " + vbTab + objWrd.Caption + vbCRLF; Text = Text + vbCRLF + "Word terminates..."; WScript.Echo (Text); objWrd.Quit(); // quit Word // End |
Additional information about manipulating Word from WSH scripts may be found in my German and my English WSH books.
(c) G. Born