简体   繁体   English

Excel VBA - 添加一个 ActiveX TreeView 控件到带有代码的工作表

[英]Excel VBA - Add an ActiveX TreeView Control to sheet with code

I'm trying to create an ActiveX TreeView Control on a sheet and name it via code.我正在尝试在工作表上创建一个 ActiveX TreeView 控件并通过代码对其进行命名。

Interestingly enough when I copy a sheet, the TreeView Control won't copy with it so my work-around is to just insert the Control via code.有趣的是,当我复制工作表时,TreeView 控件不会随之复制,因此我的解决方法是通过代码插入控件。 I dug around and couldn't find anything about creating it with code.我四处寻找,找不到任何关于用代码创建它的东西。

Anyone know how to do this?有人知道怎么做吗?

Try the next piece of code, please.请尝试下一段代码。 Only to guide you on the aproach:仅用于指导您的方法:

Sub AddTreeControl()
  Dim sh As Worksheet, TrC As OLEObject, TC As Object
  Set sh = ActiveSheet
  Set TrC = sh.OLEObjects.Add(ClassType:="MSComctlLib.TreeCtrl.2", link:=False _
        , DisplayAsIcon:=False, left:=200, top:=70, width:=150, height:= _
        200)
  Set TC = TrC.Object
  With TrC
    .Name = "TreeControlX"
    .Select
  End With
  With TC
    .nodes.Add Key:="item 1", Text:="Parent 1"
    .nodes.Add "item 1", tvwChild, Key:="one", Text:="ITEM 1, Child node 1"
    .nodes.Add "item 1", tvwChild, "two", "ITEM 1, Child node 2"
    
    .nodes.Add Key:="item 2", Text:=" Parent 2"
    .nodes.Add Key:="item 3", Text:=" Parent 3"
  End With
End Sub

Thanks FaneDuru.谢谢法内杜鲁。

This is what I came up with:这就是我想出的:

Sub Test()
'Create an ActiveX TreeView, assign dimensions, and name
    With Sheets("Sheet1").OLEObjects.Add(ClassType:="MSComctlLib.TreeCtrl.2",_
        Left:=55, Top:=450, Width:=330, Height:=400)
       .Name = "InsertNameHere"
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM