Visio Guy

Visio Discussions => Programming & Code => Topic started by: VisioBut on July 24, 2009, 04:54:05 PM

Title: Open a new file; Drop a shape; close the file
Post by: VisioBut on July 24, 2009, 04:54:05 PM
Hello there,

Im trying to open a new visio file from scratch like:
doc = visio.Documents.Add("Test.vsd")
but I only get File not found exception.

If I make doc = visio.Documents.Add("") Im able to proceed but by closing the file
doc.Close() I get an exception again.

Before closing the file, I try to drop some masters from an opened stencil. I could see only the first one being dropped(shown) in the drawing. All other are not displayed.
for master in mMst:
            page.drop(master,k*100,k*50)
            k += 1       


Any idea why all this happens ?
Title: Re: Open a new file; Drop a shape; close the file
Post by: Visio Guy on July 24, 2009, 05:46:28 PM
Hi VB,

Use Visio.Documents.Open or OpenEx.
Title: Re: Open a new file; Drop a shape; close the file
Post by: VisioBut on July 24, 2009, 10:38:51 PM
Quote from: Visio Guy on July 24, 2009, 05:46:28 PMUse Visio.Documents.Open or OpenEx.
Thank you. But I got File not found exception again if the file doesn't exist. So I still dont know how to create a new, non-existing file ... ?!?


I would really appreciate if you give me a hint about the following issues (admitted a little off-topic)



Title: Re: Open a new file; Drop a shape; close the file
Post by: VisioBut on July 27, 2009, 12:01:48 PM
P.s. Off-topic question: Python and MS Visio, are these worlds made out for each other ?!? ( ... so  many little, nasty problems)
Title: Re: Open a new file; Drop a shape; close the file
Post by: Nikolay on July 27, 2009, 02:47:44 PM
Below is kinda example code (that works on my machine (http://www.codinghorror.com/blog/archives/000818.html) :D):


from win32com.client import Dispatch
app = Dispatch("Visio.Application")

doc = app.Documents.Add("") # you can pass optional template as argument, use empty string if you don't need it
page = app.ActivePage

stn = app.Documents.Open("basic_m.vss") # this is the name of pre-installed visio stencil

k = 0
for master in stn.Masters:
page.Drop(master, k, k)  # The coordinates are in inches (!!!)
k += 1

doc.SaveAs("d:\\test.vsd") # Full path here (!!!)
app.Quit()


QuoteWhere do I find the equivalent python definitions for constants like visOpenRW etc.? I always had to hardcode them with their real values

You can do the following:

- First, run "makepy -d "Visio.Application" - this shall grab all Visio constants into Python. This is a tool in Python's /win32com/client folder.
- Second, use the constants like that:

from win32com.client import Dispatch, constants
app = Dispatch("Visio.Application")

print constants.visOpenRW  # this shall print "32"


QuoteDo you have any idea about the drop problem?

See above, the shapes are probably dropped out of the screen, since distance for Drop is in inches, not pixels.

QuoteWhy is dealing with visio objects so nonintuitive?

Because the "essential" way goes through Visio VBA, not the Python  ;)

Kind regards, Nikolay
Title: Re: Open a new file; Drop a shape; close the file
Post by: Visio Guy on July 27, 2009, 05:03:54 PM
Nonintuitive -

Visio was created in 1992, so perhaps paradigms have shifted. Also, the automation model is not the most important feature of an end-user office application, so I think sometimes automation calls get implemented in the "easiest way" for the dev team. These might perhaps be awkward for "end" developers. But hey, the Visio engineers are people too!

The trick to understanding automation in Visio, and manipulating shapes is to go through the ShapeSheet. In automation, that means shp.Cells("cellname"). Here you can get access to just about every part of a shape.