如何使用 vba 宏从 Outlook 中获取字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14517614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:22:04  来源:igfitidea点击:

How to get From field from Outlook using vba macro

vbaoutlook-2010

提问by erni

I am writing macro that will set a signature after choosing the From field or for example clicking reply. My problem is that I don't know how to get the From field value. I know how to set this field.

我正在编写宏,它将在选择“发件人”字段或例如单击回复后设置签名。我的问题是我不知道如何获取 From 字段值。我知道如何设置这个字段。

Function GetBoiler(ByVal sFile As String) As String
    Dim fso As Object
    Dim ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
    GetBoiler = ts.readall
    ts.Close
End Function

Function GetSignature(Mailbox As String) As String
   Dim Signature As String
   Dim SigStringPL As String
   Dim SigStringUK As String
    SigStringPL = Environ("appdata") & _
                  "\Microsoft\Signatures\Poland.htm"

    SigStringUK = Environ("appdata") & _
                  "\Microsoft\Signatures\United Kingdom.htm"

    If Mailbox = "[email protected]" Then
        If Dir(SigStringPL) <> "" Then
            GetSignature = GetBoiler(SigStringPL)
        Else
            GetSignature = ""
        End If
    Else
        If Dir(SigStringUK) <> "" Then
            GetSignature = GetBoiler(SigStringUK)
        Else
            GetSignature = ""
        End If
    End If
End Function


Sub Mail_Outlook_With_Signature_Plain()
' Don't forget to copy the function GetBoiler in the module.
' Working in Office 2000-2010
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String

    Dim Signature As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strbody = "content"    


    Signature = GetSignature("[email protected]")
    MsgBox (OutMail.SentOnBehalfOfName)

        On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .subject = "This is the Subject line"
        .HTMLBody = strbody & "<br><br>" & Signature
        'You can add files also like this
        '.Attachments.Add ("C:\test.txt")
        .Display
    End With


    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

Regards, erni

问候,厄尼

回答by ksp585

SenderName is the field name for 'From' field.

SenderName 是“发件人”字段的字段名称。

回答by niton

From is SentOnBehalfOfName. In normal use, it is empty until the item has been sent.

From 是 SentOnBehalfOfName。在正常使用中,它是空的,直到物品被发送。

Do not choose the From manually.

不要手动选择从。

Sub replySentOnBehalf()

Dim objMsg As mailitem

Set objMsg = ActiveInspector.currentItem.reply
objMsg.SentOnBehalfOfName = "[email protected]"

' Now that objMsg.SentOnBehalfOfName is available run your code

objMsg.Display

Set objMsg = Nothing

End Sub