Skip to content
Advertisement

pywintypes.com_error: (-2147221008, ‘CoInitialize has not been called.’, None, None)

When I try to run this code as is I get this error “IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) pywintypes.com_error: (-2147221008, ‘CoInitialize has not been called.’, None, None)” , however if I run stp_tracker alone it works fine and if I run notify stp’s alone it works just fine. I appreciate anyones input. Thanks

import time
import win32com.client
# import sys
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
# import watchdog


class MyHandler(PatternMatchingEventHandler):
    patterns = ["*.stp", "*.step", "*.txt"]

    def process(self, event):
        """
        event.event_type
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        # the file will be processed there
        print(event.src_path, event.event_type)

    def on_modified(self, event):
        self.process(event)
        notify_stps()

    def on_created(self, event):
        self.process(event)
        notify_stps()

    def on_deleted(self, event):
        self.process(event)
        notify_stps()


def stp_tracker():
    # /if __name__ == '__main__':
    path = r"W:TestFolder"
    observer = Observer()
    observer.schedule(MyHandler(), path)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()


def notify_stps():
    const = win32com.client.constants
    olMailItem = 0x0
    obj = win32com.client.Dispatch("Outlook.Application")
    newMail = obj.CreateItem(olMailItem)
    newMail.Subject = "I AM SUBJECT!!"
    newMail.Body = "Step files in directory"
    # newMail.BodyFormat = 2 # olFormatHTML https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
    # newMail.HTMLBody = "<HTML><BODY>Enter the <span style='color:red'>message</span> text here.</BODY></HTML>"
    newMail.To = 'Acoker251@outlook.com'
    # attachment1 = r"C:Tempexample.pdf"
    # newMail.Attachments.Add(Source=attachment1)

    newMail.Send()


stp_tracker()

Advertisement

Answer

Apologize for that, but searching the internet and I found something that helped. I came across the same post earlier and assumed it was deprecated info because my AutoComplete in pycharm was not picking anything up when typing pythoncom.CoInitialize() so it made me think it was outdated info. Also the same information Strive Sun explained

User contributions licensed under: CC BY-SA
11 People found this is helpful
Advertisement