Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA从邮件正文中选择文本_Vba_Email_Outlook_Copy - Fatal编程技术网

VBA从邮件正文中选择文本

VBA从邮件正文中选择文本,vba,email,outlook,copy,Vba,Email,Outlook,Copy,我想复制电子邮件的整个正文。 我使用了这个代码(我在某处找到并修改了我自己),并且它经常工作。 问题是邮件窗口不是每次=>代码从活动的winwow中选择其他内容时都被激活。 我怎样才能把它做好 我尝试了AppActivate(“Microsoft Outlook”),但它不起作用 Private Sub copymail() Dim objOutlook As Outlook.Application Dim objInspector As Outlook.Inspector

我想复制电子邮件的整个正文。 我使用了这个代码(我在某处找到并修改了我自己),并且它经常工作。 问题是邮件窗口不是每次=>代码从活动的winwow中选择其他内容时都被激活。 我怎样才能把它做好

我尝试了AppActivate(“Microsoft Outlook”),但它不起作用

Private Sub copymail()

    Dim objOutlook As Outlook.Application
    Dim objInspector As Outlook.Inspector

    Dim strDateTime As String

    ' Instantiate an Outlook Application object.
    Set objOutlook = CreateObject("Outlook.Application")

    ' The ActiveInspector is the currently open item.
    Set objExplorer = objOutlook.ActiveExplorer

    ' Check and see if anything is open.
    If Not objExplorer Is Nothing Then
        ' Get the current item.
        Dim arySelection As Object
        Set arySelection = objExplorer.Selection

        For x = 1 To arySelection.Count
            Dim m As MailItem
            Set m = arySelection.Item(x)
            m.Display
            'DoEvents
            'DoEvents
            Application.Wait (Now + #12:00:02 AM#)
            'AppActivate ("Microsoft Outlook")
            SendKeys ("^a^c")
            DoEvents
            Application.Wait (Now + #12:00:01 AM#)

        Next x

    Else
        ' Show error message with only the OK button.
        MsgBox "No explorer is open", vbOKOnly
    End If

    ' Set all objects equal to Nothing to destroy them and
    ' release the memory and resources they take.
    Set objOutlook = Nothing
    Set objExplorer = Nothing
End Sub

您可以通过MailItem.Body(纯文本)或MailItem.HTMLBody(HTML)属性访问邮件正文。无需使用SendKeys:

Sub getMailtext()

    Dim objOutlook As Outlook.Application
    Dim objInspector As Outlook.Inspector

    Dim strDateTime As String

    ' Instantiate an Outlook Application object.
    Set objOutlook = CreateObject("Outlook.Application")

    ' The ActiveInspector is the currently open item.
    Set objExplorer = objOutlook.ActiveExplorer

    ' Check and see if anything is open.
    If Not objExplorer Is Nothing Then
        ' Get the current item.
        Dim arySelection As Object
        Set arySelection = objExplorer.Selection

        For x = 1 To arySelection.Count
            Dim m As MailItem
            Set m = arySelection.Item(x)

            Debug.Print m.Body
            Debug.Print m.HTMLBody

        Next x

    Else
        ' Show error message with only the OK button.
        MsgBox "No explorer is open", vbOKOnly
    End If

    ' Set all objects equal to Nothing to destroy them and
    ' release the memory and resources they take.
    Set objOutlook = Nothing
    Set objExplorer = Nothing
End Sub

Outlook使用Word作为电子邮件编辑器。可以使用Word对象模型在消息体上进行操纵。Inspector类的WordEditor属性返回表示主体的文档类(来自Word对象模型)的实例。你可以阅读更多关于这种方法和所有可能的方法

你说的“它不起作用”是什么意思。你有什么错误吗?还是什么都没发生?通过按键来阅读电子邮件是一个糟糕的解决方案。通过打开每封邮件阅读电子邮件:我也不喜欢,但如何选择不带SendKeys的正文代码正在浏览当前打开的outlook文件夹中的所有项目,仅插入一封邮件。代码仅显示选定的电子邮件。问题是电子邮件窗口不是一直处于激活状态,因此sendkeys作用于其他窗口而不是电子邮件窗口。我不知道HTMLBody。但是如何将格式化的正文复制到Excel工作表中呢。如果我复制HTMLBody,我只会得到一个带有主体HTML代码的单元格。取而代之的是,我需要一张带有邮件格式文本的表格(带有表格、超链接等)。@user3254924请为此发布一个新问题。我们很乐意提供帮助。