Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste Special in C# vsto Excel

I am working on the C# vsto Excel application.

Whenever user pastes something in the excel template from another excel sheet,it also pastes Cell format along with the cell data in the excel template. I want to avoid this. So i googled & i came across term paste special.

Paste special will only paste the contents and will no alter the format of the current sheet.

I want to introduce paste special option in my vsto application.

I have code here,

   Application.OnKey("^v", "PasteSpecV");

but its not working... can any one help me with this ?

like image 255
Sangram Nandkhile Avatar asked Dec 29 '22 01:12

Sangram Nandkhile


2 Answers

  1. Download dll From http://globalmousekeyhook.codeplex.com/
  2. Add Reference MouseKeyboardActivityMonitor.dll

        private KeyboardHookListener k_keyListener;
    
        private void ThisWorkbook_Startup(object sender, System.EventArgs e)
        {
            k_keyListener = new KeyboardHookListener(new AppHooker());
            k_keyListener.Enabled = true;
            k_keyListener.KeyDown += new KeyEventHandler(k_keyListener_KeyDown);
        }
    
        void k_keyListener_KeyDown(object sender, KeyEventArgs e)
        {
            if (Control.ModifierKeys == Keys.Control)
                if (e.KeyCode == Keys.V)
                {
                    Worksheet actSht = ActiveSheet as Worksheet;
                    Range rng = actSht.Application.Selection as Range;
                    if (MessageBox.Show("You are about to paste values only. Do you want to continue?", "Paste Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        rng.PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
                    }
                    e.Handled = true;
                }
        }
    
like image 155
Tushar Chhabhaiya Avatar answered Jan 13 '23 21:01

Tushar Chhabhaiya


After lots of search and try and error methods,i finally managed to do "Paste Special". The sheet on which i am working , i have delacred as Static Worksheet in the class called commonData

class CommonData
    {
      public static Worksheet DATASHEET;

    }

after that, i used that worksheet in ThisWorkbook.cs

On the ThisWorkbook Start up, i replaced PASTE(^v) by VBA Function Paste_cell

    private void ThisWorkbook_Startup(object sender, System.EventArgs e)
    {
      // replacing paste by macro function Paste_cell
      CommonData.DATASHEET.Application.OnKey("^v", "Paste_cell"); 

    }

Open excel sheet on which you are working, Press ALT + F11 i.e. VBA Macros Editor.

Tools >> Macros >> Create new macro, It will create Module 1 in the Project Explorer, Paste the following code in the module1

Sub Paste_cell()

If MsgBox("You are about to Paste only Values and not the format, proceed?", vbQuestion + vbOKCancel, GSAPPNAME) = vbOK Then
On Error Resume Next

ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

End If

End Sub

Now,if you copy paste any value from any excel sheet,It will only paste the cell data and it will not paste its Format.It will prompt following message in order to alert user. So the original Format will not change.

Cheers, :-)

like image 33
Sangram Nandkhile Avatar answered Jan 13 '23 21:01

Sangram Nandkhile