Excel Macro to Open a Password Protected File, Help Needed

I created a macro to open an Excel password protected file but have a number of questions. The macro is the first of the two macros below.

QUESTION 1: Is there a better (i.e. more secure) way of entering the password (i.e. I have entered it as text). There are two scenarios here a) for password protected files that do NOT us a password manager and b) for webpages whose passwords are stored in 1Password.

QUESTION 2: In testing the application I noticed that I needed a delay after opening the file. I would like to change the pause from 10 seconds to until the file is opened. Is there a way to do this noting I did try Pause Until Condition Met but that did not work (i.e. the macro failed o execute which I suspect because it is at the front immediately before it has finished loading).

QUESTION 3: In the last step of the if statement I delete the Excel sheet called SHEET. XLSX. Is there a more elegant way to do this such as s to delete all sheets that are called / titled the sheet that I opened?

PROBLEM 1: With respect to the second bundles macro which includes AppleScript. This macro did not work when I decided to use brute force and write an AppleScript. The AppleScript works perfectly as an AppleScript but does a) NOT work within KM and b) results in the following error message. Would like to know / understand why.

Worth noting, I do believe that I have the proper accessibility per:

Screen Shot 2021-12-12 at 5.36.21 PM

When I run the macro I get the following error code:

20211212_km_applescript fail

Would very much appreciate your help with the above.

Thank you.

JBC_Start Applications Macros.kmmacros (64 KB)

Keyboard Maestro Export

  1. What does this screen in your Security/Privacy prefs look like on your system?

  2. The next time you run the macro and get that error message, try opening the KM Engine log (Help > Open Logs Folder), find the full error message text, and tell us what that says.

Again, thank you, you are on a role!

I looked in the Automation section and Excel was the only application that was not enabled. I enabled in it and all was good!

It is like all things, knowledge = power = solutions. A heartfelt thanks you.

PS. You cannot mark this one solved as answers to QUESTIONS 1 to 3 remain outstanding BUT STILL, YOU NAILED THIS ONE! THX!

1 Like

Hey Joel,

You can get passwords from the keychain using Keyboard Maestro or the security shell command.

This works in Microsoft Office 2016 on Mojave:

# Open a Password Protected Workbook or Worksheet.
set passwd to "1234" -- whatever you want
set theWbpath to "/Users/xxxxx/Desktop/number1.xlsx" -- example path
set theWb to "number1.xlsx" -- example file
set theSheet to "sheet1" -- example name of sheet

tell application "Microsoft Excel"
   open theWbpath
   unprotect workbook theWb password passwd
   # or
   unprotect sheet theSheet password passwd
end tell

This works in Microsoft Office 2016 on Mojave.

# Delete a Worksheet.
tell application "Microsoft Excel"
   set display alerts to false
   set sheetList to sheets whose name starts with "N"
   if length of sheetList = 1 then
      delete item 1 of sheetList
   end if
   set display alerts to true
end tell

You cannot script 1Password, and they've stated they will never make it scriptable. Please do complain.

The way to use pause-until for an application is:

  1. Pause until app is frontmost.
    • KM can still operate before the app is ready.
  2. Pause until a menu path is enabled.
    • Using a path is faster than a menu item name, because KM doesn't have to search – it has a reference.

This two step pause-until is pretty bombproof.

-Chris

Appreciate this noting that to date I have stayed away from Keychain preferring 1Password because I can use it on work laptop which is not a mac :frowning:

Yes, I already did so. While I consider it a missed opportunity I am guessing that they consider it a security issue.

Interesting.

I tried Pause Until App is Frontmost which did NOT work because, in this case, the Excel sheet was not loaded so could not receive the password.

Is my understanding correct that by ADDING Pause Until a Menu Path is Enabled that this will solve this problem?

Thanks,

Unfortunately it only works conditionally @ccstone. I had already sent @Joel a suggestion with 1Password yesterday.


In my example I have the Excel document on the desktop.
By entering the shortcut, this is called and after the login dialog appears in the foreground, that 1Password autofill login is performed.

In my gif, the Excel document is opened once with 1Password Mini unlocked and then the KM macro pauses until you enter your master password.

2021_12_12_Support_1

Exel <5C8B 211213T185905>.kmmacros (88,3 KB)


Yes.

One pause-until action with two conditions.

Got it thanks! It may be a day or two as I am have very full work days and can play mostly on the weekend.

The list of tweaks is also getting very loooooong!