【发布时间】: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