pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)
Asked Answered
M

3

7

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 = '[email protected]'
    # attachment1 = r"C:\Temp\example.pdf"
    # newMail.Attachments.Add(Source=attachment1)

    newMail.Send()


stp_tracker()
Marque answered 7/4, 2020 at 0:33 Comment(2)
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 think it was outdated info.Marque
Add pythoncom.CoInitialize(), refer: CoInitialize has not been calledOverhand
M
4

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

Marque answered 10/4, 2020 at 16:3 Comment(0)
I
16

First:

import pythoncom

Then set:

xl=win32com.client.Dispatch("Excel.Application",pythoncom.CoInitialize())
Involutional answered 30/9, 2021 at 18:2 Comment(2)
you solution made my day :X TNX :xThrasher
what is this doing and why does it work?Toname
M
4

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

Marque answered 10/4, 2020 at 16:3 Comment(0)
C
0

just try this i think it will solve your problem just fine

import pythoncom

pythoncom.CoInitialize()
Ceballos answered 5/7, 2023 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.