So i tried this example to open and return tasks, but get an error that it cannot open the file, the file and script are in the same directory for this test.
import win32com.client
def list_task_attributes(file_path):
ms_project_app = None
try:
# Create an instance of the Microsoft Project application
ms_project_app = win32com.client.Dispatch("MSProject.Application")
# Open the Microsoft Project file in read-only mode
ms_project_app.FileOpen(file_path, ReadOnly=True)
# Access all tasks and list their attributes
for task in ms_project_app.ActiveProject.Tasks:
print(f"\nTask ID: {task.ID}")
for attribute in dir(task):
# Exclude attributes that start with '__' (internal attributes)
if not attribute.startswith('__'):
value = getattr(task, attribute)
print(f"{attribute}: {value}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
try:
# Close the Microsoft Project file only if it's open
if ms_project_app and ms_project_app.ActiveProject:
ms_project_app.FileClose()
except Exception as close_error:
print(f"An error occurred while closing the project: {close_error}")
if __name__ == "__main__":
# Replace 'your_project_file.mpp' with the actual path to your Microsoft Project file
project_file_path = 'GreenwoodIN.mpp'
try:
list_task_attributes(project_file_path)
except Exception as e:
print(f"An error occurred: {e}")
Get this error:
Error:
An error occurred: (-2147352567, 'Exception occurred.', (1004, '', 'Project cannot open the file.', 'C:\\Program Files\\Microsoft Office\\root\\Office16\\VBAPJ.CHM\x00', 131072, 0), None)
I can open the file manually from the folder its located in and no issues, but not within python.. I will say that executing the above, will create an instance of MS Project in my task manager.