VBA - 用条件替换OFT模板中的文本

时间:2017-09-21 15:33:30

标签: vba excel-vba oft excel

我开始使用VBA。这次我正在创建一个简单的业务电子邮件自动化,可以使用OFT模板从excel发送。下面的代码非常适合替换文本,但它不能用于创建正确的条件。我需要添加一个条件,如果单元格为空,则不执行任何操作,如果包含某些内容,请添加" on" &安培;单元格中的文本。 "改变来源"部分代码显然是错误的。有人可以帮忙吗?

Sub Test()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim x As Variant
    Dim Sour As String


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

    On Error Resume Next

    x = Cells(1, 3)

        vTemplateBody = otlNewMail.HTMLBody

        strFind = "NAME1"
        strNew = Cells(x, 3)
        .HTMLBody = Replace(.HTMLBody, strFind, strNew)

'Change SOURCE
        vTemplateBody = otlNewMail.HTMLBody

        strFind = "SOURCE1"
        strNew = if Cells(x, 3) = "n/a" Then .Skip Else = " on " & Cells(x, 12)
        .HTMLBody = Replace(.HTMLBody, strFind, strNew)

1 个答案:

答案 0 :(得分:0)

fyi:这两个程序片段的运行完全相同

OutApp.CreateItem(0)
OutApp.CreateItem(1)
OutApp.CreateItem(2)
OutApp.CreateItem(3)
OutApp.CreateItem(4)
with OutApp
    .CreateItem(0)
    .CreateItem(1)
    .CreateItem(2)
    .CreateItem(3)
    .CreateItem(4)
end with