Paste Special in C# vsto Excel

这一生的挚爱 提交于 2019-12-03 16:19:09
  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;
                }
        }
    

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, :-)

I used the following code to copy-paste only values and formatting using PasteSpecial in VSTO 2010

            Excel.Worksheet lastSheet = Application.ActiveSheet;
            Excel.Workbook wb = Application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

            for (int i = this.Worksheets.Count; i >= 1; i--)
            {
                Excel.Worksheet source = this.Worksheets[i];
                source.Activate();
                Application.Cells.Select();
                Application.Selection.Copy();

                Excel.Worksheet sheet = wb.Worksheets.Add();
                sheet.Activate();
                Application.Selection.PasteSpecial(Excel.XlPasteType.xlPasteValues);
                Application.Selection.PasteSpecial(Excel.XlPasteType.xlPasteFormats);
                sheet.Name = source.Name;
                sheet.Range["A1"].Select();
                Clipboard.Clear();
            }
            lastSheet.Activate();
            lastSheet.Range["A1"].Select();

            wb.SaveAs(fileName);
            wb.Close();

The line Clipboard.Clear() is the same as VBA CutCopyMode = False, to deselect what was selected to copy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!