使用vba

时间:2018-05-17 11:36:56

标签: vba outlook-vba

我必须从MeetingItem.Recipient获取用户名,我试过以下来获取它: CStr(MeetingItem.Recipient.Address)得到了这个回应: "/o=POST/ou=Zuerich/cn=Recipients/cn=eicherr"我必须循环完成所有操作 收件人和获取用户名,例如,如果我使用上面的代码,我会得到:

"/o=POST/ou=Zuerich/cn=Recipients/cn=eicherr"
"/o=POST/ou=Group (FYHF23PDLT)/cn=Recipients/cn=kisslingie0e"
"/o=POST/ou=Group (FYHF23PDLT)/cn=Recipients/cn=katzensteink"
"/O=POST/OU=Bern/cn=Recipients/cn=junkerb"
"/o=POST/ou=Group (FYHF23PDLT)/cn=Recipients/cn=tanzg6a7"

我只需要这个字符串的最后一部分,我该怎么做?

注意: kisslingie0e和tanzg6a7这个昵称最后包含了必须避免的三个字符

或者是否有另一种方法可以从MeetingItem.Recipient.Adress获取用户名。 要获得电子邮件,我做了以下事项:

For Each recip In recips

    'Obtain the E-mail Address of a Recipient
    Dim pa As Outlook.PropertyAccessor
    Const PR_SMTP_ADDRESS As String = _
        "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
    Set pa = recip.PropertyAccessor

    Dim email as String
    email = CStr(pa.GetProperty(PR_SMTP_ADDRESS))
    Debug.Print email

End For

3 个答案:

答案 0 :(得分:1)

使用Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress获取SMTP地址 准备好处理空值和错误。

要获取NT登录名(域帐户),请使用PR_ACCOUNT阅读http://schemas.microsoft.com/mapi/proptag/0x3A00001F MAPI属性(DASL名称Recipient.AddressEntry.PropertyAccessor.GetProperty)。

您也可以使用Recipient.AddressEntry.GetExchangeUser().Alias

答案 1 :(得分:0)

删除主要文本的最简单方法是反转字符串并循环,直到找到“/”:

Dim email As String, username As String
Dim i As Integer

email = "/o=POST/ou=Group (FYHF23PDLT)/cn=Recipients/cn=kisslingie0e"

'Reverse string
email = StrReverse(email)

'Loop through string until / is found
For i = 1 To Len(email) Step 1
    If Mid(email, i, 1) = "/" Then
        Exit For
    Else
        username = username & Mid(email, i, 1)
    End If
Next i

'Reverse username
username = StrReverse(username)

如果您需要删除“cn =”,请执行以下操作:

username = Split(username, "=")(1)

如果用户名永远不包含任何数字,您可以删除这样的跟踪:

For i = 1 To Len(username) Step 1
    'Loop until a number occurs
    If IsNumeric(Mid(username, i, 1)) Then
        'Use string until the number
        username = Mid(username, 1, i - 1)
        Exit For
    End If
Next i

答案 2 :(得分:0)

这是另一个建议,如果源在所需字符串之前的“Recipients / cn =”之前是一致的,那么如果它们在第三个字符或倒数第二个字符中是数字,则可选择剥离最后一个字符。

    'find the location of constant, set vEM
    vLoc = InStr(email, "Recipients/cn=")
    vEM = Mid(email, vLoc + 14, 50)
    'Check if third to last or second to last character is numeric
    vOffset = 0
    If IsNumeric(Mid(vEM, Len(vEM) - 2, 1)) Then
       vOffset = 3
    ElseIf IsNumeric(Mid(vEM, Len(vEM) - 1, 1)) Then
       vOffset = 2
    Else
       vOffset = 0
    End If
    vEM = Left(vEM, Len(vEM) - vOffset)