Monday, April 27, 2009

Edit Contents in Excel Sheet Programatically

Issue: To edit on autoshape added to a worksheet and change the text in it programatically.
Language: VB.net
Solutions: Firstly import below assembly.
Imports Microsoft.Office.Interop.Excel
Code to create objects of Excel Workbook.
Dim Wb As Object
Dim Ex As Excel.Application = New Excel.Application
Dim Ws1 As Excel.Worksheet
Code to Open existing Excel Worksheet:
Wb = Ex.Workbooks.Open()
Ws1 = Ex.Sheets()
Edit autoshape and add text to it:
Dim rect As Shape
Ws1.Shapes.SelectAll()
rect = Ws1.Shapes.Item(1)
rect.TextFrame.MarginTop = 200 -->Set margins of Autoshape
rect.TextFrame.MarginLeft = 80 -->Set margins of Autoshape
rect.TextFrame.Characters.Text = "Sample Text"