Tim5827

Do you know what is wrong with this code This same question was asked in another forum with no answer.

public void PasteValues()

{

Excel.Range r = (Excel.Range)Application.Selection;

r._PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, System.Type.Missing, System.Type.Missing);

}



Re: Visual Studio Tools for Office Excel Paste special

Dennis Wallentin

Tim,

Check out the following VB procedure from a workbook-centric module. It first copy data from a defined range source and then paste it to the selected range.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim p_Book As Excel.Workbook = Application.ActiveWorkbook

Dim p_Sheet As Excel.Worksheet = CType(p_Book.Worksheets(1), Excel.Worksheet)

Dim p_Source As Excel.Range = p_Sheet.Range("A1:A10")

p_Source.Copy()

Dim p_Target As Excel.Range = CType(Application.Selection, Excel.Range)

p_Target.PasteSpecial(Excel.XlPasteType.xlPasteValues)

End Sub

You can skip the 'XlPasteSpecialOperation.xlPasteSpecialOperationNone' in Your code. As the above code shows we need to have a range source from where we copy the wanted data from. Of course it's recommended to add structure error handling to assure that the selected type is of the range type etc.