Macro Message box doesn't display output?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi There,

Very much a newbie to VB and Macros.

I have a script which is linked as a macro to a button on a worksheet. The
script is used to get info about a PC and it's logged on user. Running the VB
script out of Excel functions normally and I'm able to see the displayed info
in a WSH window. However when I run it in the Excel workbook the inital
message box is opened but the resulting info is not displayed.

This is the script

***********************************
Dim s, System, Item, RemotePC
Sub Button5_Click()
RemotePC = InputBox("Enter in computer name to query", "")

On Error Resume Next

Set WshShell = CreateObject("WScript.Shell")

Set System = GetObject("winmgmts:\\" & RemotePC &
"\root\cimv2").instancesOf("Win32_ComputerSystem")

For Each Item In System
s = "Computer Info" & vbCrLf
s = s & "***********************" & vbCrLf
s = s & "Name: " & Item.Name & vbCrLf
s = s & "Status: " & Item.Status & vbCrLf
s = s & "Type: " & Item.SystemType & vbCrLf
s = s & "Mfg: " & Item.Manufacturer & vbCrLf
s = s & "Model: " & Item.Model & vbCrLf
s = s & "RAM: ~" & Item.TotalPhysicalMemory \ 1024000 & " MB" & vbCrLf
s = s & "User: " & Item.UserName

If Item.Status = "OK" Then
Item.Status = "Enabled"
Else
Item.Status = "Disabled"
End If

If Item.Status = "Disabled" Then
WScript.Echo "No connection available"
Else
WScript.Echo s
End If

Next

End Sub
*******************************

Any guru help would be appreciated.
 
K,
The following worked for me in XL2002...
Note: that WshShell is declared (as a variant) and
the WScript.echo lines are replaced with a message box.
Also, a line continuation character " _" is used on the Set System line.

Jim Cone
San Francisco, USA

'----------------------------
Sub Button5_Click()
Dim s, System, Item, RemotePC, WshShell
RemotePC = InputBox("Enter in computer name to query", "")

'On Error Resume Next

Set WshShell = CreateObject("WScript.Shell")
Set System = GetObject("winmgmts:\\" & RemotePC & _
"\root\cimv2").instancesOf("Win32_ComputerSystem")

For Each Item In System
s = "Computer Info" & vbCrLf
s = s & "***********************" & vbCrLf
s = s & "Name: " & Item.Name & vbCrLf
s = s & "Status: " & Item.Status & vbCrLf
s = s & "Type: " & Item.SystemType & vbCrLf
s = s & "Mfg: " & Item.Manufacturer & vbCrLf
s = s & "Model: " & Item.Model & vbCrLf
s = s & "RAM: ~" & Item.TotalPhysicalMemory \ 1024000 & " MB" & vbCrLf
s = s & "User: " & Item.UserName

If Item.Status = "OK" Then
Item.Status = "Enabled"
Else
Item.Status = "Disabled"
End If

If Item.Status = "Disabled" Then
'WScript.echo "No connection available"
MsgBox "No connection available"
Else
'WScript.echo s
MsgBox s
End If

Next
End Sub
'----------------------------


Hi There,

Very much a newbie to VB and Macros.

I have a script which is linked as a macro to a button on a worksheet. The
script is used to get info about a PC and it's logged on user. Running the VB
script out of Excel functions normally and I'm able to see the displayed info
in a WSH window. However when I run it in the Excel workbook the inital
message box is opened but the resulting info is not displayed.
This is the script
***********************************
Dim s, System, Item, RemotePC
Sub Button5_Click()
RemotePC = InputBox("Enter in computer name to query", "")
On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")
Set System = GetObject("winmgmts:\\" & RemotePC &
"\root\cimv2").instancesOf("Win32_ComputerSystem")

For Each Item In System
s = "Computer Info" & vbCrLf
s = s & "***********************" & vbCrLf
s = s & "Name: " & Item.Name & vbCrLf
s = s & "Status: " & Item.Status & vbCrLf
s = s & "Type: " & Item.SystemType & vbCrLf
s = s & "Mfg: " & Item.Manufacturer & vbCrLf
s = s & "Model: " & Item.Model & vbCrLf
s = s & "RAM: ~" & Item.TotalPhysicalMemory \ 1024000 & " MB" & vbCrLf
s = s & "User: " & Item.UserName
If Item.Status = "OK" Then
Item.Status = "Enabled"
Else
Item.Status = "Disabled"
End If

If Item.Status = "Disabled" Then
WScript.Echo "No connection available"
Else
WScript.Echo s
End If

Next
End Sub
*******************************

Any guru help would be appreciated.
 
Thanks alot Jim, that's worked a treat. I did however have to leave the "On
error resume next line" in the code otherwise if you entered in a random name
or just clicking Ok or Cancel it came back with a runtime error.

Thanks again mate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top