Development

Tips & code for programming Visio & using the ShapeSheet

Maps & Geography

Maps & geography shapes & templates galore!

Network

Shapes & tools for Visio network diagrams.

News

Find out what’s new in the Visio world!

Shapes

Download useful & attractive Visio shapes. For free!

where can i buy windows 7 cheapest buy windows 7 upgrade from xp windows vista purchase price buy inventor 2010 pctools spyware doctor 5.5 buy windows 7 best price norton ghost 12 product key windows 7 price future shop windows xp price singapore windows 2008 web server download omnipage coupon buy windows 7 starter download adobe indesign cs3 for mac buy vista to windows 7 microsoft project 2003 download buy windows xp operating system windows 7 home premium cheap mathcad pricing photoshop buy mac discount autocad 2009 purchase microsoft money 2007 deluxe download photoshop mac autodesk maya 2009 price windows vista business download windows 7 price 64 bit purchase windows 7 ultimate full version buy windows 7 home premium key lightroom for apple purchase windows xp india adobe cs4 photoshop buy where to buy encarta 2009 purchase vista oem buy windows 7 tesco cheap adobe fireworks windows 7 price online best price corel draw 11 mac discount powerpoint buy microsoft office for mac canada buy microsoft project 2002 adobe production premium mac turbotax 2009 deluxe best price windows 7 ultimate 64 bit best price buy windows 7 amazon adobe flash pricing adobe dreamweaver sale purchase windows 7 ultimate buy quickbooks pro 2007 roxio copy & convert 3 buy download photoshop elements 6 steinberg nuendo price uniblue registrybooster 2009 download cheap photoshop programs adobe photoshop elements cheapest buy windows 7 future shop adobe presenter 7 download microsoft office 2003 sale buy quarkxpress 8 for mac purchase adobe cs3 design premium purchase after effects cs3 paperport professional 12 prices cakewalk download parallels desktop price buy microsoft windows 7 home premium cheap adobe after effects for mac how to purchase windows 7 online download microsoft streets and trips 2008 microsoft office 2003 cheapest buy windows 7 online singapore purchase autodesk 3ds max 2009 buy windows xp pro best price adobe flash cs3 buy uniblue registrybooster 2009 cheap windows 7 ultimate retail buy autodesk autocad buy microsoft office document imaging purchase microsoft office 2003 buy windows xp media centre edition discount microsoft windows 7 ultimate windows 7 price upgrade cheap adobe photoshop software burnaware professional price buy acronis true image 2010 windows 7 price change buy acrobat 7 where to buy adobe flash cs3 quicken 2010 price buy windows xp corporate buy office 2007 student product key windows 7 price hong kong buy windows 7 64 buy acrobat professional 9 symantec winfax pro 10.4 buy cheap windows 7 keys adobe photoshop cs4 buy buy microsoft visio 2007 turbotax deluxe 2009 download windows xp price in rupees buy lightroom software buy windows 7 buy windows 7 serial key steinberg nuendo 3 download download autodesk 3ds max 9 buy ms project online windows 7 price egypt windows 2008 enterprise download parallels mac download adobe lightroom sale cheap cubase 5 where to buy windows xp service pack 2 purchase windows vista download cheap windows 7 in singapore lowest price microsoft office purchase windows 7 license key where can i buy windows 7 starter edition buy i.r.i.s. readiris pro 11 cheap windows xp operating system buy office 2003 key where to buy windows xp cheap autocad mechanical 2009 download buy matlab software buy 3ds max 2010 student download dragon naturally speaking autocad inventor download get autocad 2010 buy adobe illustrator cs2 buy windows 7 mac windows xp price canada steinberg cubase sx3 download cheap windows 2003 enterprise download adobe illustrator cs3 can i buy windows xp online windows xp price in malaysia buy adobe cs4 suite best price adobe illustrator cs3 download font folio 11 acdsee pro license key cs3 production premium mac ms streets and trips 2008 download download adobe illustrator mac windows 7 price in india where can i buy cubase sx3 windows vista download iso roxio creator 2010 coupon code microsoft office price student windows 7 home premium 64 bit oem buy microsoft frontpage software buy windows xp 64 pro cheap windows 7 home premium full version buy burnaware professional cheap microsoft works 9 microsoft visio download adobe cs4 web premium oem download disk director cheap adobe creative suite cs4 microsoft money home and business download buy parallels desktop 5 for mac cheapest norton 360 3.0 lightroom price buy windows vista pro microsoft money buy online buy microsoft expression blend 3 buy windows 7 full version for students purchase lightroom 2 buy microsoft office ultimate pdf converter professional 6 promotional code buy adobe cs3 design premium buy indesign cs3 software buy windows vista ultimate upgrade acdsee photo editor download microsoft office 2003 professional cheap windows 7 for college students illustrator cs3 download download adobe indesign cs4 cyberlink powerdirector prices buy windows vista ultimate license buy access 2003 online windows 7 price guide mathcad price buy after effects cs4 mac cheap fl studio 8 xxl cheap photoshop elements 8 buy windows vista home premium oem buy wordperfect x3 buy windows 7 home premium 64 bit buy windows 7 brisbane buy windows 7 bangkok autodesk mudbox price purchase vista license cheap adobe premiere elements 8 download adobe creative suite 4 design premium best price microsoft excel 2003 buy microsoft project 2000 microsoft expression price discount photoshop elements 8 cheap maya software 3ds max sale buy windows 7 ebay windows 7 price 2010 deskshare videoeditmagic 4.3 price buy windows xp 2010 norton 360 price comparison buy windows xp used adobe contribute mac purchase windows 7 for mac buy windows vista home premium product key buy windows 2003 datacenter buy windows 7 professional cheap microsoft windows 7 ultimate buy sony vegas pro 8 purchase access 2007 final cut express 4 cost download microsoft visio 2007 buy onenote 2003 photoshop lightroom for mac symantec pcanywhere download windows 7 price pakistan buy premiere pro cs3 cheap windows 7 for students in australia web page maker 3 download buy windows xp home sp3 adobe photoshop best buy cheap windows 2008 enterprise buy windows 7 direct download download microsoft mappoint 2009 buy windows xp home download microsoft autoroute 2007 europe ashampoo burning studio download buy windows xp calgary archicad 13 download adobe cs4 design premium trial price photoshop elements 8 windows 7 home premium cheapest autodesk lustre pricing purchase archicad 13 acdsee pro 2 download windows 7 discount canada vmware workstation 7 download discount autodesk 3ds max 2009 buy autodesk inventor professional buy windows xp home full version buy windows 7 language pack buy microsoft office 07 buy microsoft excel 2007 download propellerhead reason 4 mappoint 2009 european buy access 2007 only buy frontpage 2003 purchase windows 7 ultimate online purchase microsoft windows vista buy dragon naturally speaking 10 oil price vista gadget pinnacle studio 14 best price cheap corel video studio pro x2 buy photoshop 7 for mac buy adobe cs3 design premium mac download ashampoo burning studio 9 cheap microsoft project 2007 dvd ripper platinum 5 buy adobe premiere pro cs4 download best price microsoft windows vista windows 7 discount for high school students purchase windows 7 professional (64 bit) compare price photoshop buy windows 7 philippines buy microsoft office powerpoint 2007 purchase windows xp sp3 buy photoshop elements 8 mcafee total protection 2009 download best buy autocad 2009 buy windows vista singapore purchase windows 7 now buy windows xp mumbai buy windows 7 ultimate 64 bit oem windows 7 price kenya ms works download adobe acrobat 9 oem buy windows 7 downloadable adobe cs4 web premium trial cheap windows 7 professional best price windows 7 home premium oem download adobe photoshop elements 6 navisworks manage price windows 7 price comparison buy guitar pro 5.2 download acronis true image 11 buy nero 8 ultra edition acdsee price download adobe robohelp 8 buy powerdirector 7 ultra imsi turbofloorplan home and landscape pro 12 buy buy microsoft expression studio 2 windows 7 discount for military buy windows 7 30 download pinnacle studio 12 ultimate adobe acrobat price comparison mappoint 2009 europe buy cheap avanquest mylogomaker professional 2 buy windows xp cheap microsoft office discount code buy windows 7 switzerland buy windows xp volume license buy norton 360 download cheap adobe photoshop cs3 download microsoft expression studio 3 buy office 2010 professional fl studio 8 xxl download buy microsoft office 2007 home and student cheap adobe cs4 design premium abbyy finereader 9.0 express edition where to buy streets and trips 2010 office 2007 price student buy roxio creator 2010 cheap adobe cs4 master collection mac buy windows xp ebay cheap indesign software buy corel photoimpact x3 download avanquest mylogomaker professional 2 cheap ashampoo burning studio 8 adobe captivate pricing autocad architecture oem buy microsoft office pro 2007 where can i buy powerpoint 2003 turbo tax cost buy windows 7 india online purchase windows 7 license online buy adobe flash cs4 buy windows vista ultimate product key buy cs4 mac buy adobe indesign cs4 best buy microsoft windows 7 ultimate cheap adobe dreamweaver cs4 mac cheap windows 7 canada roxio creator 2009 special edition microsoft office 2010 price in india windows 7 price canadian buy windows xp embedded download corel draw 12 adobe creative suite 4 production premium download buy adobe photoshop cs3 extended buy windows vista business 64 bit buy ms office 2003 pro best buy quicken 2010 buy microsoft office for mac cheap buy adobe after effects cs4 cheap norton 360 3.0 best price adobe cs4 master collection windows 7 price for teachers buy windows xp student purchase adobe acrobat 8 professional buy sql server guitar pro 5 mac download
Home » Code, Development

