0

I have created this script to generate a container that I want to use it to generate containers It works fine but I also need to be able to give the containers a custom header. As you can see I tried to capture the shape id in a variable so I could use the variable to get the shape Id for the container. Nevertheless, I cannot get the shape id or assign one statically I also found out that the container has more than one shape ID. How do I Identify the ID for the header portion. I also need to be able to drop shapes in the container. I followed Microsoft instructions and tried using

vsoContainerShape.ContainerProperties.AddMember vsoShape, 
visMemberAddExpandContainer

However that doesn’t work.

Sub Add_Container()


    Dim DiagramServices As Integer

    DiagramServices = ActiveDocument.DiagramServicesEnabled

    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + 
visServiceVersion150

    Dim visapp As Visio.Application

    Dim vlan30 As Visio.Document

    Dim node As Visio.Shape

    Dim vlan30id As Integer





Application.Documents.OpenEx(Application.GetBuiltInStencilFile(visBuiltInStencilContainers, visMSUS), visOpenHidden)


Application.Windows.ItemEx("container.vsdm").Activate 'need to activate


Application.ActiveWindow.Page.DropContainer vlan30.Masters.ItemU("Classic"), Nothing  


vlan30id = vlan30.ID

Debug.Print vlan30id

Dim v30chars As Visio.Characters
Set v30chars = Application.ActiveWindow.Page.Shapes.ItemFromID(vlan30id).Characters
v30chars.Begin = 0
v30chars.End = 7
v30chars.Text = "Vlan_30"

vlan30.Close

ActiveWindow.DeselectAll

'Restore diagram services

ActiveDocument.DiagramServicesEnabled = DiagramServices


End Sub

I need to be able to get the shape id for the heading of the containers and stored in a variable so I can use the variable for passing the argument in the ItemFromID. Thanks

  • No need for activate (as it is about as bad as using .select) . The 'Documents.open' should already return the document you opened. Likewise with the 'DropContainer' Method, it returns the dropped shape directly, so no need to go looking for the shape on the page. – L8n Aug 01 '19 at 13:14

1 Answers1

1

First things first: your exact question was already answered her: http://visguy.com/vgforum/index.php?topic=6787.0

I packaged the whole code into a function, calling the function will drop a classic container on the page you passed as argument and fill the caption with the passed caption argument. The return value is the dropped Container, bottom function shows how to use the function and add shapes to the container.

'@Folder("ExampleDropContainer")
Option Explicit

Public Function DropContainerWithCaption(pg As Visio.Page, caption As String) As Visio.Shape

    Dim vsPg As Visio.Page
    Set vsPg = pg

    Dim vsStencilName As String
    vsStencilName = Application.GetBuiltInStencilFile(visBuiltInStencilContainers, visMSUS)

    Dim vsStencil As Visio.Document
    Set vsStencil = Application.Documents.OpenEx(vsStencilName, visOpenHidden)

    Dim vsMas As Visio.Master
    Set vsMas = vsStencil.Masters.ItemU("Classic")

    'If you already had the shapes you want to have inside the continer you can replace "Nothing" with them.
    Dim droppedContainer As Visio.Shape
    'Set droppedContainer = vsPg.DropContainer(vsMas, Nothing)
    'Using page.Drop circumvents some issues when a shape already occupies the space where the shape is to be dropped.
    Set droppedContainer = vsPg.Drop(vsMas, 0, 0)

    droppedContainer.Text = caption

    Set DropContainerWithCaption = droppedContainer

End Function


Sub TestExample()

    Dim newContainer As Visio.Shape
    Set newContainer = DropContainerWithCaption(ActivePage, "Bananas")

    'Example on how to add a Shape to the container, someShape is a visio.shape object
    'newContainer.ContainerProperties.AddMember someShape

End Sub

You seem to have posted a lot of questions concerning the same or similar problems lately, most of which are quite basic once you get to know VBA. You should read up a bit, especially on the concept of return values of functions. Also be aware that most questions regarding programming in Visio are exactly the same in VBA for Excel, only the interaction with the document/workbook is sometime different. Many answers can be found when searching properly

Some good links are:

The first two links are a must-read IMHO, the other two are great if you want to dive deeper into VBA.

L8n
  • 728
  • 1
  • 5
  • 15
  • Thank you. It works perfect and the explanations are clear. I'm doing a lot of reading. Again Thanks – Michael Ziegler Aug 02 '19 at 17:15
  • @MichaelZiegler If this answer resolves your problem can you mark it as accepted?It makes it more likely that someone else will find the answer (and I get some imaginary internet points ^^). – L8n Aug 02 '19 at 18:21