【问题标题】:How to dynamically create an element's position?如何动态创建元素的位置?
【发布时间】:2020-03-19 18:48:58
【问题描述】:

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

我的问题:

表单初始化后,标题框的位置错误,大小错误。一些检查表明,函数 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

【问题讨论】:

  • 列表框不能有列标题?那么.ColumnHeads 属性是什么? docs.microsoft.com/en-us/office/vba/api/…
  • 你好 braX,.ColumnHeads 属性激活了标题的附加行。确实如此。但是您不能在这些字段中放置任何文本。这仅在您并行使用.RowSource 时有效。但如果您动态创建内容,则不会。

标签: vba outlook listbox userform


【解决方案1】:

我通过改变解决了我的问题:

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

到:

Private Sub UserForm_Activate()
   [...]
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 2013-06-03
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    相关资源
    最近更新 更多