0

I need to automate something in excel. The first step should be to run a macro that is started by clicking a VBA-Button. Is there a way I can click a VBA-Button using Powershell?

Thanks!

Community
  • 1
  • 1
Steff
  • 143
  • 2
  • 13
  • Probably....... – Tom Jul 05 '17 at 12:46
  • 4
    Humans click buttons, computers just run the script directly. Check out [this question to run an excel macro from powershell](https://stackoverflow.com/questions/19536241/calling-excel-macros-from-powershell-with-arguments) – JNevill Jul 05 '17 at 12:53
  • I i run the macro like this, a "browse file box" pops up and i have to select files. How do I handle this? – Steff Jul 05 '17 at 13:02

2 Answers2

0

Provide macro name hidden inside this button, as an argument of cmd command.

Steps for you:

  1. get macro name (main sub) from VBA project.
  2. get path of excel application on your system.
  3. get path of excel file where VBA macro is stored.

Try running with Powershell:

"path_to_excel_app" "path_to_excel_file_with_macro" /x macro_name

Here is my working exaple with MS access:

"C:\Program Files (x86)\Microsoft Office\Office15\MSACCESS.EXE" "R:\path_to_macro_file\AccessFileWithMacro.accdb" /x macroName
0

You only need to run this PS

# start Excel
$excel = New-Object -comobject Excel.Application
#open file
$FilePath = 'FilePath'
$workbook = $excel.Workbooks.Open($FilePath)
##If you will like to check what is happend
$excel.Visible = $true
## Here you can "click" the button
$app = $excel.Application
$app.Run("MacroName")

       ##The macro name is the trigger of your button