Repeat a Macro for a Defined Number of Times

Hi,

I have been using KM for a while, having come to it from QuicKeys.

I have a macro that copies cells from an Excel table into a Form in Word and then saves each individual form with a unique file name. It works perfectly as I used KM to perform the same steps as QK. There are 34 consecutive actions in that macro.

What I have been unable to work out is how to get that macro to repeat for a number of times, that number needs to be input by me.
Any help would be much appreciated!

Thanks and a Happy and Safe New year to all.

Peter

Hey Peter,

Welcome to the forum!   :smile:

This one is pretty simple.

Use a Prompt for User Input action.

And a Repeat action.

The appended macro should give you the basic idea.

-Chris


Repeat with User Designated Number of Iterations v1.01.kmmacros (7.6 KB)


EDIT ⇢ macro updated to v1.01 2021/01/21 18:47 CST:
⠀⠀⠀⠀⠀⠀ – Added some output text to the Display Text in Window action.


1 Like

Hi Chris,

Than you for that, I only do this once a year to generate invoices for my wife's small business!

On looking at the Macro, I realise that I had already got most of the way there, but I'm still confused, as the macro refuses to repeat.

Do I place all the actions that I wish to repeat inside the Prompt for User Action or do I place them in the Repeat loop? Should the Repeat Loop be immediately below the Prompt for User Action or should it appear at the end of the list of actions that I wish to repeat if they are not placed inside the Prompt For user Action?

Sorry to ask such basic questions, I am retired now and came to KM after I retired and use it in a very basic way, so I struggle to retain the understanding of how this very useful application works!!!

Regards Peter

Hey Peter,

Did you run the macro?

If not then run it, and see how it works.

(I've changed the original macro to v1.01 to demonstrate using variables and text tokens in the Display Text action).

The macro is designed to demonstrate what you want to do.

  • The prompt lets you enter the number of repeats with a default of 3 (you can change this).
  • The Repeat action repeats what's inside it.

-Chris

Thanks Chris, I did run the Macro but like an idiot, did not grasp the significance.
Pretty obvious really, sorry

1 Like

The way I would design this is to keep your existing Macro as is, but call it using the KM Execute a Macro action, which you place in the Repeat Actions block (delete/disable the existing Display Text action).

Instead of the Display Text action I would use a Notification action that would display once for each call to your Macro. Of course, it might be better to put this Notification Action at the top of your existing Macro so you could display some information about the specific call.

Thank you for your help, I have gone with your idea of calling the macro, Chris's solution worked as well, it helped me a bit as your solution separated the Repeat from the actual macro and helped to trouble shoot it. Interestingly running the macro in the debug mode never failed until I swapped from stepping through it to running it.

Here are a couple of marginally interesting things

  1. I first used this macro in Quickeys way back in 2009 on a 2008 iMac, using Word & Excel of that time.
    Basically the macro copies and pastes entries from 2 adjacent cells in an Excel spreadsheet into a word document which is then saved as a file using the data from one of the spreadsheet cells as the file name. At the time Mailmerge in Word could only produce a single file from a mailmerge operation which was no use as I need to send each document to a different email address
    In order to make the macro work I had to insert pauses of around 1.5 seconds into the macro in order that the iMac and screens could keep up.
  2. Startly stopped supporting QK some 10 years ago now and as time has gone by the workarounds to keep it running on the latest version of the OS and faster Macs, became ever more tricky. With the advent of Catalina, 32bit Applications were no longer supported. I had started the move to KM before this happened.
  3. I re constructed this macro in KM and it worked fine on Yosemite OS with Word/Excel 2011and I was able to speed up the macro by reducing the length of the pauses between actions in the macro.
  4. Fast forward to now, I have a 2019 iMac with 16gb of RAM an SSD, OS Catalina, Office 365 and the latest version of KM. So that Mac is several orders of magnitude faster than my 2008 model (2Mb RAM, conventional HDD). The first time I ran this macro it simply failed to work with the files not being saved correctly and records being skipped. I recalled having this problem years ago and increasing the pause time between actions fixed it.

To my surprise to get it to run reliably over several hundred records the pause time between actions had to be increased to about twice what they were 12 years ago!!! I have turned off Onedrive in 365 and only save to my internal SSD, the only apps running are KM, Word & Excel with the internet off (I tried it with it on, slower still), So after several thousand pounds of expenditure in both Hardware & Software, it appears that everything runs slower! Any Thoughts?
Regards Peter

Hey Peter,

You'll have to post the macro, so we can actually test.

Everything else is just conjecture and pretty much a waste of everyone's time.

How to Post Your Macro to the Forum

Please also zip appropriate example Word and Excel files.

When you make it easy for others to test, you're much more likely to get quality help.

-Chris

Rather than using a fixed about of Pause, it is better to use Pause Until action based on some condition suitable to your app. I have no idea what that might be without seeing your actual Macro.

Hi Chris, I would love to do that but unfortunately the spreadsheet contains information that is covered by the GDPR, and also sensitive business information.
At some point I will create a dummy spreadsheet to post along with the macro, meantime it all works fine, so panic over for the time being :slight_smile:
Thanks for everyone's help,

1 Like

Hi, Thankyou for that suggestion, it's become clear that KM is way more sophisticated than QK and I will look into the alternative that you have suggested
Regards peter