【问题标题】:Import Outlook message to Textbox/Richtextbox using Drag&Drop使用拖放将 Outlook 消息导入文本框/富文本框
【发布时间】:2017-10-31 20:49:50
【问题描述】:

我正在尝试将 Outlook 消息导入我的 vb.net 表单以填充文本框/富文本框。我使用了 Eric Moreau 的一些代码来处理导入功能。问题是代码会导入消息并将其保存到临时文件夹中。我的问题是我需要一个没有任何节省的解决方案。相反,它应该填充一个富文本框字段,然后我将使用该富文本框将其保存到应用程序的 my.settings 中。我似乎无法弄清楚要更改什么才能将行为从保存更改为实际填充我的字段。 代码如下(原代码全部归功于 Eric Moreau)

Option Strict On

Public Class MailDnD
Dim objOL As New Microsoft.Office.Interop.Outlook.Application

Private Sub me_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    lblFile.Text = String.Empty
    Try

        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            'supports a drop of a file from Windows Explorer

            ' copy the name of the dragged files into a string array
            Dim draggedFiles As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())

            'handle each file passed as needed
            For Each fileName As String In draggedFiles
                'hardcode a destination path for testing
                Dim strDestinationFile As String = _
                            IO.Path.Combine(My.Settings.TempFolder.ToString, _
                                            IO.Path.GetFileName(fileName))
                'test if source and destination are the same
                If strDestinationFile.Trim.ToUpper = fileName.Trim.ToUpper Then
                    lblFile.Text += strDestinationFile + _
                                    " - E-post meddelandet är redan importerat!" + _
                                    Environment.NewLine
                Else
                    lblFile.Text += "Importerar - " + _
                                    strDestinationFile + Environment.NewLine
                    IO.File.Copy(fileName, strDestinationFile)
                End If
            Next

        ElseIf e.Data.GetDataPresent("FileGroupDescriptor") Then
            'supports a drop of a Outlook message

            'Dim objMI As Object - if you want to do late-binding
            Dim objMI As Microsoft.Office.Interop.Outlook.MailItem

            For Each objMI In objOL.ActiveExplorer.Selection()
                'hardcode a destination path for testing
                Dim strFile As String = _
                            IO.Path.Combine(My.Settings.TempFolder.ToString, _
                                            (objMI.Subject + ".msg").Replace(":", ""))
                lblFile.Text += strFile + Environment.NewLine
                objMI.SaveAs(strFile)
            Next
        End If
        lblFormat.Text = String.Empty

    Catch ex As Exception
        lblFile.Text = "Ett fel uppstod vid import, vänligen testa igen" + Environment.NewLine + ex.ToString
    End Try
End Sub

''' <summary>
''' Reset the status label
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub me_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DragLeave
    lblFormat.Text = String.Empty
End Sub

''' <summary>
''' Handle the DragOver event
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub me_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        'handle a file dragged from Windows explorer
        e.Effect = DragDropEffects.Copy
        lblFormat.Text = "Dra över e-post meddelandet"
    ElseIf e.Data.GetDataPresent("FileGroupDescriptor") Then
        'handle a message dragged from Outllok
        e.Effect = DragDropEffects.Copy
        lblFormat.Text = "Dra över e-post meddelandet"
    Else
        'otherwise, do not handle
        e.Effect = DragDropEffects.None
        lblFormat.Text = ""
    End If
End Sub

只是为了澄清导入功能按预期工作。它将 Outlook 消息保存到文件夹,但我有点希望它不保存,而是将消息行导入到我的应用程序内的富文本框。如果您需要更多信息,请联系我

亲切的问候,

【问题讨论】:

  • Public Class MailDnD Dim objOL As New Microsoft.Office.Interop.Outlook.Application 应该是整个代码部分的一部分
  • 有人吗?到目前为止只有 12 次观看 :(
  • 如果我理解正确,您不想将消息保存到临时文件,而是将消息的内容读入RichTextBox?如果到目前为止我是正确的,让我问你这个问题:当用户同时丢弃多条消息时,你将如何处理?这是您真正想向用户显示的内容,还是只需将其直接保存到My.Settings(或两者都保存)?
  • 是的,我想将消息信息存储在富文本框中,然后将该富文本框保存到 my.settings。从那里我可以随时从 my.settings 中提取信息。多文件问题不会成为问题,因为这是我自己做的,我当时只会拉 1 个文件。
  • 问题是,当您直接从 Outlook 中拖动邮件时,您唯一的选择是将它们存储在一个临时文件中,因为似乎没有一种将 MailItem 存储为的好方法原始内存数据。

标签: vb.net outlook vb.net-2010


【解决方案1】:

调整您的代码以仅获取一项并不难。您基本上只需要删除循环并使其选择第一项即可。

我切换到DragEnter 事件而不是DragOver,因为前者仅在鼠标进入表单时才被提升,而后者会持续提升直到对象被放下或鼠标离开表单。鼠标悬停在表单上时,放置数据无法更改,因此您不需要一直检查它。

我还冒昧地更正了一些“särskrivningar”:),重命名了一些变量以便更好地理解,并进行了调整,以防止您在一次。

我已经对大部分代码进行了注释,但如果您有任何问题或不清楚的地方,请告诉我!

Dim Outlook As New Microsoft.Office.Interop.Outlook.Application

