【问题标题】:Outlook VBA Copy part (selection) of mailbody邮件正文的 Outlook VBA 复制部分(选择)
【发布时间】:2021-12-16 23:52:08
【问题描述】:

是否可以将您在邮件正文中选择的文本复制到变量中

例如

Sub cpytxt()
  Set txt = Selection
  'Do something
End Sub

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    来源:gmayor,来源/字体:http://www.vbaexpress.com/forum/showthread.php?52985-VBA-get-selected-text-from-Outlook-email-body-and-use-in-Excel

    Public Sub ShowTextSelected()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim olInsp As Object
    Dim wdDoc As Object
    Dim strText As String
    
        On Error Resume Next
        'Get Outlook if it's running
        Set OutApp = GetObject(, "Outlook.Application")
    
        'Outlook wasn't running, so cancel
        If Err <> 0 Then
            MsgBox "Outlook is not running so nothing can be selected!"
            GoTo lbl_Exit
        End If
        On Error GoTo 0
    
        Set OutMail = OutApp.ActiveExplorer.Selection.Item(1)
        With OutMail
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            strText = wdDoc.Application.Selection.Range.Text
        End With
        MsgBox strText
    lbl_Exit:
        Set OutMail = Nothing
        Set OutApp = Nothing
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Exit Sub
    End Sub
    

    【讨论】:

    • 正是我需要的,谢谢
    • 是否也可以抓取部分主题?我可以看到 olInsp 设置为 .Getinspector,但不特定于“body”,那么如何将其设置为“subject”?
    • 这个Getinspector,在主题中看不到选择,但是“OutApp.ActiveExplorer.Selection.Item(1)”是完整的主题,这很有用。
    • 是的,我知道。但是 Getinspector 具有 Selection 属性,所以我找不到解决方法。
    • 我没有找到解决方案,但是您可以使用 split、left、right、mid 和 len 内部函数来选择您需要的主题的单词。
    猜你喜欢
    • 2015-03-10
    • 2018-11-08
    • 2018-10-05
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    相关资源
    最近更新 更多