Home » Code, Development

Edit Visio Masters Programmatically…the Right Way!

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:

[code lang=”vb”]
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:

[code lang=”vb”]
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


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!


  • Chris says:


    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,

  • 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”


  • 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 says:

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

  • Visio Guy says:

    Hi Jacob,

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



  • 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


  • 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.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

  • 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



  • 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:


    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:


    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:


    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.

  • Yousuf says:

    My question specially for Visio Guy (Chris Roth): how can you find the commands for Visio Add ons like OrgC11?
    you mentioned:
    Call Visio.Application.Addons(“OrgC11?).Run(“/toolbar_horiz1?)
    Call Visio.Application.Addons(“OrgC11?).Run(“/toolbar_sidebyside2?)
    How can we find information about other commands? Using Macro recoderder is no help!

    Many thanks,

  • gnash says:

    My question related to this function is about the assignment when the shape is grouped:
    Set shp = mstCopy.Shapes(1)

    This seems to point to the first the first shape in the grouped shape and not the containing “wrapper” shape.

    My goal is to perform the following on a master shape in the stencil using the code in this article:

    shp.CellsSRC(visSectionObject, visRowMisc, visHideText) = True

    If I run this code against a shape object on the layout page created from a master, it is executed against a parent shape object that may or may not contain additional shapes ~ shape.shapes.count > 1 = true ~ the shape is grouped…

    So inspecting the master of the shape I see the same internals, namely shape.shapes.count > 1 = true. The assumption with the above is that the first shape is assumed to point to the container shape or a grouped object? I would expect based on the shape object model something like this to be the suggested approach.

    Set shp = mstCopy.Shapes(1).RootShape

    For a grouped shape on the layout page RootShape points to the desired containing shape, however for a master shape RootShape = nothing.

    What is the suggested approach to reference the RootShape of a master shape?

  • Visio Guy says:

    Hi gnash,

    Shapes have a shp.ContainingPage, shp.ContainingMaster or shp.ContainingShape (sub-shapes of groups). I would have preferred .ContainingPage to work for both pages and masters, but oh well.

    But you don’t have to go that far. Once you have a master, you can get it’s PageSheet, ie: mstCopy.PageSheet. This is a ShapeSheet, essentially the same as a page, but with slight variation in cell names.

    If you want to hide text on a shape, you don’t want the “master wrapper”, you really want the shape. So you should do something like this:

    If mstCopy.Shapes.Count > 0 Then

    Dim shp As Visio.Shape
    Set shp = mstCopy.Shapes(1)

    ‘// You don’t need the extra step of the cell object
    ‘// but it makes the following code easier to read:
    Dim c as Visio.Cell
    set c = shp.CellsSRC(visSectionObject, visRowMisc, visHideText)

    // Several ways to set a cell’s value or formula:
    c.ResultIU = 1 ‘// 0 = false, 1 = true in the ShapeSheet
    c.Formula = “TRUE”

    End If

    In your code you were doing:

    shp.CellsSRC(visSectionObject, visRowMisc, visHideText) = true

    but you should do something more like:

    shp.CellsSRC(visSectionObject, visRowMisc, visHideText).ResultIU = 1

    as the code snippet above shows.

  • I wrote a Module to copy the insert Picture function from the Visio Organigramm addon.
    Feel free to use it.

    <br />
    ''<br />
    '' Insert Pictures into Visio-shapes<br />
    '' By Christian Lindenau 16.04.2012<br />
    '' http://www.facebook.com/profile.php?id=100000570159833<br />
    <p>Option Explicit</p>
    <p>'' Pseudo properties<br />
    Private Sub set_x(ByRef sh As Shape, ByVal value As Double)<br />
        sh.Cells("pinx").FormulaForce = CStr(value) + " mm"<br />
    End Sub</p>
    <p>Private Sub set_y(ByRef sh As Shape, ByVal value As Double)<br />
        sh.Cells("piny").FormulaForce = CStr(value) + " mm"<br />
    End Sub</p>
    <p>Private Sub set_h(ByRef sh As Shape, ByVal value As Double)<br />
        sh.Cells("height").FormulaForce = CStr(value) + " mm"<br />
    End Sub</p>
    <p>Private Sub set_w(ByRef sh As Shape, ByVal value As Double)<br />
        sh.Cells("width").FormulaForce = CStr(value) + " mm"<br />
    End Sub</p>
    <p>Private Function get_x(ByRef sh As Shape) As Double<br />
        get_x = CDbl(sh.Cells("pinx").Result("mm"))<br />
    End Function</p>
    <p>Private Function get_y(ByRef sh As Shape) As Double<br />
        get_y = CDbl(sh.Cells("piny").Result("mm"))<br />
    End Function</p>
    <p>Private Function get_w(ByRef sh As Shape) As Double<br />
        get_w = CDbl(sh.Cells("width").Result("mm"))<br />
    End Function</p>
    <p>Private Function get_h(ByRef sh As Shape) As Double<br />
        get_h = CDbl(sh.Cells("height").Result("mm"))<br />
    End Function</p>
    <p>'' The stuff you are looking for :)<br />
    Public Sub Insert_Pic(ByRef sh As Shape, Path As String)</p>
    <p>    If sh.CellsU("User.HasPicture") = 0 Then</p>
    <p>        Dim shape_x, pic_x As Double<br />
            Dim shape_y, pic_y As Double<br />
            Dim shape_w, pic_w As Double<br />
            Dim shape_h, pic_h As Double</p>
    <p>        Dim aspect_ratio As Double</p>
    <p>        Dim pic As Shape</p>
    <p>        shape_h = get_h(sh)<br />
            shape_x = get_x(sh)<br />
            shape_y = get_y(sh)<br />
            shape_w = get_w(sh)</p>
    <p>        Set pic = sh.Import(Path)</p>
    <p>        pic_h = get_h(pic)<br />
            pic_w = get_w(pic)</p>
    <p>        aspect_ratio = pic_w / pic_h</p>
    <p>        pic_w = shape_h * aspect_ratio</p>
    <p>        sh.Resize visResizeDirE, pic_w, visMillimeters</p>
    <p>        set_h pic, shape_h * 0.9<br />
            set_w pic, pic_w * 0.9<br />
            set_x pic, pic_w / 2<br />
            set_y pic, shape_h / 2</p>
    <p>        pic.AddNamedRow visSectionUser, "Visible", visTagDefault<br />
            pic.AddNamedRow visSectionUser, "Margin", visTagDefault</p>
    <p>        pic.CellsU("User.Margin").FormulaForceU = "Sheet." + CStr(sh.ID) + "!User.Margin+0.5 pt"<br />
            pic.CellsU("User.Visible").FormulaForceU = "=AND(Sheet." + CStr(sh.ID) + "!User.HasPicture, " _<br />
            + "Sheet." + CStr(sh.ID) + "!User.ShowPicture, Sheet." + CStr(sh.ID) + "!User.DocShowPicture)"</p>
    <p>        sh.CellsU("User.HasPicture").FormulaForceU = "1"</p>
    <p>        sh.CellsU("User.ShowPicture").FormulaForceU = "1"<br />
            sh.CellsU("User.DocshowPicture").FormulaForceU = "=TheDoc!User.DocShowPicture"<br />
            sh.CellsU("User.Margin").FormulaForceU = _<br />
                 "=IF(User.ShapeType=6,0,MIN(0.635 mm,MIN(Height*0.05,Width*0.05)))"</p>
    <p>        sh.CellsU("User.ExpandedForPicture").FormulaForceU = "1"</p>
    <p>        sh.CellsU("User.PictureAspectRatio").FormulaForceU = Str(CDbl(aspect_ratio))<br />
            sh.CellsU("User.PictureAreaWidth").FormulaForceU = "=IF(TextDirection,Width,Width/3)" & _<br />
                 "-IF(User.ShapeType<2,2*User.Margin+1 pt,1 pt)"</p>
    <p>        sh.CellsU("User.PictureAreaHeight").FormulaForceU = _<br />
            "=IF(TextDirection,Height/3,Height)-IF(User.ShapeTypeUser." & _<br />
            "PictureAreaAspectRatio,User.PictureAreaWidth," & _<br />
    <p>        sh.CellsU("User.PictureHeight").FormulaForceU = _<br />
                 "=IF(User.PictureAspectRatio>User.PictureAreaAspectRatio," & _<br />
    <p>        sh.CellsU("User.DividerOffset").FormulaForceU = _<br />
            "=MAX(0,IF(TextDirection=0,(Height-((Height-TEXTHEIGHT(" & _<br />
            "TheText,TxtWidth))/2)-User.CalcTextLineHeight),(Width-((" & _<br />
    <p>        sh.CellsU("User.CalcTextLineHeight").FormulaForceU = _<br />
    <p>        sh.CellsU("User.PictureID").FormulaForceU = "=""" + pic.Name + """"</p>
    <p>        Set pic = Nothing</p>
    <p>    End If<br />
    End Sub<br />

  • Sorry for exploding the commentbox :/

    VG: no worries, I cleaned it up a bit!

  • Jay says:

    Hi VisioGuy,

    I’m trying to save instances from one master stencil as new masters (otherwise create my own stencil and copy a stencil from another file into my custom stencil). So far I’ve managed to create the stencil and create an empty master, but I can’t seem to find a way to copy the master over … even with (Set mstCopy = mst.Open). Any ideas?

    Btw the master I’m trying to copy contains a few user defined cells which I’d like to capture as well.

  • Jay says:

    I think I may have had the wrong approach to my problem. After going back to the design board I think I’ve found a suitable workaround which simply requires adding images as fill patterns. Does anyone know how this is done though C#?


  • Josh309 says:

    super helpful. this site is better than online microsoft help..

  • Milan says:

    Hi Visio,

    PLEASE REPLY ASAP. My deadline is approaching

    I am trying to auto fit my text in shape cells. I found out in visio we have to provide GUARD(MAX(TEXTWIDTH(TheText), 1 in)) in width and
    GUARD(MAX(TEXTHEIGHT(TheText,Width), 0.75 in)) in hieght .

    But how to achieve this in vb.net

    I tried below code but it threw a compiler error.
    vToShape.Cells(“Width”).Formula =(MAX(TEXTWIDTH(TheText),0.75 in))

  • ChrisB says:

    Hi Visio Guy,
    Firstly, this is a top rate site – thanks for your time and effort in pulling this information together.

    Secondly, I do have a question relating to Shape Masters – of course!

    I have written a visio app to document a technical data solution in terms of relationships between tables and SQL scripts (table x read by script 1 and writen to table y which is read by script 2 and written to table z wich is read….. – you get the picture?)

    When you drop a script object on the screen (a process shape) it identifies if there is a corresponding page in the document of that same name – i.e. ‘Script 2’ – if so, it changes to that page – if not it creates a new page (called Script 2) and drops a ‘code_block_desc’ shape onto the page (having set pagination, scaling etc., so it fills the page).

    The code_block_desc object is a shape master that I have built myself – in truth a collection of other shapes arranged in a way to represent a form where key data is captured.

    This shape master is made up of lots of individual shapes – and these are NOT grouped prior to being saved as the Shape Master.

    One of the first elements (sub-shapes) in the master is a placeholder for the script name – however, I can’t seem to find a way to select the element (which is not a sub-shape as it’s not a group) to automatically set the script name value.

    I’m trying to set up other automation tasks (hyperlinks to script files based upon text values stored in other elements) but all are dependent upon me extracting text from various elements.

    I’m happy to send a copy of the shape master if you’re happy to send me an email address – any help you can offer will be greatly appreciated.


    (Another) Chris

  • ChrisB says:

    I’ve answered my own question – I put some nominal text into each sub-shape/element that would uniquely identify it, and then ran this simple code block:
    <br />
    Function test_subSelect()</p>
    <p>Dim i As Integer<br />
    Dim j As Integer<br />
    Dim shObj As Shape</p>
    <p>With Visio.ActiveWindow<br />
        For i = 1 To .Page.Shapes.Count<br />
            Debug.Print i & vbTab & .Page.Shapes(i).Name<br />
            If Left(.Page.Shapes(i).Name, 15) = "Code_Block_Desc" Then<br />
                Set shObj = .Page.Shapes(i)<br />
                For j = 1 To shObj.Shapes.Count<br />
                    'Debug.Print j & vbTab & shObj.Shapes(j).Name<br />
                   Debug.Print j & vbTab & shObj.Shapes(j).Text<br />
                Next j<br />
            End If<br />
        Next i<br />
    End With<br />
        'Debug.Print .ContainingShape.Name<br />
       'Debug.Print .ContainingShape.Shapes.Count</p>
    <p>End Function<br />

    This returned the following:

    1 Code_Block_Desc.47
    3 Script Name:
    5 Version
    6 Date
    8 Description:
    9 Change Summary
    11 Test Script Location:
    12 Developer
    13 Reviewer
    14 Quality
    16 Script Location:
    17 Script_Name_Text_Block
    18 dt1
    19 v1
    20 cs1
    21 Script_Steps_1
    22 dt2
    23 v2
    24 cs2
    25 dt3
    26 v3
    27 cs3
    28 dt4
    29 v4
    30 cs4
    31 dt5
    32 v5
    33 cs5
    34 dt6
    35 v6
    36 cs6
    37 QA_Script_Location_Text_Block
    38 d1
    39 d2
    40 d3
    41 d4
    42 d5
    43 d6
    44 r1
    45 r2
    46 r3
    47 r4
    48 r5
    49 r6
    50 q1
    51 q2
    52 q3
    53 q4
    54 q5
    55 q6
    56 Script_Location_Text_Block
    60 Script_Steps_2

    Giving me a working map of each component part of my master shape, meaning I can now identify, access or set the value of any cell in my master shape programatically.

    If there’s a way to re-order the index of each element so no1 starts top left, and 60 is bottom right, then that would be even more helpful – but not crucial.

    Thanks again…

  • ChrisB says:


    Opened the shae Master to edit it, and looked at the Drawing Explorer Window – used this to navigate around each component piece of the master and renamed ‘process.17’ to ‘Script_Name’, etc., etc.

    Now I can call upon the value or set the value of every single component by name or ID.

    <br />
    Function test_subSelect()</p>
    <p>Dim i As Integer<br />
    Dim j As Integer<br />
    Dim shObj As Shape</p>
    <p>With Visio.ActiveWindow<br />
        For i = 1 To .Page.Shapes.Count<br />
            Debug.Print i & vbTab & .Page.Shapes(i).Name<br />
            If Left(.Page.Shapes(i).Name, 15) = "Code_Block_Desc" Then<br />
                Set shObj = .Page.Shapes(i)<br />
                For j = 1 To shObj.Shapes.Count<br />
                    Debug.Print j & vbTab & shObj.Shapes(j).Text & " (" & shObj.Shapes(j).Name & ")"<br />
                Next j<br />
            End If<br />
        Next i<br />
    End With<br />

    now gives me:
    <br />
    1   Code_Block_Desc.48<br />
    1    (Background)<br />
    2    (Script_Name_Back)<br />
    3   Script Name: (Script_Name_lbl)<br />
    4    (Change_Hdr)<br />
    5   Version (Ver_lbl)<br />
    6   Date (Dat_lbl)<br />
    7    (Desc_Back)<br />
    8   Description: (Desc_lbl)<br />
    9   Change Summary (Sum_lbl)<br />
    10   (Test_Script_Back)<br />
    11  Test Script Location: (Test_Script_lbl)<br />
    12  Developer (Dev_lbl)<br />
    13  Reviewer (Rev_lbl)<br />
    14  Quality (Qua_lbl)<br />
    15   (Script_Loc_Back)<br />
    16  Script Location: (Script_Loc_lbl)<br />
    17   (Script_Name)<br />
    18   (Dat1)<br />
    19   (Ver1)<br />

    My solution is now really taking shape.

  • Visio Guy says:

    Hi Chris,

    You can name the subshapes within a shape. Even better, these shapes are protected in scope from other shapes on a page, so you can have ten shapes with sub-shapes named “Script”.

    These subshapes won’t get the auto-indexed names that you may have run into: Script.1, Script.2, Script.3, etc.). Because they are ‘protected’ within the group. Top-level shapes on a page or within a group (ie: ‘in the same scope’) can’t have the same name, and will get the suffix-indexing, just for your information.

    So if you’re using Visio 2010, go to the Developer tab and click Shape Name. There you can rename a selected shape or subshape within a group. Now you can easily find a subshape by name.

    <br />
    Set shpSub = shpGroup.Shapes("Script")<br />

    Or you can iterate:
    <br />
    For Each shpSub In shpGrp.Shapes<br />
         If (shpSub.Name = "Script") Then<br />
               '....do something<br />
        End If<br />
    Next shpSub<br />

  • Rajashekar says:

    How to hide a shape from visio programatically using C#??

  • Visio Guy says:

    Hi Rajashekar,

    There are many ways to hide shapes. You’ll want to read this article first: The Hidden World of Visio Shapes.

    So, the first example in the article would look something like this in C#:

    <br />
    // shp is a Visio.Shape object. In this<br />
    // example, shp has 3 geometry sections.<br />
    //<br />
    // '1' is 'true' in the Visio ShapeSheet.<br />
    shp.Cells["Geometry1.NoShow"].ResultIU = 1;<br />
    shp.Cells["Geometry2.NoShow"].ResultIU = 1;<br />
    shp.Cells["Geometry3.NoShow"].ResultIU = 1;<br />
    shp.CellsU["HideText"].ResultIU = 1;<br />

  • Rajashekar says:

    shp.Cells[“Geometry1.NoShow”].ResultIU = 1;
    shp.Cells[“Geometry2.NoShow”].ResultIU = 1;
    shp.Cells[“Geometry3.NoShow”].ResultIU = 1;
    shp.CellsU[“HideText”].ResultIU = 1;

    i guess the above code is not working in visio 2003.
    can u sugesst any othr method.

  • Visio Guy says:

    That is example code. Your shape might not have all three Geometry sections.

  • Rajashekar says:

    Can u suggest a method in which multiple shapes are put in a box?

  • Visio Guy says:

    Hi Rajashekar,

    Your question is too general and would take hours and hours to answer. Plus, it doesn’t really relate to this article.



  • Rajashekar says:

    If a shape contains multiple sheets in Visio,then how can we add text to different sheet programmatically using C#? can anyone help me in this

  • Visio Guy says:

    Shapes have shapes collections if they are groups.

    // Get the selected shape:
    Visio.Shape shp = Visio.ActiveWindow.Selection(1);

    // Set text of the third sub-shape inside of shp:
    shp.Shapes[3].Text = “Test”;

  • Gediminas says:

    I am just trying to get into Visio shapesheet and basics of VBA this function you posted would suit me perfect, but I my basic knowledge of VBA is not enough. I can’t find a way to modify your function so it would work in my drawing. Maybe you could add extra annotations, where I should enter master name and how I should do that. I mean either i should use something like Set mst = Visio.ActiveDocument.Masters.Item(“MasterName”) or other expresion

  • […] Visio Guy » Edit Visio Masters Programmatically…the … – 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 …… […]

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 lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">

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