Update an excel spreadsheet from vbscript

Posted on 2014-08-27
Last Modified: 2014-10-01
I am trying to write a vbscript that will open a spreadsheet, update values and then call a procedure.  This is what I have so far, but I am getting an error when attempting to write values to a specific sheet.  What am I missing?

Option Explicit
Const vbNormal = 1         ' window style

DIM wshShell, EnvVar, CurrentDefPrinter, ReportPrinter
'Create a new Windows Script Host Shell Object
Set wshShell = CreateObject("Wscript.Shell")
'Set it to read the env variables
Set EnvVar = wshShell.Environment("Process")
ReportPrinter = EnvVar.Item("AF11x17Printer")

DIM objXL, objWb, objWs, objR, objNet     ' Excel object variables
DIM file, st, gfuncs, installpath
file = "C:\REP_Reports\Report Design\WIKSWT_MON10.xls"

'--Start Excel and run it visibly
Set objXL = WScript.CreateObject("Excel.Application")
objXL.Visible = True
objXL.DisplayAlerts = true

'-- Open the file
Set objWb = objXl.WorkBooks.Open(file)

set objWs = objWb.ActiveWorkbook.Worksheets(4)
objWs.cells(24,3).Value = 0
objWs.cells(25,3).Value = "AUTO"
objXL.Run "AutoRunCTCalcs"

'--Close the workbook and Excel
objXL.ActiveWorkbook.Close False
Set objwb = Nothing
Set objXL = Nothing
Question by:AaronGreene1906
    LVL 65

    Expert Comment

    Try changing this
    set objWs = objWb.ActiveWorkbook.Worksheets(4)
    to this
    set objWs = objWb.Worksheets(4)

    You don't need ActiveWorkbook since you are already reference it with objWb


    Author Comment

    That worked perfectly.  I can update values as needed, but the last line is still a problem.  I can't get it to call that procedure.  Should it be

    objXL.Run "C:\REP_Reports\Report Design\WIKSWT_MON10.xls!AutoRunCTCalcs"
    LVL 65

    Accepted Solution

    You should be able to use
    objXL.Run Mid(file, InStrRev(file, "\") + 1) & "!AutoRunCTCalcs"

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
    This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now