Visual Basic - 屏幕截图然后插入电子邮件

时间:2015-06-10 13:09:02

标签: vba excel-vba screenshot outlook-vba excel

我是使用Visual Basic的新手。我在MS Excel中使用Visual Basic。我正在努力解决必须将屏幕截图粘贴到电子邮件中。我有以下内容,请您告诉我需要输入的内容。

由于

    Sub AddToNaughtyList()
 '
    ' AddToNaughtyList Macro
    '
    Range("Y3:Z3").Select
    Application.CutCopyMode = False
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("U2:V2").Select
    Selection.Copy
    Range("Y3:Z3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("E2").Select

    Dim aOutlook As Object
    Dim aEmail As Object
    Dim StudentName As String
    Dim SendAddress As String

 ' copy picture

    Range("C6:S39").Select
    Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap

'setup email

    Set aOutlook = CreateObject("Outlook.Application")
    Set aEmail = aOutlook.CreateItem(0)
    StudentName = ActiveCell.Value
    SendAddress = Range("D5")


'Cells(1, "A").Value

'Set Address

    aEmail.To = SendAddress

'Set Subject

    aEmail.Subject = "Weekly Progress Report for " & StudentName

'Set Body for mail

    aEmail.HTMLBody = "<font face=calibri><html><body>Nat's *Student needs to attend Friday Detention* text goes here.</font><br /><br />" _

'paste into email???????????

aEmail.Display

1 个答案:

答案 0 :(得分:0)

我看到了两种可能的解决方案:

  1. Outlook使用Word来呈现/编辑邮件正文。因此,您可以使用Word对象模型粘贴剪贴板中的图像。 Inspector类的WordEditor属性从Word对象模型返回Document类的实例,该对象模型表示消息正文。 Selection类的PastePasteSpecial方法插入剪贴板的内容。有关详细信息,请参阅Chapter 17: Working with Item Bodies
  2. 将屏幕截图保存为磁盘上的文件,然后将其作为附件附加到邮件项目中。 Attachments类的Add方法在Attachments集合中创建一个新附件。然后你可以在邮件正文中提到它。请注意,您需要为附加图像指定cid属性以在正文中添加引用。
  3. 例如:

      Attachment attachment = newMail.Attachments.Add(@"E:\Pictures\image001.jpg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
    
      string imageCid = "image001.jpg@123";
    
      attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid);
    
     newMail.HTMLBody = String.Format("<body><img src=\"cid:{0}\"></body>", imageCid);