简体   繁体   English

VBA Outlook 文本未添加到电子邮件正文

[英]VBA Outlook Text not added to email body

My working code will browse to all incoming emails moved to a specified folder.我的工作代码将浏览到所有移至指定文件夹的传入电子邮件。 Once it's in the folder, macro will run to check if that email is existing in my Excel History File.一旦它在文件夹中,宏将运行以检查该电子邮件是否存在于我的 Excel 历史文件中。

The code below is working in such a way that it returns me the value of the Agent's name.下面的代码以这样一种方式工作,它向我返回代理名称的值。 However, it doesnt append that agent's name in the email.但是,它不会在电子邮件中附加该代理的姓名。 Is this code correct for adding text in email body:此代码对于在电子邮件正文中添加文本是否正确:

emlBody = ActiveAgnt & vbCrLf & emlBody

If yes, then why does the Agent's name added in the email body?如果是,那么为什么在电子邮件正文中添加代理人的名字? Below is the rest of the code.下面是代码的其余部分。

Set objFolder = Session.GetDefaultFolder(olFolderInbox).Folders("For Processing")
Set objItems = objFolder.Items

For Each obj In objItems
        For lgCurrentRow = 2 To lgLastRow
            If obj.Class = olMail Then
                strSubj = myXLApp.Worksheets("Sheet1").Cells(lgCurrentRow, "C")
                StrSR = myXLApp.Worksheets("Sheet1").Cells(lgCurrentRow, "D")
                StrAgnt = myXLApp.Worksheets("Sheet1").Cells(lgCurrentRow, "E")
                emlSubj = obj.Subject
                emlBody = obj.Body

            '>>>>>Check if incoming email has an existing SR in History File<<<<<
                If emlSubj Like "*" & strSubj & "*" Then
                    Debug.Print strSubj
                    '*****If existing then check if there is an existing SR
                    '*****Append to email if SR is existing
                    If StrSR <> "" Then
                        Debug.Print StrSR
                    '*****If SR is not existing check if an agent is tagged to the email.
                    '*****If agent is tagged then append agent's name to email
                    ElseIf StrAgnt <> "" Then
                        Debug.Print StrAgnt
                    End If
                    Exit For
            '^^^^^Check if incoming email has an existing SR in History File^^^^^

            '>>>>>If Incoming file is not match Check Active Agents list to assign Agent<<<<<
                 ElseIf emlSubj <> "*" & strSubj & "*" Then
                        Call ActiveAgents(ActiveAgnt)
                        Debug.Print ActiveAgnt
                        emlBody = ActiveAgnt & vbCrLf & emlBody
            '^^^^^If Incoming file is not match Check Active Agents list to assign Agent^^^^^

                Exit For
                            Set obj = Nothing
                            Set objItems = Nothing
                            Set objFolder = Nothing
                            Set objOL = Nothing
                            Set obj.Body = Nothing
                End If

            End If

        Next

    Next

Instead of代替

emlBody = ActiveAgnt & vbCrLf & emlBody

try尝试

obj.Body = ActiveAgnt & vbCrLf & obj.Body

Here's the code that worked which incorporated @niton and @Doug Glancy suggestions.这是结合了@niton 和@Doug Glancy 建议的有效代码。

ElseIf emlSubj <> "*" & strSubj & "*" Then
                    Call ActiveAgents(ActiveAgnt)
                    Debug.Print ActiveAgnt
                    obj.Body = ActiveAgnt & vbCrLf & obj.Body
                    obj.Save

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

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