Copilot-generated desktop flow fails on Excel automation with dynamic selectors

I used Copilot in Power Automate Desktop to generate a flow that automates Excel data entry from our ERP system. The flow works perfectly during recording and initial testing, but fails in production when Excel window titles change or when multiple Excel instances are open.

Copilot generated this selector for the Excel window:


Selector: :WindowInstance[Name="Book1 - Excel"]

The issue is that production files have dynamic names like “Invoice_20250103.xlsx - Excel” or “Report_Q4_Final.xlsx - Excel”. The automation is blocked because it can’t find the exact “Book1” window name.

I’ve tried manually editing the selector to use wildcards (Name="*Excel"), but then it targets the wrong Excel window when multiple instances are running. The Copilot flow generation didn’t account for dynamic selector usage or provide any window identification logic beyond the literal recorded name.

What are the Excel automation best practices for handling dynamic window titles in Power Automate Desktop? How should I modify Copilot-generated selectors to work reliably in production environments with variable file names?

Copilot-generated flows are great starting points but they record literal values from your session. For dynamic scenarios, you need to enhance the selectors with variables and window management logic that Copilot doesn’t automatically add.

That makes sense. Is there a way to pass the expected filename as a variable and use it in the selector? Or should I be using a different approach entirely for targeting specific Excel windows?

Yes, use variables in selectors. Modify Copilot’s generated selector to: :WindowInstance[Name="%ExcelFileName% - Excel"] where ExcelFileName is an input variable. But there’s a better approach: use the “Launch Excel” action to get a window handle, then reference that handle in subsequent Excel actions instead of using window selectors. This eliminates the window identification problem entirely.

Here’s a complete solution covering all three focus areas:

Copilot Flow Generation Enhancement: Copilot creates flows based on literal recording sessions, which is excellent for rapid prototyping but requires enhancement for production use. The key is understanding what Copilot generates versus what you need to add manually:

Copilot provides:

  • Basic action sequence
  • Literal selectors from recording
  • Simple data flow between actions

You must add:

  • Variable-based selectors
  • Window handle management
  • Error handling and retry logic
  • Multi-instance scenarios

Treat Copilot output as a template that captures your process logic, then enhance it with production-grade patterns.

Dynamic Selector Usage: Replace Copilot’s static selector with dynamic alternatives:

Option 1 - Variable-based selector:


:WindowInstance[Name="%FileName% - Excel"]

Set FileName variable from your flow inputs or upstream actions.

Option 2 - Window handle approach (RECOMMENDED):

Replace UI automation with Excel instance management:

  1. Remove Copilot’s “Focus window” action
  2. Add “Launch Excel” action at flow start:
    • Set “Launch Excel” → Opens file: %FilePath%
    • This returns ExcelInstance variable
  3. Use ExcelInstance in all subsequent Excel actions
  4. Actions reference the instance object, not window selectors

Example transformation:

Copilot generated: “Click UI element in window [Name=‘Book1’]”

Production version: “Write to Excel worksheet [Excel instance: %ExcelInstance%]”

Option 3 - Advanced selector with partial matching:


:WindowInstance[Name~=".*Excel$"]

Use regex patterns for flexible matching, but add position or process ID attributes to disambiguate multiple instances.

Excel Automation Best Practices:

  1. Use Native Excel Actions Over UI Automation:

    • Copilot often generates UI clicks and keyboard inputs
    • Replace with dedicated Excel actions: “Write to Excel worksheet”, “Read from Excel worksheet”, “Run Excel macro”
    • Native actions are faster, more reliable, and don’t require window focus
  2. Window Management Pattern:

    
    // At flow start
    Launch Excel [FilePath: %InputFile%] → ExcelInstance
    
    // During processing
    Write to Excel worksheet [Instance: %ExcelInstance%, Range: A1]
    
    // At flow end
    Close Excel [Instance: %ExcelInstance%, Save: Yes]
    
  3. Handle Multiple Excel Instances: If your flow must work with multiple files simultaneously:

    • Launch each with separate “Launch Excel” actions
    • Store instances in distinct variables (ExcelInstance1, ExcelInstance2)
    • Reference the specific instance variable in each action
    • Never rely on window focus or active window assumptions
  4. Error Handling for Excel Operations: Enhance Copilot’s flow with:

    
    On block error:
      Wait 2 seconds
      Retry current action
      If retry fails after 3 attempts:
        Log error details
        Send notification
        Close Excel gracefully
    
  5. File Path Management:

    • Use full paths, not relative: “C:\Data%FileName%” not “./Data/%FileName%”
    • Validate file exists before launching Excel
    • Add “Wait for file” action if file is generated by upstream process
  6. Performance Optimization:

    • Disable screen updates: Set Excel.ScreenUpdating = False via macro
    • Batch write operations instead of cell-by-cell
    • Use “Write to Excel worksheet” with range parameter for bulk data

Migration Strategy from Copilot Output:

  1. Identify all UI automation actions targeting Excel
  2. Map each to equivalent native Excel action
  3. Replace window selectors with Excel instance references
  4. Add error handling blocks around Excel operations
  5. Test with multiple file names and concurrent Excel instances
  6. Add logging to track which file is being processed

Specific Fix for Your Scenario:


// Replace Copilot's window-based approach:
Focus window [Name="Book1 - Excel"]
Click UI element [Selector: ...]

// With instance-based approach:
Launch Excel [FilePath: %InputFilePath%] → ExcelInstance
Write to Excel worksheet [
  Instance: %ExcelInstance%,
  Range: "A1",
  Value: %ERPData%
]
Close Excel [Instance: %ExcelInstance%, Save: Yes]

This eliminates selector issues entirely and works regardless of filename, window position, or number of open Excel instances.

Testing Checklist:

  • Different filenames (dynamic values)
  • Multiple Excel windows open simultaneously
  • Excel not visible (minimized or background)
  • File locked by another process
  • Network path delays
  • Excel prompts (save changes, macro security)

The fundamental principle: Copilot generates UI automation because that’s what it observes during recording. Production flows should use object-based automation (Excel instance) whenever possible, reserving UI automation only for scenarios where native actions don’t exist.