web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Help with a Complicate...
Power Automate
Answered

Help with a Complicated Flow (open Excel, refresh table, run macros)

(0) ShareShare
ReportReport
Posted on by 95

Hi everyone

 

I'm working on creating a flow that does the following:

1. Opens an Excel file in a static location (the file is an Export from a SharePoint list) and click the "refresh all" button

>>> Let's call this "Master File"

2. Save the Master File and keep it open

3. Open an Excel file in another static location (the file location is static, but the naming convention is dynamic - "FileName-0707" this week...and next week, it'll be "FileName-0714")

>>> Let's call this "Sync File"

4. In the Sync File, run 2 macros

5. Save the Sync File.

 

Any thoughts on whether this is possible? I tried recording but got some mixed results.

 

Thanks a lot in advance for the advice 🙂

I have the same question (0)
  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Don’t see why not. 

    for the date, you just need to run through some logic. 

    If you run this, say on a Wednesday, do you want it to run for this Friday or last Friday?

     

    the logic is to set a base Friday, Ex 06/30/2023. 

    now you’ll use that to calculate all Fridays after. So if I run any day after the Friday I need the file name, I say:

     

    Subtract Dates (today - base Friday) = DayDifference

     

    Day Difference divided by 7 gives Weeks (with decimal)

     

    Truncate Weeks gives an integer of weeks from BaseFriday

     

    Add Weeks(integer) to Base Friday to get the TargetFriday

     

    Convert TargetFriday to text for the format you want ex. MM-dd

     

    now you can add that text to your filename to make it dynamic. 

    hope this helps. 

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Example:

     

    DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateAndTime CurrentDateTime=> CurrentDateTime
    /# we get the number of days from current date to our base monday
    we divide that number by 7 to get the number of weeks (with a partial week as a decimal)
    we remove the decimal to get the number of flat weeks
    we add those weeks back to the base monday to get the monday for this week
    we calc beg/end of period from the day before thismonday to a week before thismonday#/
    DateTime.Subtract FromDate: CurrentDateTime SubstractDate: BaseMonday TimeUnit: DateTime.DifferenceTimeUnit.Days TimeDifference=> DayDifference
    SET WeekDifference TO DayDifference / 7
    Variables.TruncateNumber.GetIntegerPart Number: WeekDifference Result=> WeeksFromBase
    DateTime.Add DateTime: BaseMonday TimeToAdd: WeeksFromBase * 7 TimeUnit: DateTime.TimeUnit.Days ResultedDate=> ThisMonday
    DateTime.Add DateTime: ThisMonday TimeToAdd: -1 TimeUnit: DateTime.TimeUnit.Days ResultedDate=> EndOfPeriod
    DateTime.Add DateTime: ThisMonday TimeToAdd: -7 TimeUnit: DateTime.TimeUnit.Days ResultedDate=> BegOfPeriod
    # we put the beg/end of period into text format so we can populate clockshark
    Text.ConvertDateTimeToText.FromDateTime DateTime: BegOfPeriod StandardFormat: Text.WellKnownDateTimeFormat.ShortDate Result=> BegOfPeriod_ShortDate
    Text.ConvertDateTimeToText.FromDateTime DateTime: EndOfPeriod StandardFormat: Text.WellKnownDateTimeFormat.ShortDate Result=> EndOfPeriod_ShortDate
    Text.ConvertDateTimeToText.FromCustomDateTime DateTime: EndOfPeriod CustomFormat: $'''MM.dd.yyyy''' Result=> EndOfPeriod_mmddyyyy_Dots

  • Verified answer
    Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Do not use the UI to refresh the connections in your file. It's unstable. Instead, use the following PowerShell script. We use it all the time for projects where we need to refresh the connections in a file:

     

    # Excel input file
    $xlsx = "%InputFilePath%"
    
    # Create Excel COM object
    $excel = New-Object -ComObject Excel.Application
    
    # Make excel invisible
    $excel.Visible = $false
    
    # Disable excel alerts
    $excel.DisplayAlerts = $false
    
    # Open excel workbook
    $wb = $excel.Workbooks.Open($xlsx)
    
    # initiate refresh of the connections
    $wb.RefreshAll()
    
    # get all the connections in the work book
    $connections = $wb.Connections
    
    # for each connection wait while it is refreshing
    for ($i = 1; $i -le $connections.Count; $i++)
    {
     $connection = $connections[$i]
     while ($connection.ODBCConnection.Refreshing)
     {
     Start-Sleep -Seconds 3
     }
    }
    
    # Save
    $wb.Save()
    
    # Cleanup (quit, garbage collection)
    $excel.Quit()
    Remove-Variable -Name Excel
    [GC]::Collect()
    [GC]::WaitForPendingFinalizers()

     

    This will launch Excel, open the document, refresh all connections, wait for the refresh to complete, save the file and close Excel.

    You just need to pass the path to the file as %InputFilePath%. Since it's a static file, it should be simple enough.

     

    Then, if you want it open, you can open it again via the Launch Excel action. Or, you can modify the PowerShell script to make it not close Excel at the end (delete the part that goes after $wb.Save() ). And then use Attach to running Excel in PAD to create an Excel instance variable that you can use later to close this Master File.

     

    Launching the other file is simple. You can use Get files in folder with a name filter and then apply sorting to get the latest file. Alternatively, if, for example, you always run your flow on the date that is in the Sync File name (or the day after), you can calculate it by using Get current date and time then Add to date time (if needed to deduct 1 day for example) and then Convert text to date time to get it formatted as MMdd.

     

    Then, finally, there is an action to Run Excel macro. So, when you have your files open, you can use this action to run your macros on the Sync file. Finally, use Save Excel and Close Excel to finish.

     

    Make sure you close both files, so none of them stays open. You might want to initially keep the Excel instances visible, so you can see it and close it manually in case something breaks. But when you're done, you can definitely run the entire flow in the background with both instances being invisible. No need to use any UI at all.

     

    In case something breaks and your file gets locked because it is open in another app, simply terminate the Excel process via the Task manager. This can happen if you launch an invisible instance of Excel and forget to close it.

     

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution.
    If you like my response, please give it a Thumbs Up.

    If you are interested in Power Automate, you might want to follow me on LinkedIn at https://www.linkedin.com/in/agnius-bartninkas/

     

  • Chris110 Profile Picture
    95 on at

    Thanks for the help everyone!!!

     

    Agnius - I used most of your solution. Here's where I ended up:

     

    Part 1 - Convert datetime to text to get the MMdd and Set variable for the location of where the file should save (e.g. C:\Users\me\sync folder\%YEAR%\Sync File-%YEAR%-%MMdd)

    Part 2 - Launch new Chrome and go to the application where the "Sync File" originates from. Use the Press button on web page actions to download the file. Save to the variable set in part 1.

    Part 3 - Launch Excel to open the Master File. Instead of using PowerShell, I used Send keys (Control-Alt-F5) to force the refresh all. I got a little "fancy" (well....fancy for me at least) and used Display message so I had a chance to check for errors, make sure everything looked ok, etc.

    Part 4 - Launch Excel to open today's Sync File. Used Run Excel macro for macro #1 and another for #2. Another Display message to check the data.

    Part 5 - Go back to the original Chrome instance from Part 1, click a few buttons, and import the changes.

    Part 6 - Close the Excel files

     

    DONE! Thanks again everyone!

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Great that it works for you, but I would have really avoided using keystrokes (or any other kind of Excel UI automation). But at least you now have an idea of what to do in case your UI automation breaks at some point, or if you want to improve your flow to make it unattended, run faster and be more stable. For now, if it works - that's all that matters 🙂

     

  • Chris110 Profile Picture
    95 on at

    Thanks! Yeah it's a good start...the total run time is about 5 minutes, including the time it takes for the file to refresh, macros to run, and data extract from the source system.

     

    In the last 15-20 minutes, I took your advice and used PowerShell to refresh the file. I'm always a little hesitant to use PowerShell because I'm not familiar with it...but I tested it out and it seems to be working.

     

    Thanks again for the advice and guidance! Looking forward to showing my team this week. If you have other thoughts about how I can speed this up or best practices, I'm all ears! (E.G. "Don't use Press button on web page - use XYZ instead")

     

    One thing I noticed...it seems to take the longest (a few seconds) when the file name is being populated in the Save as window in Chrome. "FileName" is defined as C:\Users\me\sync folder\%YEAR%\Sync File-%YEAR%-%MMdd (YYYY and MMdd are also defined when the flow starts)

     

    Chris110_0-1690128630593.png

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    How long does it take you to perform the task manually? It is normal for some processes to take longer to do automatically than it takes a human to do it manually, when there are some "finding" of data or elements involved. Unless it is absolutely critical to push for speed, I would focus on stability and consider the fact that those 5 will now run on their own and you can do something else in the meantime.

     

    It seems to me based on the flow you described that it is likely okay. Feel free to share a screenshot of the entire flow and then if I notice some areas for improvement, I'll let you know.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • Chris110 Profile Picture
    95 on at

    It typically takes me 15-20 minutes...but now it's down to about 5 minutes...

     

    "Perfection is the enemy of good" - and this automation is definitely in a VERY good state. Seeing the automation "stall" for about 10-15 seconds just makes me cringe a little on the inside haha I can absolutely live with it 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 632

#2
Haque Profile Picture

Haque 458

#3
Valantis Profile Picture

Valantis 357

Last 30 days Overall leaderboard