0

I have the below Python code:

    import win32com.client as client
outlook = client.Dispatch('Outlook.Application')
namespace = outlook.GetNameSpace('MAPI')

account = namespace.Folders['email account']

inbox = account.Folders['Inbox']


print(inbox.Name)
print(inbox.Parent.Name)
print(inbox.Items.Count)

HMEmails = [message for message in inbox.Items if message.SenderEmailAddress.startswith('ics.notifier')]

for message in HMEmails:
    print(message)


folder = inbox.Folders('ICS Reports')

for message in HMEmails:
    message.Move(folder)

When run via the command prompt, this code returns the number of email items in the specified folder, the names of them, and also moves them to a separate folder if they meet the criteria.

I understand that I must use Visual Basic for Applications to use the code within the Outlook application, but what would the VBA code for that look like? And is there a way to then assign the above script function to a button within Outlook so that the function is applied after clicking?

EDIT:
It seems the VBA code to run the Python script is rather simple. Below is the code that I thought should work:

'Initialise function
Sub RunPythonScript()
    
    'Provision of file path locations to Python Exe and script
    PythonExe = "C:\Users\user\AppData\Local\Programs\Python\Python38\python.exe"
    PythonScript = """C:\Users\user\OneDrive - laptop\Documents\A&I\Python\offAutomation.py"""
    
    'Invoke the Python script
    Shell (PythonExe & PythonScript)
    
End Sub

When I click "Run" I get an error

"file not found" error.

Both file locations are valid since I am able to access both by pasting them into my search bar.

Community
  • 1
  • 1
Jamie.G099
  • 11
  • 5
  • Plenty of examples to be had here on SO. Eg: https://stackoverflow.com/questions/18135551/how-to-call-python-script-on-excel-vba Also plenty of pointers if you Google "vba run python script" so maybe start there and post back if you run into a specific problem calling your script. – Tim Williams Nov 02 '20 at 17:10
  • Cheers Tim, I'll give them a look through and report back – Jamie.G099 Nov 02 '20 at 17:16
  • I've entered in an edit, I don't suppose you know why it says "file not found" when they're definitely valid locations do you? TIA – Jamie.G099 Nov 03 '20 at 15:56
  • Maybe try adding a space between the two paths, or for the second one try a non-OneDrive path. – Tim Williams Nov 03 '20 at 16:34
  • Thank you Tim, works fine now. To solve, I used the following: Shell (PythonExe & " " & PythonScript) – Jamie.G099 Nov 03 '20 at 17:56

1 Answers1

0

Many thanks to Tim Williams for the help on this. In order to run an external Python script from Outlook using VBA see the following example:

'Initialise function
Sub RunPythonScript()

'Provision of file path locations to Python Exe and script
PythonExe = "C:\Users\user\AppData\Local\Programs\Python\Python38\python.exe"
PythonScript = """C:\Users\user\OneDrive - laptop\Documents\A&I\Python\offAutomation.py"""

'Invoke the Python script
Shell (PythonExe & " " & PythonScript)

End Sub

Please note, the triple quotation marks for the PythonScript variable are necessary since there are spaces present within the file path.

  1. The Sub function has been defined at the start
  2. In my case, two variables have been assigned file paths for Python.exe and my Python script
  3. The Shell function at the end runs the variables file paths, but they needed to be split by a space next to the ampersand symbol - that is why it didn't run originally.
  4. End Sub marks the end of the VBA code.
Jamie.G099
  • 11
  • 5