''' <summary>
''' Custom method called by the DragDrop event when a mail is dropped onto the application. 
''' Handles the updating of the User Interface.
''' </summary>
''' <param name="Mail">The mail dropped onto the application.</param>
''' <remarks></remarks>
Private Sub OnMailDropped(ByVal Mail As Microsoft.Office.Interop.Outlook.MailItem)
    SenderTextBox.Text = Mail.SenderEmailAddress
    SubjectTextBox.Text = Mail.Subject
    BodyRichTextBox.Text = Mail.Body
End Sub

Private Sub MailDnD_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    Try
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then 'Supports the drop of a file from Windows Explorer.

            'Copy the names of the dragged files into a string array.
            Dim DraggedFiles As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())

            'Check that only one file is selected.
            If DraggedFiles.Length = 0 Then
                lblFile.Text = "Inget e-postmeddelande valt!"
                Return 'Do not continue.

            ElseIf DraggedFiles.Length > 1 Then
                lblFile.Text = "Du kan endast importera ett e-postmeddelande i taget!"
                Return 'Do not continue.

            End If

            'Get the file path of the dragged file.
            Dim FileName As String = DraggedFiles(0) 'Regular arrays are zero-based, which means the very first item has index 0.

            'Load the file into a MailItem.
            Dim Mail As Microsoft.Office.Interop.Outlook.MailItem = _
                CType(Outlook.Session.OpenSharedItem(FileName), Microsoft.Office.Interop.Outlook.MailItem)

            'Update the status label.
            lblFile.Text = "Importerade: " & FileName

            'Invoke our custom method.
            OnMailDropped(Mail)

        ElseIf e.Data.GetDataPresent("FileGroupDescriptor") Then 'Supports the drop of a Outlook message.

            'Check that only one mail is selected.
            If Outlook.ActiveExplorer().Selection.Count = 0 Then
                lblFile.Text = "Inget e-postmeddelande markerat!"
                Return 'Do not continue.

            ElseIf Outlook.ActiveExplorer().Selection.Count > 1 Then
                lblFile.Text = "Du kan endast importera ett e-postmeddelande i taget!"
                Return 'Do not continue.

            End If

            'Get the selected mail.
            Dim Mail As Microsoft.Office.Interop.Outlook.MailItem = _
                CType(Outlook.ActiveExplorer().Selection(1), Microsoft.Office.Interop.Outlook.MailItem)
            'In Office applications the collections are one-based, thus we do ".Selection(1)" for the first item instead of ".Selection(0)".

            'Update the status label.
            lblFile.Text = "Importerade: " & Mail.Subject

            'Invoke our custom method.
            OnMailDropped(Mail)

        End If
    Catch ex As Exception
        lblFile.Text = "Ett fel uppstod vid import, vänligen testa igen" + Environment.NewLine + ex.ToString
    End Try
End Sub

Private Sub MailDnD_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) _
        AndAlso CType(e.Data.GetData(DataFormats.FileDrop), String()).Length = 1 Then 'Allow only one file at a time.

        'Handle a file dragged from Windows explorer
        e.Effect = DragDropEffects.Copy
        lblFormat.Text = "Dra över e-postmeddelandet"

    ElseIf e.Data.GetDataPresent("FileGroupDescriptor") _
        AndAlso Outlook.ActiveExplorer().Selection.Count = 1 Then 'Allow only one mail at a time.

        'Handle a message dragged from Outlook
        e.Effect = DragDropEffects.Copy
        lblFormat.Text = "Dra över e-postmeddelandet"

    Else
        'Otherwise, do not handle
        e.Effect = DragDropEffects.None
        lblFormat.Text = ""
    End If
End Sub

代码注意事项:

  • 每次将有效的电子邮件项/文件放到表单上时,都会调用自定义 OnMailDropped() 方法。

  • SenderTextBox 是显示发件人电子邮件地址的文本框。

  • SubjectTextBox 是显示电子邮件主题的文本框。

  • BodyRichTextBox 是显示电子邮件正文的文本框。

您可能也注意到了,我将字符串与和号 (&amp;) 连接,而不是加号 (+)。这是因为在 VB.NET 中 &amp; 是本机字符串连接运算符。在某些情况下,使用 + 可能会导致问题。
请参阅The difference between + and & for joining strings in VB.NET 了解更多信息。

希望这会有所帮助!

【讨论】:

  • 哇,这是一些扎实的工作!它完全按照我想要的方式工作。非常感谢!我检查了你的个人资料和你的网站顺便说一句,很高兴看到那里有更多的 Doom 粉丝! :)
  • @OliverPersson:很高兴我能帮上忙! -- 哈哈,是的……我丑陋的网站。自从我在 2012 年创建它以来,它看起来就是这样。当时我对 HTML 或 Javascript一无所知 ;)。从那以后,我学到了很多东西,而且我实际上正在开发一个新版本的网站。
  • 顺便说一句,现在当我让您在线时,您是否知道如何为用户执行的每个操作创建一个新的 my.setting ?例如,每次用户单击一个按钮时,它都会创建 my.settings.Ärende+1 例如?因此,如果用户单击该按钮 3 次,它将创建 my.settings.Ärende1/2/3 吗?我不需要完整的代码示例,只需要关于如何编写它的语法
  • @OliverPersson :不幸的是,设置是静态的,无法在运行时更改。但是,您可以通过将设置类型更改为System.Collections.Specialized.StringCollection 来使设置保存字符串列表,如下图所示:i.stack.imgur.com/zJgKN.png
  • @OliverPersson :要初始化列表,您必须将其复制粘贴到设置的Value 中:&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /&gt;
猜你喜欢
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-18
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
相关资源
最近更新 更多