excel VBA从excel替换Outlook OFT文件中的文本

时间:2013-02-26 18:41:24

标签: excel vba outlook-2010 outlook-vba

在excel中我有三列:

A:        B:               C:
UserName  UsermailAddress  RecordToBeDeleted

我正在使用excel中的以下代码从Outlook(使用OFT文件)发送和发送电子邮件到excel电子表格B列中的每个电子邮件地址。

我发送的代码就发送到B列中的每个电子邮件地址

我遇到的问题是,在电子邮件消息(OFT文件)中,我有两个关键字(UserNameRecordToBeDeleted),我需要在每个与该邮件地址一起发送的电子邮件中替换这些关键字。

代码在OFT文件中找到两个关键字,但只是删除它们或用空条目替换它们。

我做错了什么,以便我可以替换OFT邮件中excel文件中的单词。

我在excel表中的VBA是:

Sub RunEmails()
    Dim i As Integer
    Dim rngCell As Range

    For Each rngCell In Range("A2", "A" & CStr(Cells(Rows.Count, "A").End(xlUp).Row))
        Call DoTest(rngCell.Offset(0, 1).Value, "JDoe@abc.com", rngCell.Offset(0, 2).Value, rngCell.Value)
    Next rngCell

End Sub

Sub DoTest(EmailAddress As String, CCAddress As String, RecordToBeDeleted As String, UserName As String)
    Dim oApp As New Outlook.Application

    Dim olNewMail As Outlook.MailItem
    Const Template As String = "D:\Documents\list\list-mail.oft"

    Set olNewMail = oApp.CreateItemFromTemplate(Template)

    olNewMail.Recipients.Add EmailAddress
    olNewMail.HTMLBody = Replace(olNewMail.HTMLBody, ">UserName<", UserName)
    olNewMail.HTMLBody = Replace(olNewMail.HTMLBody, ">RecordToBeDeleted<", RecordToBeDeleted)
    olNewMail.VotingOptions = "Can distribution list be Deleted YES;Can distribution list be Deleted NO"

    olNewMail.Send
End Sub

1 个答案:

答案 0 :(得分:0)

UserName(和其他变量)中的RecordToBeDeletedDoTest的值似乎为空,因为您从主过程调用sub,但是传递了参数{{1} } - 或按引用,默认情况下这是。在您的情况下,这意味着您获得了所拥有的内容 - 因为它们未在ByRef中声明/分配,RunEmails值也是空的。在您的情况下使用ByRef参数:

ByVal

了解详情:Passing Arguments by Value or by Reference

希望这很有用。