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.