我无法将附件直接从 Outlook 复制并粘贴到 vb.net 应用程序

问题描述 投票:0回答:1

我的代码有一个“问题”,我有一个按钮,其功能是将用户剪贴板上的文档粘贴到列表中,问题是当我尝试直接复制并粘贴 Outlook 附件时,但在我的应用程序显示剪贴板中未检测到任何内容的消息,但我可以将该文件粘贴到文件资源管理器中的任何位置。

有没有办法通过代码来粘贴Outlook附件?

非常感谢!

这是我的职责:

Private Sub btnPegar_Click(sender As Object, e As EventArgs) Handles btnPegar.Click
    ' Get clipboard data
    If Clipboard.ContainsData(DataFormats.FileDrop) Then

        Dim fileDropList As String() = CType(Clipboard.GetData(DataFormats.FileDrop), String())

        ' Variable to track whether at least one valid file has been found
        Dim archivoValidoEncontrado As Boolean = False

        ' Iterate over the list of files and do what you need with each one
        For Each filePath As String In fileDropList

            ' Get file extension
            Dim extensionActual As String = Path.GetExtension(filePath)

            ' Check if the file extension is one of those allowed
            If extensionActual.Equals(".PDF", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".TIF", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".JPG", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".DOC", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".DOCX", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".XLS", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".XLSX", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".BMP", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".PNG", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".GIF", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".DOCUMSG", StringComparison.OrdinalIgnoreCase) OrElse
               extensionActual.Equals(".MSG", StringComparison.OrdinalIgnoreCase) Then

                'The file has an allowed extension
                archivoValidoEncontrado = True

                Try
                    ' Check if the file has already been added previously
                    If archivosAgregados.Contains(filePath) Then                        
                        MessageBox.Show($"The file '{Path.GetFileName(filePath)}' has already been added above.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Else
                        ' Add the file to the list of added files
                        archivosAgregados.Add(filePath)

                        ' Get file information
                        Dim fileInfo As New System.IO.FileInfo(filePath)

                        ' Get the file size in KB
                        Dim fileSizeBytes As Long = fileInfo.Length
                        Dim fileSizeKB As Double = fileSizeBytes / 1024
                        Dim roundedFileSizeKB As Double = Math.Round(fileSizeKB, 2)

                        ' Add the file information to the ListView
                        Dim LI As ListViewItem = Me.lstFiles.Items.Add(filePath)
                        LI.StateImageIndex = 0
                        LI.SubItems.Add(roundedFileSizeKB.ToString() & " KB")
                    End If
                Catch ex As Exception
                    MessageBox.Show($"Error checking file '{Path.GetFileName(filePath)}': {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        Next

        If lstFiles.Items.Count > 0 Then

            bProcesar.Enabled = True
            bEliminar.Enabled = True
        Else
            bProcesar.Enabled = False
            bEliminar.Enabled = False
        End If

        'All files do not match extension
        If Not archivoValidoEncontrado Then
            MsgBox("Extensions not allowed on the clipboard.")
        End If
        'There is nothing on the clipboard
    Else
        MsgBox("The clipboard does not contain files.")
    End If
End Sub
vb.net outlook copy clipboard
1个回答
0
投票

您的代码假设有一个真实的文件链接复制到剪贴板。 Outlook 附件的情况并非如此 - 附件不存在于本地文件系统中,仅存在于 Outlook 邮件存储中。

您需要处理

FileGroupDescriptor
/
FileContents
剪贴板格式。

© www.soinside.com 2019 - 2024. All rights reserved.