Executing VBA out of Shell - shell

I am generating Excel Files with Pentaho Data Integration and I want to start a Macro automaticly after creation.
Until now, the Macro is started while opening the file.
But this is not a good way: Some users dont have permissions to execute Macros and each time you open the file Excel is asking if you want to save the changes.
I am wondering if there is a way to execute a VBA Macro in MS Excel out of the Windows Shell.
What I found is this code to open a file:
Shell "C:\Programme\Office2k\Office\EXCEL.EXE " & _"C:\...\MyExcelFile.xls"
But this is not what I want. I want to start the Macro exactly one time, and before any user opened it.
Do you have any ideas?

The solution with vbscript looks like this (Open, Save, Close without User Interaction):
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\dev\testo.xls")
objExcel.Application.Run "testo.xls!test"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Quit

Related

How to execute VBS?

I found how to make this
Download_Example
I have a question about how to make execute VBS in vb6 (VBS haves form3 (from vb6 project .)show)
I made a dialog with Microsoft common dialog control 6.0
CommonDialog1.Filter = "File (*.vbs)|*.txt|All Files (*.*)|*.*"
CommonDialog1.DefaultExt = "vbs"
CommonDialog1.DialogTitle = "Select File"
CommonDialog1.ShowOpen
The FileName property gives you the variable you need to use
A work-around might be just executing the script using Shell.
Shell "wscript.exe c:\myscript.vbs", vbNormalFocus
Shell "wscript.exe " & CommonDialog1.FileName, vbNormalFocus
See Microsoft's wscript documentation.
vbNormalFocus is there to restore focus to your vb6 program. It is optional but you probably want it. See documentation.
Looks like you are trying to run a VBScript from your VB6 app to open a dialog in the VB6 app.
VB6 -> VBScript -> Same VB6
You cannot do this with Shell since it runs the script as a separate process. The Script does not know what Form3 is because it is a component of the VB6 app and would not exist as a separate entity once the app is compiled.
Edit: Looks like what you want to do is possible but with Microsoft Script Control. Here are a few examples. Thank you #GSerg for pointing this out.
This or this might be used as a work-around but I don't think it is the right way to go.
Go back to your requirements. What exactly are you trying to accomplish? There has to be a better way.

Wscript does not run .vbs script due to an excel message box which pops up during refresh process

Task: I am currently working on an automation task via Windows Task Scheduler, so users can have a daily / weekly report.
Issue: I am running .vbs script, from a .bat file. The macro in the .vbs script refreshes all the pivot tables / slicers within the report and saves updated report, then it closes the Excel report application.
During testing I noticed that the .bat file stops at the point of running the refresh because Excel report comes up with the below msgBox message and then requires YES/NO in order to continue thereafter that another msg box asking to click YEs to replace the current file after refresh.
Below are the two msgbox that comes up in Excel:
Question: How do I write the .vbs script to automatically click yes to the msg boxes instead of doing it manually, there is no point of automation if there is any manual work needed. I need a clean smooth automation and I have tried to write the .vbs script in different ways.
the .vbs script below:
'Input Excel File's Full Path
'ExcelFilePath = ***Excel report file path
'Input Module/Macro name within the Excel File
MacroPath = "RefreshReport"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = True 'or "False"
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False 'or "True"
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.Run MacroPath
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True 'or "False"
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
'Leaves an onscreen message!
'MsgBox "Your Automated Task successfully ran at " & TimeValue(Now), vbInformation
Have you tried sending {Enter} in the script to accept the MsgBox?
set shell = createobject("wscript.shell")
shell.sendkeys ("{Enter}")
https://ss64.com/vb/sendkeys.html

How do I display a message box once I quit wscript

I have been trying to learn Web Scraping using VBScript since VBA for Outlook does not work like VBA for Excel
So I am trying to display a message box to see whether or not my script is getting to specific points. I can display the first message box but not the second one. I understand that it is due to the line wscript.quit but I need that line otherwise I can't open IE.
Msgbox ("hello"),0,("title")
set Shell = createobject("wscript.shell")
Shell.Run "google.ca"
wscript.quit
DIM IE
Set IE = CreateObject("IExplorer.exe")
Msgbox ("hello"),0,("title")
All help is appreciated, Thanks.

