[VBA] How to Add Line Breaks to Outlook Email Body

目次

Environment

  • Windows Specification: Windows 10 Pro
  • Version: 20H2

Background

I tried sending an email using VBA, but the line breaks in the body did not work properly, and the text was output as a single continuous line. Therefore, I learned how to add line breaks to the email body.

Using “vbCrLf”

It cannot be done with just “&”

Here is the code that did not work. (Only the email body part is shown).

objMail.Body = .Range("D6").Value & _
                    .Range("D7").Value & _
                    .Range("D8").Value & _
                    .Range("D9").Value 'メール本文

I thought that since I was changing cells, simply connecting them with & would create line breaks, but it did not work.

Let’s use “vbCrLf”

Using vbCrLf worked successfully.

objMail.Body = .Range("D6").Value & vbCrLf & _
                    .Range("D7").Value & vbCrLf & _
                    .Range("D8").Value & vbCrLf & vbCrLf & _
                    .Range("D9").Value 'メール本文

Summary

I was able to solve it like this. In my case, the cell values change every time, so I adopted this method. However, if the body text does not change, you can also add line breaks directly inside the cell using “Alt + Enter”.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次