Programatically Adding a Master to the Page

Started by ankit_visguy, July 04, 2016, 06:45:13 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ankit_visguy

Hi,

Edited by Visguy: Replaced "stencil" with "master". "stencil" is the whole library/palette, "master" is a shape inside the library.

I have a stencil called 'Data Element' that contains sub shapes ('Heading', 'SubHeading', 'DB' etc). Through code I want to add this master and amend the text on sub shapes. How can I do that. I can add the master on page but not sure how to refer the sub shapes with in the shape with the name. Can anyone help ?

Thanks,
Ankit Sharma

Visio Guy

Hi AV,

The general steps:

1. Get the document that contains the master
2. Get the master
3. Get the target page
4. Drop the master


'// Get a stencil, which is a document from the open documents:
Dim stn As Visio.Document
Set stn = Visio.Documents.Item(3) '//...probably search by document name or something.

'// Get a master by name from the stencil:
Dim mst As Visio.Master
Set mst = stn.Masters.Item("Data Element") '//...'Object name not found' error if item-by-name doesn't exist

'// Get a page object. This could be any page in any open
'// document. It doesn't have to be visible or active:
Dim pg As Visio.Page
Set pg = Visio.ActivePage

'// Drop the shape on the page. You drop a master on a page.
'// The result is an "instance of the master", which is a "shape":
Dim shp As Visio.Shape
Set shp = pg.Drop(mst, 3, 4) '//...X, Y are in INCHES = 2.54 CM

'// Get a subshape within the shape. I believe they are indexed from
'// back-most to front-most. Get the 2nd subshape from the back:
Dim shpSub As Visio.Shape
If (shp.Shapes.Count > 0) Then
  Set shpSub = shp.Shapes(2)
  shpSub.Text = "I'm Number 2!"
End If


'// Cleanup:
Set stn = Nothing
Set mst = Nothing
Set pg = Nothing
Set shp = Nothing
Set shpSub = Nothing

For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

ankit_visguy

Hi VG,

Thanks for responding. I will try this and get back to you in case of any questions. But a quick question. Is there a way to refer a sub shape inside a shape with its name.? Since I have a stencil (Master shape) already created with some sub shapes (with names). Can I refer them by name and not the index number ?

May be like MainShape.shapes("SubShapeName").text ="Desired Text"

Thanks
Ankit