Brent387
asked on
Find text in Excel so vbscript only runs once
I want this script to create a log of who has certain software and on what computer. However, I don't want multiple copies of the same data in the spreadsheet. I want it to search the spreadsheet for the computer name and if it already exists, quit. This is what I have and everything works except that it always runs so I have tons of the same data. Any advice?
Set WshShell = CreateObject("WScript.Shel l")
strProgramFiles = WshShell.ExpandEnvironment Strings("% PROGRAMFIL ES%")
' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Applic ation")
objExcel.Visible = FALSE
objExcel.Silent = TRUE
Set objShell = CreateObject("WScript.Shel l")
strUserName = objShell.ExpandEnvironment Strings("% username%" )
set objComp = WScript.CreateObject ("WScript.Shell")
strComputer = objComp.ExpandEnvironmentS trings("%C omputernam e%")
If objExcel.Cells.Find("%Comp utername%" ) = 0 Then
Set ObjPath = CreateObject("scripting.Fi leSystemOb ject")
If objPath.FileExists(strProg ramFiles & "\Software.exe") Then
' Create a new workbook.
set objWorkBook = objExcel.Workbooks.open("\ \path\exce l.xls")
strText = objWorkbook.Readall
' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Wo rksheets(1 )
objSheet.Name = "Software Logs"
' Populate spreadsheet cells with user attributes.
objSheet.Cells(1, 1).Value = "User Name"
objSheet.Cells(1, 2).Value = "Computer Name"
' Enumerate users and add user names to spreadsheet.
For Each objUser In obShell
objSheet.range("A" & objsheet.rows.count).end(- 4162).offs et(1,1).Va lue = strComputer
objSheet.range("A" & objsheet.rows.count).end(- 4162).offs et(1,0).Va lue = strUserName
Next
' Format the spreadsheet.
objSheet.Range("A1:C1").Fo nt.Bold = True
objSheet.Select
objSheet.Range("B5").Selec t
objExcel.Columns(1).Column Width = 20
objExcel.Columns(2).Column Width = 30
objExcel.Columns(3).Column Width = 20
' Save the spreadsheet and close the workbook.
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Sa ve
objExcel.DisplayAlerts = True
' Quit Excel.
objExcel.Application.Quit
' Clean Up
Set objUser = Nothing
Set objSheet = Nothing
Set objExcel = Nothing
Else
WScript.quit
End if
Else
WScript.quit
End if
Set WshShell = CreateObject("WScript.Shel
strProgramFiles = WshShell.ExpandEnvironment
' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Applic
objExcel.Visible = FALSE
objExcel.Silent = TRUE
Set objShell = CreateObject("WScript.Shel
strUserName = objShell.ExpandEnvironment
set objComp = WScript.CreateObject ("WScript.Shell")
strComputer = objComp.ExpandEnvironmentS
If objExcel.Cells.Find("%Comp
Set ObjPath = CreateObject("scripting.Fi
If objPath.FileExists(strProg
' Create a new workbook.
set objWorkBook = objExcel.Workbooks.open("\
strText = objWorkbook.Readall
' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Wo
objSheet.Name = "Software Logs"
' Populate spreadsheet cells with user attributes.
objSheet.Cells(1, 1).Value = "User Name"
objSheet.Cells(1, 2).Value = "Computer Name"
' Enumerate users and add user names to spreadsheet.
For Each objUser In obShell
objSheet.range("A" & objsheet.rows.count).end(-
objSheet.range("A" & objsheet.rows.count).end(-
Next
' Format the spreadsheet.
objSheet.Range("A1:C1").Fo
objSheet.Select
objSheet.Range("B5").Selec
objExcel.Columns(1).Column
objExcel.Columns(2).Column
objExcel.Columns(3).Column
' Save the spreadsheet and close the workbook.
objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.Sa
objExcel.DisplayAlerts = True
' Quit Excel.
objExcel.Application.Quit
' Clean Up
Set objUser = Nothing
Set objSheet = Nothing
Set objExcel = Nothing
Else
WScript.quit
End if
Else
WScript.quit
End if
There is no Application.Silent property so I dont' know what that line is supposed to achieve. You also appear to be searching for the computername before you open the workbook, and you are looking for the actual string "%COMPUTERNAME%" rather than using the strComputerName variable, which is why you'll never find it. :)
ASKER
The Application.Silent actually makes it so that when I run the script, Excel doesn't open, write the text, and close. I'd never heard of it, but it works. I never got around to putting my script back together the way it was so it's a little out of order. What I am looking for is something that will search the excel script, like a conditional statement that says if the text is there, then quit.
ASKER
That is interesting though, i just got what you were saying about the strComputer. Lemme give that a shot
ASKER
It didn't work
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Whenever a user logs on who has the software I want it to log it in excel. From what I understand I need For Each objUser for that to work. Also, I tried to run your script and now I can't get into the original spreadsheet because it's saying that it's open.
If objSheet.Usedrange.Find(st rComputern ame) Is Nothing Then
Set ObjPath = CreateObject("scripting.Fi leSystemOb ject")
If objPath.FileExists(strProg ramFiles & "\Software.exe") Then
That is what I was looking for. Thanks for your advice
If objSheet.Usedrange.Find(st
Set ObjPath = CreateObject("scripting.Fi
If objPath.FileExists(strProg
That is what I was looking for. Thanks for your advice