#179430 - 2007-08-17 02:58 PM
Script Request
|
Templar
Fresh Scripter
Registered: 2002-04-11
Posts: 41
Loc: Cincinnati
|
I'm new to COM scripting adn am still trying to learn it I;m eally only looking for a script that will open up an existing Excel Pread sheet adn update specific cells on specific worksheets. One of the cells is a drop down menu but I think just the value will work. Is there anyone who might be able to assist with some code that could do what I am looking for?
Script would do the following 1. Open a specific Excel file on a newtwork share 2. Edit specific cells on a spcific worksheet say Cell E10 and E11 on Worksheet Tuesday 3. Save the File
|
|
Top
|
|
|
|
#179432 - 2007-08-17 03:18 PM
Re: Script Request
[Re: Templar]
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Here's a quick script (user defined function) that may give you a "kickstart" for this:
Break On
$= SetExcelValue("c:\sheet.xls", "tuesday", "B5", "123")
Exit 1
Function SetExcelValue($path, $sheet, $range, $value)
Dim $Excel
Dim $WorkBook
Dim $WorkSheet
$Excel = CreateObject("Excel.Application")
$WorkBook = $Excel.WorkBooks.Open($path)
$WorkSheet = $WorkBook.Sheets.Item("Tuesday")
$WorkSheet.Range($range).Value = $value
$WorkBook.Save()
$WorkSheet = 0
$WorkBook = 0
$Excel.Quit
$Excel = 0
EndFunction
|
|
Top
|
|
|
|
#179433 - 2007-08-17 03:21 PM
Re: Script Request
[Re: Shawn]
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
This one has better error checking ...
Break On
$= SetExcelValue("c:\sheet.xls", "tuesday", "B5", "123")
Exit 1
Function SetExcelValue($path, $sheet, $range, $value)
Dim $Excel
Dim $WorkBook
Dim $WorkSheet
$Excel = CreateObject("Excel.Application")
If @ERROR = 0
$WorkBook = $Excel.WorkBooks.Open($path)
If @ERROR = 0
$WorkSheet = $WorkBook.Sheets.Item($sheet)
If @ERROR = 0
$WorkSheet.Range($range).Value = $value
$= $WorkBook.Save()
$WorkSheet = 0
Endif
$WorkBook = 0
Endif
$Excel.Quit
$Excel = 0
Endif
Exit @ERROR
EndFunction
|
|
Top
|
|
|
|
#179436 - 2007-08-17 04:09 PM
Re: Script Request
[Re: Templar]
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
You could also grab script from the macro generator. Record wat you want to do with the macro tool. Per Example open a new Excel Workboor. Your Macro Editor will show
In a KiX Script you could write:
Break ON
$objExcel = CreateObject("Excel.Application")
$objExcel.Visible = -1
$ = $objExcel.Workbooks.Add
;Get $
|
|
Top
|
|
|
|
#179437 - 2007-08-17 04:10 PM
Re: Script Request
[Re: Shawn]
|
Templar
Fresh Scripter
Registered: 2002-04-11
Posts: 41
Loc: Cincinnati
|
How would I address teh spreadsheet being password protected? Can I somehow input the password into the script?
The script is exactly what I am looking for.
|
|
Top
|
|
|
|
#179440 - 2007-08-17 04:32 PM
Re: Script Request
[Re: Witto]
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
I added an optional password protect param ...
Break On
$= SetOption("WrapAtEol", "On")
$= SetExcelValue("c:\sheet.xls", "tuesday", "B5", "123", "abc123")
? @SERROR
Exit 1
Function SetExcelValue($path, $sheet, $range, $value, optional $password)
Dim $Excel
Dim $WorkBook
Dim $WorkSheet
$Excel = CreateObject("Excel.Application")
If @ERROR = 0
$Excel.DisplayAlerts = 0
$WorkBook = $Excel.WorkBooks.Open($path)
If @ERROR = 0
$WorkSheet = $WorkBook.Sheets.Item($sheet)
If @ERROR = 0
If $password
$= $WorkSheet.Unprotect($password)
Endif
$WorkSheet.Range($range).Value = $value
If $password
$= $WorkSheet.Protect($password)
Endif
$= $WorkBook.Save()
$WorkSheet = 0
Endif
$WorkBook = 0
Endif
$Excel.Quit
$Excel = 0
Endif
Exit @ERROR
EndFunction
|
|
Top
|
|
|
|
#179442 - 2007-08-17 04:40 PM
Re: Script Request
[Re: Shawn]
|
Mart
KiX Supporter
   
Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
|
|
|
Top
|
|
|
|
#179448 - 2007-08-17 07:10 PM
Re: Script Request
[Re: Mart]
|
Templar
Fresh Scripter
Registered: 2002-04-11
Posts: 41
Loc: Cincinnati
|
Will that last set of code with the password in it open a spread sheet that is password protected?
|
|
Top
|
|
|
|
#179451 - 2007-08-17 08:00 PM
Re: Script Request
[Re: Templar]
|
Templar
Fresh Scripter
Registered: 2002-04-11
Posts: 41
Loc: Cincinnati
|
Also if I needed to edit another cell say I already am editing cell B5 but I need to edit both cell B5 and then add some other value into Call B6. Where would I add in the code to update B5 and B6?
|
|
Top
|
|
|
|
#179453 - 2007-08-17 08:07 PM
Re: Script Request
[Re: Templar]
|
Templar
Fresh Scripter
Registered: 2002-04-11
Posts: 41
Loc: Cincinnati
|
The password is workbook. The script works like a champ but when it is opening the file I get a prompt for the password. I can manually type it in and the script does it's thing. If I can get that and add another cell to edit I'm golden.
|
|
Top
|
|
|
|
#179455 - 2007-08-17 09:12 PM
Re: Script Request
[Re: Shawn]
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11628
Loc: CA
|
We'll pretend we didn't hear Shawn ask how to protect a workbook
|
|
Top
|
|
|
|
#179457 - 2007-08-17 10:09 PM
Re: Script Request
[Re: Shawn]
|
Templar
Fresh Scripter
Registered: 2002-04-11
Posts: 41
Loc: Cincinnati
|
The password is set when the xls file is saved. They went in to General options when they were savign the file and set a password upon open in the options. So if you were opening it normally it prompts you to type in the password or open it as read only.
The cells will not change all I'm updating is two cells and only two cells. cells like E7 and E8 I figured out how to change it on my onw.
Basicly this spread sheet is to track a systems up time. So each day we have to go into the spread sheet and mark if the system is up or down and leave initials. We do this each day and each month. I was able to igure out how to update the cells each day by using some of the Day and Day of Month veriables. Each of the worksheets is a month and year like AUG-07. I've got that down as well. I just need to get a second cell and the password thing taken care of.
|
|
Top
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
1 registered
(Allen)
and 1198 anonymous users online.
|
|
|