如何获取一个容器header id 到一个变量并传递给vba 来写一个新的header?

How to get a container header id to a variable and passed to vba to write a new header?

我创建了这个脚本来生成一个容器,我想用它来生成容器它工作正常,但我还需要能够为容器提供自定义 header。如您所见,我试图在一个变量中捕获形状 ID,这样我就可以使用该变量来获取容器的形状 ID。尽管如此,我无法获取形状 ID 或静态分配一个形状 ID,我还发现容器具有多个形状 ID。如何识别 header 部分的 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 中传递参数。谢谢

首先要做的事:她已经回答了你的确切问题:http://visguy.com/vgforum/index.php?topic=6787.0

我将整个代码打包到一个函数中,调用该函数会在您作为参数传递的页面上放置一个经典容器,并使用传递的标题参数填充标题。 return值为拖放的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。您应该多读一点,尤其是 return 函数值的概念。另请注意,大多数关于 Visio 编程的问题在 VBA 和 Excel 中完全相同,只是与 document/workbook 的交互有时不同。正确搜索可以找到很多答案

一些好的链接是:

前两个链接是 must-read 恕我直言,如果您想更深入地了解 VBA,另外两个链接很棒。