Edit Visio Masters Programmatically…the Right Way!

Submitted by Visio Guy on February 25, 2008 – 2:19 pm | | 8365 views 26 Comments

Read Full Article

If you are busy developing a Visio solution, you might run into the situation where you need to edit a Visio master using code.

Like any application that has survived the ages, Visio has its share black-magic techniques that lurk in dusky corners of the netherworld. And if you need to edit masters programmatically, be warned! You’re heading down a dark alley…

If you’ve already run into problems with this, don’t feel bad. Even experienced Visio developers get tripped-up by this one! And that’s because the correct way to do this isn’t obvious, and the obvious way to do it isn’t correct!

The Obvious Way Doesn’t Work…Kind-of

When you peruse the Visio object model, you’re likely to think that the obvious way to edit a master shape is something like this:

  Dim mstCopy As Visio.Master  
  Dim shp As Visio.Shape
  ' Get a master named "Bob" from our Document stencil: 
  Set mst = Visio.ActiveDocument.Masters.Item("Bob")
  ' Get the shape inside of the master:
   Set shp = mst.Shapes.Item(1) 

But what happens when you run this code? Your masters change, but those changes don’t seem to percolate down to shapes on the page!

If you are programmatically altering the masters in the Document Stencil of a Visio drawing, then the instances of those masters (ie: the shapes dropped on the page) won’t reflect your changes!

