简体   繁体   English

使用 VBA 从 Excel 格式化电子邮件正文

[英]Formatting email body from Excel using VBA

I have found the code below from here https://stackoverflow.com/a/49207287/4539709我从这里找到了下面的代码https://stackoverflow.com/a/49207287/4539709

Option Explicit
Public Sub Example()
' add ref - tool -> references - > Microsoft Outlook XX.X Object Library
    Dim olApp As Outlook.Application
    Set olApp = New Outlook.Application

    Dim Email As Outlook.MailItem
    Set Email = olApp.CreateItem(0)

' add ref - tool -> references - > Microsoft Word XX.X Object Library
    Dim wdDoc As Word.Document '<=========
    Set wdDoc = Email.GetInspector.WordEditor

    Dim Sht As Excel.Worksheet
    Set Sht = ThisWorkbook.Worksheets("Sheet1")

    Dim rng As Range
    Set rng = Sht.Range("A4:H16").SpecialCells(xlCellTypeVisible)
        rng.Copy

    With Email
        .To = Sht.Range("C1")
        .Subject = Sht.Range("B1")
        .Display

         wdDoc.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
    End With

End Sub

I have come across an issue with the code in that after you send an email the rows remain selected as per attached.我遇到了代码问题,在您发送电子邮件后,行仍然按照附件选择。 Is there anyway to clear this有没有办法清除这个

Add the following line at the end Application.CutCopyMode = False在最后添加以下行Application.CutCopyMode = False

    With Email
        .To = Sht.Range("C1")
        .Subject = Sht.Range("B1")
        .Display

         wdDoc.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
    End With

    Application.CutCopyMode = False    '<---

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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