Search code examples
pythonvisiowin32com

Python win32com.client pauses from Visio error and does not continue to execute rest of code


I'm opening and archiving Visio files.

visio = comclt.Dispatch("Visio.Application")
wsh= comclt.Dispatch("WScript.Shell")
wsh.AppActivate("Microsoft Visio")

for i in os.listdir(path): #loops through the path
    if i[-3:]=='vsd': #checks to see if it is a visio file
        doc = visio.Documents.Open(path+'\\'+i)

But when I open certain Visio files, because the visio file was created on a different machine where a local stencil was present, there is an error of .vss is part of workspace but cannot be opened. This is not a problem, I can just hit ok. So I've put in code to send key ENTER.

Here is my problem. I have the code below. But it does't work because (I think) the code pauses on doc = visio.Documents.Open(path+'\\'+i) and does not continue until ok is pressed. Once I press ok manually, the code sleeps for 2 seconds before continuing.

time.sleep(2)
wsh.AppActivate("Microsoft Visio")
wsh.SendKeys("{ENTER}") 

how do I tell python to not wait for doc = visio.Documents.Open(path+'\\'+i)? or is another way to solve this?


Solution

  • You can try to use .AlertResponse to prevent message boxes in Visio: http://msdn.microsoft.com/en-us/library/office/ff767782.aspx

    i.e. before opening the diagram, set

    Visio.AlertResponse = 1

    This should prevent the message from popping up.