Wednesday, 26 February 2025

Trigger a Power Automate Cloud Flow and Power Automate Desktop Flow using VBA in Excel

 

Trigger a Power Automate Flow Using VBA in Excel

You can trigger a Power Automate Flow from VBA in two ways:

1️⃣ Using an HTTP Request (via Power Automate's HTTP Trigger)
2️⃣ Using Power Automate Desktop (via UI Automation or Command Line Execution)


1. Trigger Power Automate Flow via HTTP Request

Steps to Follow:

  1. Create a Power Automate Flow with an HTTP Trigger

    • Go to Power Automate.
    • Click Create > Automated Cloud Flow.
    • Select "When an HTTP request is received" as the trigger.
    • Click "Save", and Power Automate will generate a POST URL.
  2. Copy the HTTP Request URL

    • This URL is needed in VBA to trigger the flow.
  3. Use VBA to Call the Power Automate Flow

    • Add this VBA code in your Excel file:
vba
Sub TriggerPowerAutomateFlow() Dim http As Object Dim JSONBody As String Dim URL As String ' Set Power Automate Flow URL (Replace with your flow URL) URL = "https://prod-XX.westus.logic.azure.com:443/workflows/XXXXXXXXXXXXXX/triggers/manual/paths/invoke?api-version=2016-10-01&sp=/triggers/manual/run&sv=1.0&sig=YOUR_SECRET_KEY" ' Set JSON payload (optional) JSONBody = "{""message"": ""Triggered from Excel VBA""}" ' Create HTTP Object Set http = CreateObject("MSXML2.XMLHTTP") ' Send HTTP POST Request With http .Open "POST", URL, False .setRequestHeader "Content-Type", "application/json" .send JSONBody End With ' Confirm Flow Triggered If http.Status = 200 Or http.Status = 202 Then MsgBox "Power Automate Flow Triggered Successfully!", vbInformation Else MsgBox "Error: " & http.Status & " - " & http.responseText, vbCritical End If ' Clean up Set http = Nothing End Sub

How It Works:

Triggers the Power Automate flow using an HTTP POST request.
Sends JSON data (optional) to Power Automate.
Checks for success or failure (HTTP status 200 or 202).


2. Trigger Power Automate Desktop (PAD) Flow Using VBA

If you want to trigger a Power Automate Desktop (PAD) flow, you can use a Shell command.

Steps:

  1. Create a PAD Flow

    • Open Power Automate Desktop.
    • Create a new flow.
    • Click "Run URL" and copy the PowerShell command.
    • To get "Run URL" click on insist button and you will find the URL.
  2. Use VBA to Run Power Automate Desktop

vba

Sub RunPADFlowViaPowerShell() Dim shell As Object Set shell = CreateObject("WScript.Shell") ' Run Power Automate Desktop Flow via PowerShell shell.Run "powershell.exe Start-Process 'ms-powerautomate:/console/flow/run?environmentid=Default-0f9f9044-e7d6-4603-b2dc-f0d91fd6ede6&workflowid=77d240b0-00a4-44fe-b948-90b47a51e089&source=Other'", 0, False Set shell = Nothing MsgBox "PAD Flow Triggered!", vbInformation End Sub

Triggers Power Automate Desktop without an HTTP request.
Good for local automation workflows.


Which Method is Best?

MethodUse CaseWorks on Cloud?
HTTP Request (Method 1)Best for triggering cloud-based Power Automate flows from Excel✅ Yes
Power Automate Desktop (Method 2)Best for triggering local automation tasks on your PC❌ No

No comments:

Post a Comment