VBA - 在Outlook电子邮件正文中选择文本

时间:2014-08-25 09:12:46

标签: excel vba excel-vba

下面是一张图片:

enter image description here

目前,我有以下代码:

Sub Mail()
    Dim wb As Workbook, sh As Worksheet
    Set wb = Workbooks("book1"): Set sh = wb.Sheets("Sheet1")
    Dim OutApp As Object
    Dim OutMail As Object
    Dim msgbody As String

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

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

    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "subjsect"
        .Body = "somerandomtexthererhejoiehtjejheirjgoejrgijewr+goehjpogerhgeirog stackoverflow"
        .Display 
    End With
    Set weditor = OutApp.ActiveInspector.wordEditor
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

我相信关键线是

    Set weditor = OutApp.ActiveInspector.wordEditor

因为它使我(我认为)能像操纵Word一样操纵身体。我只是无法搜索并选择我想要选择的文本(例如上图中的stackoverflow)。

1 个答案:

答案 0 :(得分:0)

要获取Outlook中当前打开的项目,您只需要:

Dim OutApp As Object
Dim OutMail As Object
Dim msgbody As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.ActiveInspector.CurrentItem
msgbody = OutMail.Body
' if you want to edit the body - OutMail.Body = "BOB" for example!
' search and editing code goes here...

Set OutMail = Nothing
Set OutApp = Nothing