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 / Efficient Use of Attac...
Power Automate
Answered

Efficient Use of Attach to Running Excel

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I have a Flow that takes hundreds of PDFs from my email, drops them into a folder, then parses their names into vars and sends those vars to an Excel file with some VBA to handle what to do with them. This is spurred on by a For-Each. The For-Each loads up every parsable file name and turns those into the vars before kicking them to Excel.

 

VBA writes these vars into the workbook based on find/match criteria. If a var isn't findable, it just punts it and goes to the next in the For-Each loop.

 

The rub is such: In my current Flow Excel has to be opened, targeted, closed, and released each time the For-Each fires the next set of vars into VBA. VBA is running just fine, I don't think I could make that any more efficient.

 

Is there a better means of interacting with Excel through PAD? For example, could I simply leave out the command for PAD to release the instance of Excel after the For-Each is done cycling through all of the files? Is there a way to add these parsed file names to a list, then somehow break them out like the For-Each?

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

    So, I want to make sure I understand.  

     

    Step 1 - get all email attachments and save to a folder

    Step 2 - use Get Files on the folder, now I have %Files% where each file in Files is a full path/filename

    Step 3 - you are using Parse Text to extract variables from the filename

     

    Is Excel closing as part of the VBA?  I am confused as to why it is closing.

     

    As far as parsing out all your filenames to separate variables, the short answer is "yes".  Assuming they all have the same amount of variables:

     

    Get Files

    Add list (VarList1)

    Add list (VarList2)

    Add list (VarList3)

    For each CurrentItem in Files

    Parse Text to Var1, Var2, and Var3

    Add Item to List Var1 to VarList1

    Add Item to List Var2 to VarList2

    Add Item to List Var3 to VarList3

    End For Each

     

    Now when you have 4 lists: Files, VarList1, VarList2, VarList3...they all have everything you need for Excel, correct?

    Instead of For Each use Loop:

    Loop starting at 0 to %Files.Count - 1% increment 1

    When referencing the Filename, use %Files[LoopIndex].Name% or %Files[LoopIndex].NameWithoutExtension%

    When referencing variable 1, %VarList1[LoopIndex]%

    When referencing variable 2, %VarList2[LoopIndex]%

    When referencing variable 3, %VarList3[LoopIndex]%

     

    LoopIndex will always be the current item in the loop and the number (starting at 0) should always be the same if you never sort the lists.  For this to work, all filenames must produce the same number of variables from the parse text.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    You are correct about your first two assumptions. I'm writing an equal number of vars across the whole operation.

     

    Excel is closing separately from VBA. VBA is not causing Excel to close, PAD is.

     

    Beginning of the FlowBeginning of the Flow

    Here PAD is targeting the drop folder and taking all of the files into %Files% and doing that counter-set-to-zero thing you mentioned.

     

     

     

     

    Beginning of the For-Each file breakdown into VarsBeginning of the For-Each file breakdown into Vars

    This is showing some of the file name processing that I'm doing. Its a little messy lmfao.

     

     

     

    Open Excel, attach, close, release Excel. Double check to make sure all files that can be iterated have been.Open Excel, attach, close, release Excel. Double check to make sure all files that can be iterated have been.

     

    This screenshot shows how many vars I end up with when I'm done processing the file names for VBA.

     




    @MichaelAnnis wrote:

    So, I want to make sure I understand.  

     

    Step 1 - get all email attachments and save to a folder

    Step 2 - use Get Files on the folder, now I have %Files% where each file in Files is a full path/filename

    Step 3 - you are using Parse Text to extract variables from the filename

    This final screen shot is step 3 you mentioned. I have all 5 vars and I'm ready to send them to VBA at that point. Anything that has fewer than 5 vars has been punted and is not included in the final list of vars/files.

     


    @MichaelAnnis wrote:

    As far as parsing out all your filenames to separate variables, the short answer is "yes".  Assuming they all have the same amount of variables:

     

    Get Files

    Add list (VarList1)

    Add list (VarList2)

    Add list (VarList3)

    For each CurrentItem in Files

    Parse Text to Var1, Var2, and Var3

    Add Item to List Var1 to VarList1

    Add Item to List Var2 to VarList2

    Add Item to List Var3 to VarList3

    End For Each


    Can you explain this to me a little more? I'm still pretty new to these programming/code language concepts like var lists and adding items to lists. In my current method I take the file apart piece by piece, starting left and going right. Please make corrections where you see fit: I take the For Each containing all of the Files and iterate each piece of the file name, like I am now, into its own unique var. This is where I'm getting confused. Now that I have a unique var, I need to add it to one of four lists. (It will be apparent which list to add to) Am I continuously adding a new var by replacing the contents in the new iteration of the For-Each that goes over file names? If so, is that why you worded it, "Add Item to List Var1 to VarList1" (IE, Add %FirstName% to %FirstNameList%, later Add %LastName% to %LastNameList%?)

    When there are no more files to iterate that must be the logical end of the For-Each, at which point I can send these lists of Vars to VBA?

     

    Do I need to consider what VBA is expecting as incoming vars? (I'm not sure how to word this question well.)

     

    Sub SorterMain(FirstName As String, LastName As String, DateMatch As String, F2F As String, MonthVal As String)

    Where "FirstName as String" is the incoming var, do I need to adapt this to the fact that it is now receiving a list and not a single string?

     

    Thank you sincerely for your assistance, @MichaelAnnis !!!

     

     

     

  • Verified answer
    MichaelAnnis Profile Picture
    5,727 Moderator on at

    It's hard not knowing the totality of everything that is happening.  I remember talking about going through the separate if functions in the previous post.  If the Macro workbook can stay open, we can use something simple to open the workbook and leave it open.

     

    Here is an example of Get Files, running an Excel Macro on each file, but only opening it on the first one and close it on the last:

     

    Get Files to %Files%

    For each CurrentItem in %Files%

    If %CurrentItem% = %Files[0]%

    • Launch Excel

    End (If)

    Attach to Excel

    'Run Macro in Excel

    End (For Each)

    Close Excel

     

    This logic would only 'Launch Excel' on the first iteration of For Each and would leave it open for the rest.

     

    As for adding the variables to a list, it's just an option if you would want to parse them all out first, but if you are good with the way it is going and just want to avoid opening and closing excel repeatedly, the above should fit you just fine.

     

    We usually add variables to lists if we need the variables later.  So, let's say you were to parse out all the files and rename them using the macro, but then later, you wanted to move them to separate file folders based on one of those old variables; it would have been overwritten.  By adding it to a list, you can reference it later, because they would all be in the same order, so %Filename[4]% relates to %FirstName[4]% and %LastName[4]%, etc.  But if you do not need to reference these variables after this step, then you do not need to go through that pain.

     

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Excellent! Thank you tremendously for your time to explain that simply and clearly to me!

     

    I'll give this a whirl. If I have any difficulties I'll come asking more questions.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    My Flow is running so much faster now! Its like greased lightning in a can!!! Thank you, thank you @MichaelAnnis !!

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    Glad to hear it.  I have a few that I have to shutdown Excel like that and reopen because a whole section of a flow might be skipped (which contains the initial launch) if something doesn't exist, so I know what it's like when it's open/shut/open/shut.  Enjoy.

    I'm starting a "Best Practices" series, I hope it's regular, but it's on my linked in, so feel free to check it out from time to time:  https://www.linkedin.com/in/michael-annis-80903/

    Have a good one!

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 248 Super User 2026 Season 2

#2
trice602 Profile Picture

trice602 160 Super User 2026 Season 2

#3
11manish Profile Picture

11manish 142 Super User 2026 Season 2

Last 30 days Overall leaderboard