Hey, Scripting Guy! Question

Hey, Scripting Guy! How do I get multiple lines of output in a Web page using the Document.Body.InnerHTML property? I can only get one line of output on my page.

-- CM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CM. Sounds like you’re creating an instance of Internet Explorer and trying to display script output in it. And we’re willing to bet you’re using a script similar to this:

strComputer = "."

Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"   
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Visible = 1

Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
  ("SELECT * FROM Win32_Service")
 
For Each objItem in colItems
    strHTML = objItem.DisplayName  & " = " & objItem.State & "<BR>"
    objExplorer.Document.Body.InnerHTML = strHTML
Next

In theory, this script is supposed to display the name and status of all the services installed on your computer. If you have three services installed on your computer, you expect to get output that looks like this:

Service A = Running
Service B = Stopped
Service C = Running

Instead, you’re getting output that looks like this, with only the very last service in the collection displayed:

Service C = Running

What’s wrong? Well, you have a For Each loop that loops through the collection of services. And you have a line of code that assigns the service name and state to a variable. That’s this line:

strHTML = objItem.DisplayName  & " = " & objItem.State & "<BR>"

You then have this line of code, which sets the InnerHTML property of your Web page to the value of strHTML:

objExplorer.Document.Body.InnerHTML = strHTML

This is where the problem lies. The InnerHTML property, for our purposes, consists of all the information displayed on the Web page. The first time your code loops through the collection strHTML is equal to this:

Service A = Running

And that’s exactly what gets displayed in the Web page. But what happens the second time your code loops through the collection? When that happens strHTML gets set to this:

Service B = Stopped

That’s fine, except now you’re setting the InnerHTML - remember, that’s all the information that is displayed on the Web page - to the value of strHTML. That means that the entire contents of your Web page is erased and replaced by strHTML. That’s why only one line of information is displayed: strHTML only contains one line of information. You keep setting the InnerHTML property to one line of information, then a second later you replace it with a new line. You’re replacing information rather than appending it.

So is there a way to fix this? Of course there is; otherwise we wouldn’t have chosen to answer this question! This script solves the problem for you:

strComputer = "."

Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"   
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Visible = 1

Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
  ("SELECT * FROM Win32_Service")
 
For Each objItem in colItems
    strHTML = strHTML & objItem.DisplayName  & " = " & objItem.State & "<BR>"
Next
 
objExplorer.Document.Body.InnerHTML = strHTML

What’s different here? First notice the new line of code for assigning a value to strHTML:

strHTML = strHTML & objItem.DisplayName  & " = " &  objItem.State & "<BR>"

What we’re doing here is saying that strHTML gets assigned whatever is already in strHTML plus some new information. The first time your script runs through the loop, strHTML gets assigned this value:

Service A = Running

The second time the script runs through the loop, it gets assigned the value it already has plus the new information. Thus strHTML equals this:

Service A = Running
Service B = Stopped

By the third time through, strHTML will equal this:

Service A = Running
Service B = Stopped
Service C = Running

This is the data you wanted displayed. And notice that we don’t display the data inside the For Each loop. Instead, all we do in the loop is build up the value of strHTML. After we have looped through all the data, then we finally set the InnerHTML property to the value of strHTML. And because strHTML contains all our data, all our data gets displayed on the Web page.