2
votes

I'm running Visio using automation and I'm having trouble getting the process ID of the Visio process to check when its complete. Here's my VB script:

Set visio = CreateObject("Visio.InvisibleApp")
Wscript.Echo visio.ProcessID    
Set document = visio.Documents.OpenEx("somefile.vsd", &H88)
document.ExportAsFixedFormat 1, "somefile.pdf", 1, 0
visio.Quit

and running it with cscript // nologo.


The problem is that visio.ProcessID returns a number that isn't the actual Windows process ID (e.g. 6613 when the actual process ID is 8146). The cscript host seems to finish before the Visio process exits causing problems cleaning up temp files.

Here is the Visio reference notes for:

So the question is: how can I get the Visio process ID or detect when it has properly exited?

Thanks!

2
I had a similar problem and solve it in very stupid wat .I've called pslist -t (part of sysinternals instruments) and serched for name process under wshost.exe with counting spaces to see if it's a child process.If you can find another way - it's a way too :) - npocmaka

2 Answers

1
votes

Just noticed this in the help for Visio.Application.ProcessID:

"The value returned by ProcessID is not the same as the Windows Process ID of the current Visio instance."

So I guess these are just so you can distinguish between multiple instances of Visio.

There are also Visio.Application.WindowHandle32 and Visio.Application.WindowHandle, which might be helpful, although maybe not so much for an invisible app instance.

0
votes

Can't see how this id would help you. I suspect you get an error in visio. Have you tried with

on error resume next

and after each line that could give an error

if err.number <> 0 then
  wscript.echo err.description
  err.clear
end if

There seems to be an issue if you omit parameters, so use them all. See http://msdn.microsoft.com/en-us/library/office/ms409271(v=office.12).aspx for the values. Before quiting use document.saved = true, you could check first if the result file exists.

EDIT: check if a process is running, could be you have to adapt the name of the service (check your téaskmanager)

set service = GetObject ("winmgmts:")

for each Process in Service.InstancesOf ("Win32_Process")
  If lcase(Process.Name) = "visio.exe" then
    wscript.echo "visio still running"
    wscript.quit
  End If
next
wscript.echo "visio no longer running"

EDIT2: to get the processid of the active visio app (If more than one Visio instance is running, GetObject returns the active instance. When a program is run as an add-on or by double-clicking a shape, the active instance is the one that the program was run from. Otherwise, it is the instance that was most recently run or brought to the front. If no Visio instance is running, GetObject causes an error) cfr http://webmail.vh.com.tw/Microsoft/Developing%20Microsoft%20Visio%20Solutions/27.htm

set appObj = GetObject(, "visio.application")
if appObj Is Nothing Then
  wscript.echo "There is no active Visio."
else
  wscript.echo "ProcessID: " & appObj.ProcessID
end if