Vba 如何动态创建元素';什么位置?

Vba 如何动态创建元素';什么位置?,vba,outlook,listbox,userform,Vba,Outlook,Listbox,Userform,我有一个Outlook用户表单来显示选定的电子邮件。 由于列表框的列标题中不能包含文本,因此我采用了建议的解决方案 我的问题: Public Sub createListboxHeader(lstBody As ListBox, arrHeaders) Dim lstHeader As ListBox Dim i As Integer 'create new listbox for the header Set lstHeader = Me.Controls.Add("Forms.ListBo

我有一个Outlook用户表单来显示选定的电子邮件。
由于列表框的列标题中不能包含文本,因此我采用了建议的解决方案

我的问题:

Public Sub createListboxHeader(lstBody As ListBox, arrHeaders)
Dim lstHeader As ListBox
Dim i As Integer

'create new listbox for the header
Set lstHeader = Me.Controls.Add("Forms.ListBox.1","NameOnlyForTesting")

With lstBody
    'ensure properties of body-listbox
    .ColumnHeads = False
    .ZOrder (1)
    .SpecialEffect = fmSpecialEffectFlat
    .BorderStyle = fmBorderStyleSingle
End With

With lstHeader
    'properties of header-listbox
    .BackColor = RGB(200, 200, 200)
    .Enabled = False
    .ZOrder (0)
    .SpecialEffect = fmSpecialEffectFlat
    .BorderStyle = fmBorderStyleSingle

    'make column equal
    .ColumnCount = lstBody.ColumnCount
    .ColumnWidths = lstBody.ColumnWidths

    'add header elements
    .AddItem
    For i = 0 To UBound(arrHeaders)
        .List(0, i) = arrHeaders(i)
    Next i

    'positioning of header-listbox
    .Height = 10
    .Width = lstBody.Width
    .Left = lstBody.Left
    .Top = (lstBody.Top - lstHeader.Height) - 0

    Debug.Print lstBody.Width, lstHeader.Height     ' <-- show both '400'
End With

End Sub
Private Sub UserForm_Initialize()

'find emails
Dim selEmails As Outlook.Selection
Set selEmails = getSelectedEmails() 'function not displayed here at StackOverflow

'show emails in List-Box
Call printSelectedEmailsInList(selEmails)

End Sub


Private Sub printSelectedEmailsInList(selectedEmails As Outlook.Selection)
Dim objEmail As Outlook.MailItem
Dim intCounter  As Integer
Dim arrHeaders() As Variant

With Me.lstSelectedEmails
    'configure listbox
    .Clear
    .ColumnCount = 5
    .ColumnWidths = "70;100;100;200;100"

    'configute header (AFTER body!)
    arrHeaders = Array("Date", "From", "To", "Subject", "Folder")
    Call createListboxHeader(Me.lstSelectedEmails, arrHeaders)
    MsgBox Me.Controls("NameOnlyForTesting").Width             '<-- shows'78' instead of '400'

    'fill list with emails
    intCounter = 0
    For Each objEmail In selectedEmails
        .AddItem
        .List(intCounter, 0) = objEmail.SentOn
        .List(intCounter, 1) = objEmail.SenderName
        .List(intCounter, 2) = objEmail.To
        .List(intCounter, 3) = objEmail.Subject
        .List(intCounter, 4) = objEmail.Parent.Name

        intCounter = intCounter + 1
    Next

End With
End Sub
表单初始化后,标题框的位置和大小错误。一些检查显示,函数createListboxHeader()分配了正确的值,并且没有错误。但是在该函数之后(回到初始化中)检查标题框的位置和大小时,值是错误的-证明我所看到的

有时它能正常工作,但大多数情况下不能

代码:

Public Sub createListboxHeader(lstBody As ListBox, arrHeaders)
Dim lstHeader As ListBox
Dim i As Integer

'create new listbox for the header
Set lstHeader = Me.Controls.Add("Forms.ListBox.1","NameOnlyForTesting")

With lstBody
    'ensure properties of body-listbox
    .ColumnHeads = False
    .ZOrder (1)
    .SpecialEffect = fmSpecialEffectFlat
    .BorderStyle = fmBorderStyleSingle
End With

With lstHeader
    'properties of header-listbox
    .BackColor = RGB(200, 200, 200)
    .Enabled = False
    .ZOrder (0)
    .SpecialEffect = fmSpecialEffectFlat
    .BorderStyle = fmBorderStyleSingle

    'make column equal
    .ColumnCount = lstBody.ColumnCount
    .ColumnWidths = lstBody.ColumnWidths

    'add header elements
    .AddItem
    For i = 0 To UBound(arrHeaders)
        .List(0, i) = arrHeaders(i)
    Next i

    'positioning of header-listbox
    .Height = 10
    .Width = lstBody.Width
    .Left = lstBody.Left
    .Top = (lstBody.Top - lstHeader.Height) - 0

    Debug.Print lstBody.Width, lstHeader.Height     ' <-- show both '400'
End With

End Sub
Private Sub UserForm_Initialize()

'find emails
Dim selEmails As Outlook.Selection
Set selEmails = getSelectedEmails() 'function not displayed here at StackOverflow

'show emails in List-Box
Call printSelectedEmailsInList(selEmails)

End Sub


Private Sub printSelectedEmailsInList(selectedEmails As Outlook.Selection)
Dim objEmail As Outlook.MailItem
Dim intCounter  As Integer
Dim arrHeaders() As Variant

With Me.lstSelectedEmails
    'configure listbox
    .Clear
    .ColumnCount = 5
    .ColumnWidths = "70;100;100;200;100"

    'configute header (AFTER body!)
    arrHeaders = Array("Date", "From", "To", "Subject", "Folder")
    Call createListboxHeader(Me.lstSelectedEmails, arrHeaders)
    MsgBox Me.Controls("NameOnlyForTesting").Width             '<-- shows'78' instead of '400'

    'fill list with emails
    intCounter = 0
    For Each objEmail In selectedEmails
        .AddItem
        .List(intCounter, 0) = objEmail.SentOn
        .List(intCounter, 1) = objEmail.SenderName
        .List(intCounter, 2) = objEmail.To
        .List(intCounter, 3) = objEmail.Subject
        .List(intCounter, 4) = objEmail.Parent.Name

        intCounter = intCounter + 1
    Next

End With
End Sub
Public子createListboxHeader(lstBody作为ListBox,ArrHeader)
将标题设置为列表框
作为整数的Dim i
'为标题创建新列表框
Set lstHeader=Me.Controls.Add(“Forms.ListBox.1”,“NameOnlyForTesting”)
带弹体
'确保主体列表框的属性
.ColumnHeads=False
佐尔德先生(1)
.SpecialEffect=FmSpecialEffect扁平
.BorderStyle=fmBorderStyleSingle
以
带头
'标题列表框的属性
.BackColor=RGB(200200200200)
.Enabled=False
佐尔德先生(0)
.SpecialEffect=FmSpecialEffect扁平
.BorderStyle=fmBorderStyleSingle
'使列相等
.ColumnCount=lstBody.ColumnCount
.ColumnWidths=lstBody.ColumnWidths
'添加标题元素
.AddItem
对于i=0到UBound(arrheader)
.List(0,i)=arrheader(i)
接下来我
'标题列表框的定位
.高度=10
.Width=lstBody.Width
.Left=lstBody.Left
.Top=(lstBody.Top-lstsheader.Height)-0

Debug.Print lstBody.Width,lstweader.Height'我通过更改以下内容解决了问题:

Private Sub UserForm_Initialize()
   [...]
End Sub
致:


列表框不能有列标题?那么
.ColumnHeads
属性是什么?您好,braX,属性
.ColumnHeads
为标题激活一个附加行。没错。但不能在这些字段中放置任何文本。只有并行使用
.RowSource
时,此选项才有效。但如果动态创建内容,则不会。