However, if you save your drawing, close it, then re-open, you’ll see that your changes did indeed take effect. Yes something is happening, but not in an ideal way.

Create a Master Editing Session…duh!

Now, we don’t want to edit masters, then programmatically close and open the drawing. That just wouldn’t do! So…

The proper way to get the job done is to start an editing session. The situation is very similar to what you do when you edit a master by hand:

  1. Open a master window (double-click a master, or right-click and choose Edit Master > Edit Master Shape)
    This creates a temporary copy of the master
  2. Make your changes manually: format, resize, edit text, etc.
  3. Close the master window
  4. Accept your changes by responding Yes to the prompt: Update master and all of its instances?

Step 4 is the key for manual editing. You consciously must push the changes to the instances.

This is missing from our code above. It never tells Visio to push the changes to the instances in the drawing. For that matter, that simple code never really creates a copy to fiddle with in the first place!

And that’s because the right way is all very, very non-obvious!

So we mimic the manual sitaution by using the Master.Open method to create a copy of our original master, then we push the changes to the drawing by calling Master.Close. The sample code below says it all:

  Sub EditMaster( ByRef mst As Vis.Master )
    Dim mstCopy As Visio.Master ' an object to hold a copy of mst...  
    Dim shp As Visio.Shape 
      Set mstCopy = mst.Open 
      Set shp = mstCopy.Shapes(1)
      ' Do stuff to shp here... 
      shp.Text = "Example stuff"
      Set shp = Nothing
      mstCopy.Close()
    Set mstCopy = Nothing
    Set mst = Nothing
  End Sub 

The code above works for VB6 and VBA. Dot-netters can just remove all of the Set statements, and probably don’t really need the = Nothing stuff either, since .NET is supposed to handle clean-up all by itself.

So make use of those Master.Copy and Master.Close methods to create a master editing session. Your on-the-fly programmatic changes to Visio masters will then work just fine, and you’ll be a hero at that next status meeting!

