2017-05-29 7 views
0

の中に、私は以下のような簡単なExcelのスプレッドシートがあります。VBA IF-条件はHTML-ボディEメール

A  B 
1 10 
2 20 

をそして私は電子メールを送信するために、次のVBAを使用:

Sub Test_EMail4() 
    If ExitAll = False Then 
     Dim OApp As Object, OMail As Object, signature As String 
     Set OApp = CreateObject("Outlook.Application") 
     Set OMail = OApp.CreateItem(0) 
      With OMail 
      .Display 
      End With 
      signature = OMail.HTMLbody 
      With OMail 
      .To = "[email protected]" 
      .Subject = "test" 
      .HTMLbody = "<p> Permant Content goes here </p>" 
      If Sheet1.Range("A1").Value = 10 Then 
      .HTMLbody = "<p> Content if Formula is true </p>" 
      Else 
      End If 
      End With 
     Set OMail = Nothing 
     Set OApp = Nothing 
    Else 
    End If 
End Sub 

ご覧のとおり、HTML-BodyにIf条件があります。 最初のタグ<p> Permanet content goes here </p>は電子メールに常に表示され、secontタグ<p> Content if Formula is true </p>はIF式の条件が(この場合)と一致する場合にのみ表示されます。

今は、電子メールのIF式内のコンテンツのみを表示します。どのように永久的な部分も含めることができますか?

答えて

0

ただ、このようなWith OMailブロックの外でメッセージを構築する:あなたは、これはそれを解決して置くものから、

Sub Test_EMail4() 

    Dim strBody As String 
    Dim OApp As Object, OMail As Object, signature As String 

    strBody = "<p> Permant Content goes here </p>" 
    If Sheet1.Range("A1").Value = 10 Then 
     strBody = strBody & "<p> Content if Formula is true </p>" 
    End If 

    If ExitAll = False Then 
     Set OApp = CreateObject("Outlook.Application") 
     Set OMail = OApp.CreateItem(0) 
     With OMail 
      .display 
     End With 
     signature = OMail.HTMLBody 
     With OMail 
      .To = "[email protected]" 
      .Subject = "test" 
      .HTMLBody = strBody 
     End With 
     Set OMail = Nothing 
     Set OApp = Nothing 
    Else 
    End If 

End Sub 
+0

こんにちはロビン、私は私のVBAで2つのコードをコピーするとき何かを考慮する必要がありますか? VBAに入力すると、自分の電子メールにはコンテンツが表示されません。 – Michi

+0

私の編集を参照してください - 助けが必要です - 元のコードで提案された提案を 'Sub'全体に入れます –

+0

こんにちはRobin、私はあなたのコードを置くと" Object required(424) "というエラーが出ます。私はSub()... End Sub()を最初のコードの周りに置こうとしましたが、それでも動作しません。私のミスはどこにあるのか知っていますか? – Michi

0

Dim strBody As String 

strBody = "<p> Permant Content goes here </p>" 
If Sheet1.Range("A1").Value = 10 Then 
    strBody = strBody & "<p> Content if Formula is true </p>" 
End If 

その後は、あなたの現在のコードあたりHTMLbodystrBodyを設定します。そのイモが必要ないので、Signatureを削除しました。

Sub Test_EMail4() 
If ExitAll = False Then 
    Dim OApp As Object, OMail As Object 
    Set OApp = CreateObject("Outlook.Application") 
    Set OMail = OApp.CreateItem(0) 

     With OMail 
     .Display 
     .To = "[email protected]" 
     .Subject = "test" 
     If Sheet1.Range("A1").Value = 10 Then 
     .HTMLBody = "<p> Permant Content goes here </p>" & .HTMLBody 
     Else 
     .HTMLBody = "<p> Content if Formula is true </p>" & .HTMLBody 
     End If 
     End With 
     With OMail 
     .Display 'change to "send" 
     End With 

    Set OMail = Nothing 
    Set OApp = Nothing 
Else 
End If 
End Sub