如何获取容器标头ID到变量并传递给vba以编写新标头?

时间:2019-07-31 19:11:07

标签: vba visio

我已经创建了此脚本来生成容器,我想使用它来生成容器。它可以正常工作,但我还需要能够为容器提供自定义标头。如您所见,我试图在变量中捕获形状ID,因此可以使用该变量获取容器的形状ID。但是,我无法获得形状ID或静态分配一个形状ID,我还发现该容器具有多个形状ID。如何识别标头部分的ID。我还需要能够在容器中放置形状。我按照Microsoft的说明进行操作,并尝试使用

vsoContainerShape.ContainerProperties.AddMember vsoShape, 
visMemberAddExpandContainer

但是那不起作用。

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

我需要能够获取容器标题的形状ID,并将其存储在变量中,以便可以使用变量在ItemFromID中传递参数。谢谢

1 个答案:

答案 0 :(得分:0)

第一件事:她的确切问题已经回答了她:http://visguy.com/vgforum/index.php?topic=6787.0

我将整个代码打包到一个函数中,调用该函数将在您作为参数传递的页面上放置一个经典容器,并使用传递的caption参数填充标题。返回值是删除的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

您最近似乎发布了很多关于相同或相似问题的问题,一旦您了解VBA,其中大部分都是非常基本的。您应该阅读一些内容,尤其是关于函数返回值的概念。还应注意,有关Visio中编程的大多数问题在VBA for Excel中完全相同,只是有时与文档/工作簿的交互不同。正确搜索后会发现很多答案

一些好的链接是:

前两个链接是必读的恕我直言,如果您想更深入地研究VBA,另外两个非常有用。