简体   繁体   English

Visual Studio PowerShell 将附件从 Outlook 拖放到列表框

[英]Visual Studio PowerShell Drag & Drop Attachments From Outlook to List Box

i have got the drag and drop working on my Windows Form.我已经在我的 Windows 窗体上进行了拖放操作。 i can drop items from my desktop or any folder but if i try to Drag an attachment straight from Outlook it won't do any thing.我可以从我的桌面或任何文件夹中删除项目,但如果我尝试直接从 Outlook 中拖动附件,它不会做任何事情。 do i need to add extra PowerShell commands int my current code我是否需要在当前代码中添加额外的 PowerShell 命令

######################################## This is For Drag And Drop 

  $listBox1_DragOver = [System.Windows.Forms.DragEventHandler]{
    if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) 
    {
        $_.Effect = 'Copy'
    }
    Else
    {
        $_.Effect = 'None'
    }
    }

    $listBox1_DragDrop = [System.Windows.Forms.DragEventHandler]{
        foreach ($filename in $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)) 
        {
            $listBox1.Items.Add($filename)
        }

        }

        ### Add events to form ###

    $listBox1.Add_DragOver($listBox1_DragOver)
    $listBox1.Add_DragDrop($listBox1_DragDrop)
    #$form.Add_FormClosed($form_FormClosed)

        #### Show form and return result ###
    $dialogResult = $Form12.ShowDialog()
    if ($dialogResult -eq [System.Windows.Forms.DialogResult]::OK)
     {
      $Form12.SuspendLayout()
       [array]$items =  $listbox1.Items| sort -CaseSensitive
       if ($items.Count -gt 1){
       $items
       }
       ELSE
       {
       [string]$items[0]
       }
      $Form12.Close() | out-null
     }

在此处输入图片说明

After lot's of research i came across the code below, to summarise what it does.经过大量研究,我发现了下面的代码,总结了它的作用。

it will let you drag and drop files out of Outlook, it will then copy and paste that into a folder which then gives you the path and file name.它将允许您将文件从 Outlook 中拖放,然后将其复制并粘贴到一个文件夹中,然后该文件夹为您提供路径和文件名。 it is pretty cool so if anyone else is stuck here is my working script and how i implemented it to my form这很酷,所以如果其他人被困在这里是我的工作脚本以及我如何将它实现到我的表单中

######################################## This is For Drag And Drop 
$Listbox1.AllowDrop = $true 
$Listbox1.Add_DragDrop({
    if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) { 
        foreach ($FileName in $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)) {
            Copy-Item -Path $FileName -Destination $textbox6.text -Force
            $Listbox1.Items.Add($FileName)

        }
    }
    else 
    {
        $Outlook = New-Object -ComObject Outlook.Application;
        $Selection = $Outlook.ActiveExplorer().Selection                                                                       
        foreach ($Item in $Selection) {
            foreach ($Attachment in $Item.Attachments) {
                Write-Verbose $Attachment.FileName
                $Name = Join-Path -Path $textbox6.text-ChildPath $Attachment.FileName
                $Attachment.SaveAsFile($Name)


            }         
        }

    }
})
$Listbox1.Add_DragEnter({$_.Effect = [Windows.Forms.DragDropEffects]::Copy})
$Form12.Controls.Add($Listbox1) 

# Activate the form     

[void] $Form12.ShowDialog()

i had to change my form a little by adding an Input Folder Button & Textbox6 to show the text of the folder that has been selected this is important as the Script above needs a directory to save the files to, please see code below.我不得不通过添加一个Input Folder Button & Textbox6来显示已选择的文件夹的文本来稍微更改我的表单,这很重要,因为上面的脚本需要一个目录来保存文件,请参阅下面的代码。

###################################### Get Folder Using Folder Browser and output text into textbox 
$button4_Click = {

$folderBrowserDialog3=New-Object System.Windows.Forms.FolderBrowserDialog
[void]$folderBrowserDialog3.ShowDialog()
$folderBrowserDialog3.SelectedPath
$textBox6.Text = $folderBrowserDialog3.SelectedPath
}

$button6_Click = {

$folderBrowserDialog1=New-Object System.Windows.Forms.FolderBrowserDialog
[void]$folderBrowserDialog1.ShowDialog()
$folderBrowserDialog1.SelectedPath
$textBox2.Text = $folderBrowserDialog1.SelectedPath
}

$button7_Click = {

$folderBrowserDialog2=New-Object System.Windows.Forms.FolderBrowserDialog
[void]$folderBrowserDialog2.ShowDialog()
$folderBrowserDialog2.SelectedPath
$textBox3.Text = $folderBrowserDialog2.SelectedPath
}

after i got that to work the thing that bugged me the most was i couldn't see the files in the listbox so i added a button to do exactly that please see code below在我得到它之后,最让我烦恼的是我看不到listbox的文件,所以我添加了一个button来做到这一点,请参阅下面的代码

###################################### Shows Files In ListBox 1
    $button5_Click = {
    #$textbox8.Text = ""
    $listBox1.Items.Clear()
    $items = Get-ChildItem $textbox6.Text
    ForEach($item in $items){
    $listBox1.Items.Add($item.FullName)
       }
    }

as you can see i have added $listbox1.Items.Clear() this will enable you to keep clicking the show files button without it duplicating the path and file in the listbox如您所见,我添加了$listbox1.Items.Clear()这将使您能够继续单击显示文件按钮,而不会复制listbox的路径和文件

the final result of my Form has come out great please see my image layout below, if anyone needs help to get it working on your own Form please comment and i will do my best to help.我的Form的最终结果非常好,请参阅下面的图像布局,如果有人需要帮助以使其在您自己的Form上工作,请发表评论,我会尽力提供帮助。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM