0

I have a working VBScript file and VBA macro inside Excel worksheet that does this:

  • Refreshes all data connections
  • Writes a timestamp in a specific cell
  • Save and closs the Excel worksheet

VBS file:

 Set objExcel = CreateObject("Excel.Application")

 objExcel.Application.Run "'G:\Blank.xlsm'!Module9.Date"
 objExcel.DisplayAlerts = False
 objExcel.Application.Quit

 Set objExcel = Nothing

VBA inside the Blank.xlsm worksheet:

Sub Date()
    ActiveWorkbook.RefreshAll

    With Range("M12")
        .Value = Now()
        .NumberFormat = "dd/mm/yy hh:mm"
        ActiveWorkbook.Save
    End With
End Sub

Is it possible to keep the Excel macro-free .xslx file and run both of those functions from a VBScript file, which would not call the macro inside the Excel workbook to do the things I need, but rather complete those tasks by itself? I'm very new to VBScript (and frankly, VBA, too), so I'm sorry if this comes as too basic of a question.

3
  • Look into using an XLAM file (Excel Add-in) which can operate on a code free XLSX file. Commented May 17, 2018 at 18:36
  • 1
    Are you sure your VBA code compiles as is? You can't have a method named Date! Commented May 17, 2018 at 18:41
  • Please don't edit the question to add additional requirements after receiving an answer and accepting it. If you have another question, you should ask a new question. Commented May 17, 2018 at 20:47

1 Answer 1

2

Yes, of course, it's possible. Here:

Option Explicit

Dim objExcel, objWorkBook

Set objExcel = CreateObject("Excel.Application")
Set objWorkBook = objExcel.Workbooks.Open(filePath)

objExcel.Application.Visible = False
objExcel.DisplayAlerts = False

objWorkBook.RefreshAll

With objWorkBook.Sheets(1).Range("M12")
    .Value = Now()
    .NumberFormat = "dd/mm/yy hh:mm"
End With

objWorkBook.Save
objWorkBook.Close

objExcel.Application.Quit
WScript.Echo "Finished."
WScript.Quit
Sign up to request clarification or add additional context in comments.

3 Comments

Works like a charm, big thanks! I added a write protection for the workbook though and struggling to properly add password operator to the Worksbooks.Open method - will update the OP.
@dotsent12 Please don't edit the question to add additional requirements. If you have another question, you should ask a new question.
Anyway, for this specific issue, you don't have to ask a new question because it's been asked before several times. Since VBScript doesn't support named arguments, you should use objExcel.Workbooks.Open (filePath,,,,, "WritePassword").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.