Controlling Microsoft word options with VBScript

I have compiled 2 VBScript .vbs files in an attempt to control the use of smart quotes (also known as curly quotes) in Microsoft Word. I am experimenting with using VBScript to undertake Microsoft Word functions.
The outcome I would like is as follows: whilst having a Microsoft word document open, I would like to be able to open one of the .vbs files to turn smart quotes on effective immediately, and conversely be able to open the other .vbs file to turn smart quotes off effective immediately.
Unfortunately, whilst having a Microsoft word document open, running these scripts by double clicking the appropriate .vbs file appears to have no effect.
However, if I open (by double clicking) one of the .vbs files whilst Microsoft word is closed, and then open Microsoft word, the smart quotes settings will reflect the script in the .vbs file.
I have reproduced the scripts from the .vbs files below. There is a line of junk code in each one preceded by an apostrophe - as I said I have been experimenting.
How do I amend the scripts to achieve the aforementioned outcome?
Any assistance is greatly appreciated. Stuartzz
Script (in a .vbs file) for turning smart quotes off:
On Error Resume Next
Set objWord = CreateObject("Word.Application")
'objWord.Visible = True
Set objOptions = objWord.Options
objOptions.AutoFormatAsYouTypeReplaceQuotes = False
objOptions.AutoFormatReplaceQuotes = False
ObjWord.Quit
Script (in a .vbs file) for turning smart quotes on:
On Error Resume Next
Set objWord = CreateObject("Word.Application")
'objWord.Visible = True
Set objOptions = objWord.Options
objOptions.AutoFormatAsYouTypeReplaceQuotes = True
objOptions.AutoFormatReplaceQuotes = True
ObjWord.Quit
VBScript version 5.8.7601.16978
.net framework version v4.0.30319
Windows 7 Ultimate Service Pack 1 64-bit operating system
Microsoft Office Professional Plus 2010
Microsoft Word 14.0.5128.5000 (64-bit)
Using CreateObject will start a new instance of the Word application. It will not affect a currently running one. To get a currently running instance, you need to use GetObject.
So, instead of this:
Set objWord = CreateObject("Word.Application")
Use this to grab first instance of Word:
Set objWord = GetObject(, "Word.Application")
All of that being said, if you used a macro written in VBA, it would always run within the currently open file. You could even apply a toolbar button for easier access.

How to set focus on file dialog opened in Vbscript

Our Team is automating tests/test data preparation in QTP and we do the scripting in VBScript.
In several tests the tester who runs the script need to supply an MS-Excel file with the indata. We use UserAccounts.CommonDialog for this and it works great. Except for one litle problem, when we run this from QTP the file dialog does not get focus. It's opened in the background and it's easy for the tester that runs the script to miss it and waste time waiting for the dialog.
How do we give the file dialog focus?
Code Example:
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "MS-Excel Spreadsheets|*.xls|All Files|*.*"
while ObjFSO.ShowOpen = false
msgbox "Script Error: Please select a file!"
wend
msgbox "You selected the file: " & ObjFSO.FileName
My guess is that since the dialog is modal, the ShowOpen method doesn't return the execution control back to the script until the dialog is closed. So there's no way to interact with the dialog as part of your test script.
As a workaround, you could spawn a parallel script that would wait for the dialog and activate it. But I guess QTP cannot run two scripts in parallel, so you'll probably need an external shell script (written in VBScript / JScript / PowerShell / etc).
Edit: Try the following:
Create an external VBScript file (.vbs) with the following contents:
Set oShell = CreateObject("WScript.Shell")
While Not oShell.AppActivate("Open")
WScript.Sleep 500
Wend
This script calls WshShell.AppActivate to activate a window whose title contains Open (replace it with the actual dialog title). If there's no such widnow at the monent, it retries the attempt after 0.5 sec (you can increase the delay if you wish).
Launch this script from your QTP test before opening the dialog. (Use SystemUtil.Run or something like this.)
I'm not sure, but I think this should do the trick.
Did you try recording a click on the dialog - so that QTP will click on it to set focus before proceeding?

Resources