How to run a simple VBA function in Outlook from a PowerShell script

Ludovic T 0 Reputation points
2025-03-18T13:16:57.3366667+00:00

Hi,

I've have a simple function in an Outlook VBA module that I want to run from a PowerShell script.

MySub_in_Outlook

I'm already sending emails using PowerShell so I guess my script should start with something like:

$outlook = new-object -comobject outlook.application 

But I don't know how to call the VBA function.

Would anybody have done that already? Thanks for any advise.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2025-03-18T18:03:37.35+00:00

    You can try something like this:

    # Create the Outlook application object
    $outlookApp = New-Object -ComObject Outlook.Application
    # Get the VBA project
    $vbaProject = $outlookApp.Project1	# or maybe ThisOutlookSession????
    # Run the macro
    $vbaProject.Application.Run("MySub")
    

    Note: Not tested!

    I'm not familiar with VBA/Outlook. I'm not sure of the name used as the project.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.