Related Posts

  1. Create Visio Flowcharts Programmatically
  2. Detect Sub-selected Shapes Programmatically
  3. Run VBA Code When Documents Open
  4. Microsoft Exchange Server 2007 Stencil
  5. Customized Visio HTML Export

26 Comments »

  • Chris says:

    Hi,

    Nice site…
    I m using com object to create org. chart and want to change color of different shape/node. How can i achieve it?

    Thanks in advance

  • Visio Guy says:

    Hi Chris,

    You can program Visio to change the color of a Visio shape by setting cell-formulas in the FillForegnd section of the ShapeSheet. Go to Window > Show ShapeSheet to see what I’m talking about.

    Fill color cells are: FillForegnd and Fillbackgnd.

    This code will change the fill of the first selected shape in a window to red:

    ActiveWindow.Selection(1).Cells(“FillForegnd”).Formula = “rgb(255,0,0)”

    That should get you started!

    - Chris

  • Lisa Wenzel says:

    Hello Guy,

    I’m looking for a way to find out the master of a selected shape and use this name to select the same shape on another page. how can I read the name of the Master (and the position of the selected shape) and use this to select the same shape on another page?

    Perhaps you have an idea or can write an article about. I would like to move or delete a shape which is on all pages on the same position with one action.

    best regards,
    Lisa

  • Jacob says:

    Hello Visio Guy,

    You have very good knowledge in Visio programming and i am sure you can help me with my problem. I am creating an orgchart using Visio.Application.ActivePage.Drop(vShape, dblXLocation, dblYLocation). The chart is fine but I wanted to “arrange subordinates” “side by side” “Double Side” programmatically. How can I do so?
    If it’s not possible, is there a way of mentioning the default subordinate positioning type as “side by side” “Double Side”

    Jacob

  • Visio Guy says:

    Hi Jacob,

    What you want to do is possible, but it’s hard to describe in just a comment. I’ll try and give you some hints here.

    You need to (programmatically) select the shape that is the parent of the children to be arranged, then call the appropriate OrgChart add-on commands.

    For Visio 2007, the add-on is called “OrgC11″, but you have to know which arguments to pass to it. And the arguments have changed in the past and might change in the future, so you need to be sure which version of Visio is being used.

    These are found by examining the Visio UIObject, looking at the actions for the buttons in the “Organization Chart” toolbar.

    A few code examples would then be:

    Call Visio.Application.Addons(“OrgC11″).Run(“/toolbar_horiz1″)
    Call Visio.Application.Addons(“OrgC11″).Run(“/toolbar_sidebyside2″)

    Again, that is with the superior shape selected. The arguments for Run() were found by digging through the document’s CustomToolBarSets to find the AddOnName and AddOnArgs for the Organization Chart toolbar. I don’t know if those arguments will work in Visio 2003, though.

    Get the Visio 2007 SDK to find out more about analyzing the User Interface.

    Alternatively, you can figure out which shapes are “child” shapes of a particular org-chart box, then do your own layout on them. To determine which shapes are connected to which shapes, you’ve got to do some work. Have a look at this thread in the Visio Guy forums to get started:

    How to Identify the Shapes Connected by a Specific Connector

  • Jacob says:

    Hi Visio Guy,

    Thank you very much for your help. It worked. I searched for this in Microsoft sites and in several other sites. But only your solution worked.

    Based on your advice, I tried examining the Visio UIObject, looking at the actions for the buttons in the “Organization Chart” toolbar but could not figure out how to do it. Can you throw some lights on it? I could reach up to the UIObject in object browser.

    I also need to insert picture to my orgchart shape. I went through your example. It works fine from Macro. I am using VB.NET 2005. How can I do it from VB.NET. The main problem I faced is there is no “sendkey”, the sendkeys.Send function does not accept argument false after the {Enter}.

    Hope I did not confuse you.

    Once again Thanks for the help

    Jacob

  • Jacob says:

    Hi Visio Guy,
    Do you have any suggestions for inserting picture into visio shape from vb.net
    Jacob

  • Visio Guy says:

    Hi Jacob,

    Shape objects have an Import method. Just call shp.Import(filepath)

    Cheers,

    Chris

  • Jacob says:

    Hi Chris,

    Thankyou for the quick response. Shape.Import(filename) is importing the file to the page not into the shape. Am i doing something wrong

    Jacob

  • Visio Guy says:

    If your shape is a group (ie: shp.Shapes.Count > 0) the image should be imported into the group.

  • Jacob says:

    You are right. It’s getting imported. How do I achieve the same behaviour of InsertPicture i.e. when I use shape.import(…), it does not resize the picture to the height of the shape nor it aligns the picture to the left of the shape. Please help.
    This is one solution I found while searching for this. It works from VBA / Macro. But I am opening Visio from my VB.NET application and setting the OrgChart.
    ——————————————————————————————————————————-
    Dim shp As Visio.Shape
    Dim adn As Visio.Addon
    Set adn = Visio.Addons(“OrgC11″)
    ‘Loop thru your shapes
    Set shp = Visio.ActivePage.Shapes(“Assistant”)
    Visio.ActiveWindow.DeselectAll
    Visio.ActiveWindow.Select shp, Visio.VisSelectArgs.visSelect
    SendKeys “C:\Users\davidp\Pictures\anypicture.jpg{ENTER}”, True
    adn.Run “/cmd=InsertPicture”
    ‘end loop
    ——————————————————————————————————————————–

    Also in your previous post you have mentioned about examining the Visio UI object. Do you use some tools for it? Can you share with me how to do it?

  • Jacob says:

    Hi Visio Guy,
    Do you have any suggestions for me
    Jacob

  • Sam says:

    How want to edit visio shape data programatically plz reply.

  • Sam,

    Maybe this will help.

    Also have a look around the forum, there’s a topic about something that might come close here. Good luck

  • Anna says:

    Visio experts, please help!

    I would like to create a macro to run the add-ons. Can someone please tell me how? Thanks!

  • Visio Guy says:

    Hi Anna,

    The Visio.Application object has an add-ons collection. You can start there…

    Also, for general questions, not related to the Visio Guy article, please try our forum: http://www.visguy.com/vgforum

    Cheers,

    Chris

  • Abdul Ghani says:

    Hi Visio Guyz,

    I am working on a project where in I need to save the properties of master in to SQL DataBase, is there any way to accomplish this. I have done allot of googling for but did’nt any useful article hope you can help me.

    Its Urgent..
    Please reply

  • flow3rgirlz says:

    Hi,

    i’m new with vba and macro. I tried to run your code but it requires me the insert macro. Can u please explain to me how to run the code. Thanks.

  • flow3rgirlz says:

    Hi,

    I draw a picture in visio. I would like to save the picture as master in stencil. Is there a way to create a new stencil and add the picture as master in it? I tried to google it and still couldnt find any way to do it. I also try to make a dummy stencil and try to edit it as what u shown above. But it still doesn’t work. Can anyone please help me out. I’m sort of stuck in here.

    Thanks in advance…

  • Visio Guy says:

    Hi F3G,

    This article is about programmatically editing masters.

    To manually create a master, you just drag from the drawing to a stencil. It’s best to group whatever you draw first into a single shape first (Shape > Grouping > Group or Ctrl + G)

    You can create a new stencil in which to create the master via:

    File > Shapes > New Stencil

    Or you can put the master in the “local” stencil of a document:

    File > Shapes > Show Document Stencil

    Stencils that open with Visio templates (as purchased from Microsoft) are non-editable. Stencils that you create open read-only when opened with a template. But you can click on the icon in the top-left corner of the stencil and choose “Edit Stencil” to make it writable.

  • flow3rgirlz says:

    Hi Visio Guy,

    I would like to program it automatically using vba.

  • Robert says:

    Hi Visio Guy,
    This is a great macro concept but there is one little bit still missing and I cant seem to figure it out. I have custom built shapes with some basic stencil icons. Well when I change the Masters at the shapesheet level, manually or programicly, my icon is reduced to a garbled mess.
    How do I keep my icon from changing?

  • Visio Guy says:

    Hi Robert,

    1. Right click a master
    2. Choose Edit Master > Master Properties…
    3. Un-check: Generate icon automatically from shape data

  • aannuu says:

    hi

    I just want to create a visio application using .net with a textbox and a image insertion. Please anyone can help me.

  • Visio Guy says:

    Hi aannuu,

    That’s a pretty small question that requires a really big answer!

    You might try posting your question on our Visio Guy Forum and try to be a bit more specific as to what you need to do, and what you don’t understand.

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.