VB code executed with WScript to open a spreadsheet and run a named macro

steallan

Active Member
Joined
Oct 20, 2004
Messages
304
Ok this one is a bit unusual.

I'm actually using an Alteryx Event to run Wscript.exe, to run a bit of VB code. The code is supposed to open a workbook and run a VBA macro in there.

VBA macro is fine, works like a charm when I run it manually. I have no issues in Alteryx, its the darn VB code I cant get right. Anyone here good with that?

Example code below, pinched from the internet. All it needs do is open the excel file, and run the single macro, then once the macro is done, stop and shut down the excel file.

I cant get it to work though. Error messages about not being able to access, or cant find, etc. Tried a few different things. The internet has various posts by people trying to achieve similar outcomes, but all examples/solutiion are just slighty different enough to not help me.

Anyone done anything like this before and can help with the right VB code? So the excel file containing the VBA macro is called AlteryxMacros.xlsm, and the macro saved in there is called Rename.

Below are two examples i've failed with:

1.

Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run \\uug.vcm.cc\Domestic_Retail\CollectionsAnalyticsTeam\StephenAllan\Alteryx\AlteryxMacros.xlsm!Sheet1.Rename
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing


one thing seems to be how to call the macro and if its in a module or a sheet. I'm not sure if my macro is in sheet1 or a module, how do you tell?

2.

dim objExcel
set objExcel = CreateObject("Excel.Application")
objExcel.Visible = TRUE
objExcel.DisplayAlerts = FALSE

Path1="\\uug.vcm.cc\Domestic_Retail\CollectionsAnalyticsTeam\StephenAllan\Alteryx\AlteryxMacros.xlsm"

Set objWorkbook = objExcel.Workbooks.Open(Path1, 0, True)

Dim fso, path, file, recentDate, recentFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set recentFile = Nothing
For Each file in fso.GetFolder("\\uug.vcm.cc\Domestic_Retail\CollectionsAnalyticsTeam\DCA\Invoices\Input\").Files
If (recentFile is Nothing) Then
Set recentFile = file
ElseIf (file.DateLastModified > recentFile.DateLastModified) Then
Set recentFile = file
End If
Next

If recentFile is Nothing Then
WScript.Echo "no recent files"
Else
objExcel.Workbooks.Open recentFile
End If

objExcel.Run("AlteryxMacros.xlsm!Rename")

objWorkbook.Saved=False

objExcel.quit


So in the above, much bigger example, the Macro ChangeSheetName is called without specifiying that its in sheet1. There's also a lot of extra code in the centre, sometthing about each file the the folder, but I dont see why that bit is needed. Just run the VBA macro!

Any help would be much appreciated.

Thanks

Stephen
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
5,951
Anyone done anything like this before and can help with the right VB code? So the excel file containing the VBA macro is called AlteryxMacros.xlsm, and the macro saved in there is called Rename.
First, Wscript.exe runs VBScript or JScript code, not VB. Try this as your VBScript:
Code:
Dim objExcel
Set objExcel = CreateObject("Excel.Application")
With objExcel
  .Workbooks.Open "\\uug.vcm.cc\Domestic_Retail\CollectionsAnalyticsTeam\StephenAllan\Alteryx\AlteryxMacros.xlsm"
  .Visible = True
  .Run "Rename"
  .ActiveWorkbook.Close True
  .Quit
End With
The Rename macro must be in a (standard) module in AlteryxMacros.xlsm, and declared as either
Sub Rename()
or
Public Sub Rename()

To determine whether the Rename macro is in a module or a sheet module, look at the title bar in the VBA editor. It will include "SheetX" if it's in a sheet module. You can also look at the Project Explorer and via 'Toggle Folders' (the yellow folder icon), show the folders tree of Microsoft Excel Objects (sheet modules and ThisWorkbook), (standard) Modules, Forms and Class Modules. For example (from Excel Visual Basic (VBA) Editor: Complete And Easy Guide To The VBE):

 

steallan

Active Member
Joined
Oct 20, 2004
Messages
304
I'm going to read all of the guide to VBA today, thanks for linking that.

VBScripts, not VB - thanks for pointing that out, I've been calling it the wrong thing like a chump.

and lastly, the Vbscript works! I can now Event call my workbook and run my Macros. This is a massive help, thank you John. You've helped me a lot recently, on other posts as well. I really appreciate it. The amount of people you must have helped over the years through this forum is incredible.
 

Forum statistics

Threads
1,078,290
Messages
5,339,331
Members
399,293
Latest member
maras

Some videos you may like

This Week's Hot Topics

Top