简体   繁体   中英

How to paste image from clipboard to Outlook email Body?

Here is my situation. I've made a Application Windows Form which is editing an Excel sheet depending on what the user is doing (buttons / toggle / text box / etc..).

Once the edtion is complete the new Excel file is generated.

My programm then selects a range of cells, and copies it. It goes to Clipboard.

I've done multiple tests (if(){}else{} / saving into a .jpg / etc..) and everything is checked true.

I don't want to ATTACH my image. I don't want to save it, even temporarily, then paste it in the body through the saved .jpg file. I "just" want to make a Ctrl+V, into my Outlook eMail Body.

here's how i get the image from my clipboard ("MessageBody" is declared at the top as a public string so that i can call it through different regions):

public void ReadData()
{
    Excel excel = new Excel(@"E:\c#\Project#XX\Resources\TEST.xlsx", 1);
    excel.CopyRange(0, 0, 32, 12);

    IDataObject iData = Clipboard.GetDataObject();

    if (iData.GetDataPresent(DataFormats.Bitmap))
    {
        Bitmap MessageBody = (iData.GetData(DataFormats.Bitmap, true) as Bitmap);
        //pbx.Image = Image;
        //image.Save(@"E:\c#\Project#XX\Resources\bitmap1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        //MessageBody = image;
    }
    excel.Close();
    excel.Quit();
}

Here's my message building code :

private void flatCustButton013_Click(object sender, EventArgs e)
{
    ReadData();
    string MessageSubject = $"SomeSubject";
    Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
    newMail.Subject = MessageSubject;
    newMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
    MessageHTMLBody = "<html><body>SomeText.<img src="cid:MessageBody"</img></body></html>";
    newMail.HTMLBody = MessageHTMLBody;
    //newMail.Body = System.Windows.Input.ApplicationCommands.Paste;
    //newMail.Display(false);
    //newMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML; ;
    //newMail.HTMLBody = System.Windows.Input.ApplicationCommands.Paste;
    //newMail.Body = MessageBody;
    newMail.To = ToAddress;
    newMail.SentOnBehalfOfName = FromAddress;
    newMail.CC = CcAddress;
    newMail.BCC = BccAddress;

    //System.Windows.Input.ApplicationCommands.Paste;
    //wrdEdit = application._Inspector.WordEditor

    newMail.Send();
}

In the previous version, people had to open an Excel file, change the Cells manually, and then click a button (with a macro in VB). It would open a Outlook eMail item (display:true) with the image pasted already, then just needed to click "Send" and it was ok.

My 2.0 version is meant to automatise this by just click a "Send //Generated Excel Sheet//"Button.

Here's the macro code in VB :

Sub SendMail()

    Dim Messager As New Outlook.Application
    Dim Mail As Outlook.MailItem
    Dim WrdEdit

    Range("my_range").CopyPicture

    Set Messager = New Outlook.Application
    Set Mail = Messager.CreateItem(olMailItem)

    With Mail
        .To = "some folks"
        .CC = "some folks"
       '.BCC = ""
        .Subject = "SomeSubject"
        .Display
        .HTMLBody = Mess + .HTMLBody
    End With

    Set WrdEdit = Messager.ActiveInspector.WordEditor

    WrdEdit.Application.Selection.Paste

    Set WrdEdit = Nothing
    Set Messager = Nothing
    Set Mail = Nothing

ActiveWorkbook.Close SaveChanges:=False

End Sub

But i don't get why i would pass through the Inspector (which i have no knowledge about) if i'm succeeding in retrieving the clipboard image.

I found a way by adding an attachment from a local saved file to the mail and displaying it into the body with HTML.

Like so :

newMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

Outlook.Attachment attachment = newMail.Attachments.Add(@"path.imageformat", Outlook.OlAttachmentType.olEmbeddeditem, null, $"someTitle");

string imagecid = "whatever";

attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3613041E", imagecid);

newMail.HTMLBody = String.Format("<body><img src=\"cid:{0}\"></body>", imageCid);

the " http://schemas.microsoft.com/mapi/proptag/0x3712001E " code is here : http://www.outlookcode.com/codedetail.aspx?id=1915 .

And it works.

However, i really don't like having urls in my code, is there another way ?

And also, it looks like whatever i put into my 'imagecid' string nothing changes.

I'm trying hard to understand what i did with the code, and not just letting it work as it is.

Looks like i need some authorisation from Office to work through an application ? And especially when i want to paste image in a body (which can contain other stuff than just pixel data).

If i did not need this autorization with the VB script, i suppose, it is because i had to "physicly/humanly" press "Send Mail" button in Outlook ?

Are there any other possibilities ? I'm still using a file though to paste in my body, can't i just make a Ctrl+C Ctrl+V programmatically ? :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM