2017-02-15 28 views
0

VBAを使用してOutlookのメッセージを作成しようとしていますが、テキストの文字列、いくつかのテーブル(RangetoHTML関数を使用)、画像、メール署名。電子メール本文に署名とクリップボードイメージを追加するVBA

私はコマンドを使用して、すなわち、(より多くの場合より、それは正しく画像が表示されない)画像を使用してファイルを保存し、添付ファイルを通してそれを追加することを避けたい:

img src= 'img_name'.jpg

私はそれは、次のコードを使用して行うことができるが、これまでのところ、私は画像をテキストの後、署名の前に配置することができません期待していた。

Sub Mail_Selection_Range_Outlook_Body() 

    'Variables 
    Dim r1 As Range 
    Dim r2 As Range 
    Dim s As String 
    Dim wordDoc As Word.Document 
    Dim OutApp As Object 
    Dim OutMail As Object 

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

    'Get the text that will go on the mail body 
    s = ActiveWorkbook.Sheets("Plan2").Range("A1") 
    Set r1 = Sheets("Plan1").Range("A1:D4") 

    With OutMail 
     Set wordDoc = OutMail.GetInspector.WordEditor 

     .To = "[email protected]" 
     .Subject = "test" 
     .HTMLbody = s & RangetoHTML(r1) & .HTMLbody 

     'Set the range that will be pasted as an image 
     Set r2 = Sheets("Plan1").Range("A5:D9") 
     r2.CopyPicture Format:=xlPicture 
     OutMail.Display 

     'Set the position to paste the image 
     wordDoc.Application.Selection.Start = currentPosition 
     wordDoc.Application.Selection.End = wordDoc.Application.Selection.Start 

     'Paste the image 
     wordDoc.Application.Selection.Paste 
     .Close olSave 

    End With 

    Set OutMail = Nothing 
    Set OutApp = Nothing 

End Sub 
  1. を私が使用している場合コマンドwordDoc.Application.Selection.Start = currentPositionイメージがテキストの前に表示されます。
  2. の直前にwordDoc.Application.Selection.Start = currentPositionの直前にコマンドを置いても同じです。
  3. wordDoc.Application.Selection.Start = Len(.Body)を使用すると、画像は署名後に配置されます!

ありがとうございます。

+0

「OutMail.Display」の前に 'Application.Paragraphs.Add()。Paste'のようなものがあると分かりません。(署名が表示されたら追加されていると思います) – Slai

+0

画像を追加しないと電子メールの添付ファイルとして電子メールには画像は含まれず、画像は表示されません(電子メールの一部ではないため)。 **唯一の**その他の方法は、画像をサーバにアップロードすることです(受信者が利用可能になります)。画像を電子メールに添付するには、以下の解決策/コードを使用してください:http://stackoverflow.com/questions/32223633/vba-excel-how-can-i-send-an-image-sived-in-excel-on-電子メールでのハードドライブ - 32224724#32224724 – Ralph

+0

(上記の解決策では)Outlookは、画像が電子メール本文に表示されているため、添付ファイルが添付ファイルとして表示されないことに注意してください。しかし、これは単なる見通しのようなものを示す方法です。他の電子メールクライアントは画像を(正しく)添付ファイルとして表示します。そうしないと、画像がメール本文に表示されませんでした。 VBAコードを読んで、画像が実際にメールに添付されることにも気づくでしょう。それでも、Outlookは写真をそのように表示しません(理由は問わない)。 – Ralph

答えて

0

私は実際にちょうど私のコードにいくつかの調整を行うことによって、回避策を見つけることができた:

Sub Mail_Selection_Range_Outlook_Body() 

'Variables 
Dim r1 As Range 
Dim r2 As Range 
Dim s1 As String 
Dim s2 As String 
Dim wordDoc As Word.Document 
Dim OutApp As Object 
Dim OutMail As Object 

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

'Get the text that will go on the mail body 
s1 = ActiveWorkbook.Sheets("Plan2").Range("A1") 
s2 = ActiveWorkbook.Sheets("Plan2").Range("A2") 
Set r1 = Sheets("Plan1").Range("A1:D4") 

With OutMail 
    Set wordDoc = OutMail.GetInspector.WordEditor 

    .To = "[email protected]" 
    .Subject = "test" 
    .HTMLbody = s1 & RangetoHTML(r1) & .HTMLbody 

    'Set the range that will be pasted as an image 
    Set r2 = Sheets("Plan1").Range("A5:J22") 
    r2.CopyPicture Format:=xlPicture 
    OutMail.Display 

    'Set the position to paste the image 
    wordDoc.Application.Selection.Start = currentPosition 
    wordDoc.Application.Selection.End = wordDoc.Application.Selection.Start 

    'Paste the image 
    wordDoc.Application.Selection.Paste 

    .HTMLbody = s2 & .HTMLbody 

    .Close olSave 

End With 

Set OutMail = Nothing 
Set OutApp = Nothing 

End Sub 

私はまだ添付ファイルを介して画像を貼り付ける避けることができるよ。この方法で。

注:ピクチャの後に続くテキストを新しい行に配置するために、参照セル(この場合は "Plan2"の "A1")内のテキストの前に"<br>"を追加するだけです。

関連する問題