简体   繁体   中英

VBA - Select text in Outlook email body

Below is an image:

在此处输入图片说明

Currently, I have the following code:

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

I believe that the key-line is

    Set weditor = OutApp.ActiveInspector.wordEditor

because it enables me (I think) to manipulate the body as if it were in Word. I just can't get it to search and select the text I want to be selected (such as stackoverflow in the image above).

To get the currently open item in your Outlook, all you need is:

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

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