mhammond / pywin32

Python for Windows (pywin32) Extensions
5.08k stars 799 forks source link

Exception thrown inconsistently while calling VisualReportsEdit() on MS Project application #1548

Closed gavinhailey-sunriselabs closed 4 years ago

gavinhailey-sunriselabs commented 4 years ago

Expected behavior: MS Project generates the visual report and opens it in excel. Actual behavior: Exception thrown sometimes, successfully generates in 1 of every 10 tries or so.

Code:

import win32com.client as win32

class MSProject:
    def __init__(self):
        self.mpp = win32.gencache.EnsureDispatch('MSProject.Application')
        self.Project = None
        self.mpp.Visible = 0

    def load(self, doc):
        try:
            self.mpp.FileOpen(doc)
            self.Project = self.mpp.ActiveProject
        except Exception as e:
            print("Error opening file", e)

    def save(self):
        self.mpp.FileSave()
        self.mpp.Quit()

    def create_resource_usage_report(self):
        self.mpp.OptionsSchedule(DurationUnits=5, WorkUnits=5)
        self.mpp.FileSave()
        templates = self.mpp.VisualReportTemplateList
        template_path = templates.Item(10).TemplatePath
        self.mpp.VisualReportsEdit(template_path)

def main(doc):
    project = MSProject()
    project.__init__()
    project.load(doc)
    project.create_resource_usage_report()

Exception: Traceback (most recent call last): File "C:/Users/ghailey/python/MicrosoftProjectExport/generate_usage_report.py", line 42, in <module> main('C:\\Users\\ghailey\\Downloads\\AIO200529-pln-v00c Retinopathy imager Phase 0 Plan for proposal.mpp') File "C:/Users/ghailey/python/MicrosoftProjectExport/generate_usage_report.py", line 35, in main project.create_resource_usage_report() File "C:/Users/ghailey/python/MicrosoftProjectExport/generate_usage_report.py", line 27, in create_resource_usage_report self.mpp.VisualReportsEdit(template_path) File "C:\Users\ghailey\AppData\Local\Temp\gen_py\3.8\A7107640-94DF-1068-855E-00DD01075445x0x4x9\_MSProject.py", line 3087, in VisualReportsEdit return self._oleobj_.InvokeTypes(2143, LCID, 1, (11, 0), ((12, 16), (3, 48)),strVisualReportTemplateFile pywintypes.com_error: (-2147352567, 'Exception occurred.', (1004, 'Microsoft Project', 'An unexpected error occurred with the method.', 'C:\\Program Files\\Microsoft Office\\Root\\Office16\\VBAPJ.CHM\x00', 131072, 0), None)

Python: v3.8.3 pywin32: v228

mhammond commented 4 years ago

The exception is being thrown by MSProject, so there's not much pywin32 can do other than catch it - there's no evidence of a pywin32 bug here. You might want to see https://github.com/mhammond/pywin32#support for other support options.