none
Powershell and Excel CutCopyMode

    Question

  • Hi all,

    I hope somebody can help, the situation is the below:

    I have written a script that copies an Excel range to clipboard then pastes it to a new excel document.

    The problem that I'm facing is that I can't programatically clear / delete the clipboard. Every time I run the script I get the following message:

    There is a large amount of information on the Clipboard. Do you want to be able to paste this information into another program later? Then a Yes, No or Cancel box.

    I've tried the following:

    $Workbook.Application.CutCopyMode = $False
    $Workbook.Application.CutCopyMode = False
    $Workbook.Application.CutCopyMode = 0

    But everytime it states that this property isn't settable.

    Looking round the web you see the vb alternative i.e.

    Selection.Copy
    Application.CutCopyMode = False (This clears the clipboard)

    Hope someone can help.

    Dave

    samedi 3 mars 2012 00:29

Réponses

  • Try adding this:

    $xl.DisplayAlerts = $False

    • Marqué comme réponse Dafalk samedi 3 mars 2012 19:26
    samedi 3 mars 2012 01:46
  • To Clear clipboard you can use following command:

    [System.Windows.Forms.Clipboard]::Clear()

    Hope this helps...!!!

    Thanks & Regards
    Bhavik Solanki

    Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.

    • Marqué comme réponse Dafalk samedi 3 mars 2012 19:26
    samedi 3 mars 2012 02:35
  • Thanks for the replies....

    So I've added both comments in and it works as expected.

    To add the system.windows.form.clipboard I had to add the following:

    # Load System.Windows.Forms assembly.
    $null = [Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

    Then run the script in STA mode.

    powershell -noprofile -sta -command .\DoStuff.ps1

    • Marqué comme réponse Dafalk samedi 3 mars 2012 19:26
    samedi 3 mars 2012 19:26

Toutes les réponses

  • Try adding this:

    $xl.DisplayAlerts = $False

    • Marqué comme réponse Dafalk samedi 3 mars 2012 19:26
    samedi 3 mars 2012 01:46
  • To Clear clipboard you can use following command:

    [System.Windows.Forms.Clipboard]::Clear()

    Hope this helps...!!!

    Thanks & Regards
    Bhavik Solanki

    Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.

    • Marqué comme réponse Dafalk samedi 3 mars 2012 19:26
    samedi 3 mars 2012 02:35
  • Thanks for the replies....

    So I've added both comments in and it works as expected.

    To add the system.windows.form.clipboard I had to add the following:

    # Load System.Windows.Forms assembly.
    $null = [Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

    Then run the script in STA mode.

    powershell -noprofile -sta -command .\DoStuff.ps1

    • Marqué comme réponse Dafalk samedi 3 mars 2012 19:26
    samedi 3 mars 2012 19:26