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 ?
Hi VB,
Use Visio.Documents.Open or OpenEx.
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 ... ?!?
- If the file already exists, I can opened it w/o exceptions though.
I would really appreciate if you give me a hint about the following issues (admitted a little off-topic)
- Where do I find the equivavlent python definitions for constants like visOpenRW etc.? I always had to hardcode them with their real values, e.g. visOpenRW == &H20.?
- Do you have any idea about the drop problem?
- Why is dealing with visio objects so nonintuitive :) ?
P.s. Off-topic question: Python and MS Visio, are these worlds made out for each other ?!? ( ... so many little, nasty problems)
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
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.