Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Manipulation through VB

Status
Not open for further replies.

Oughter

Technical User
Oct 23, 2001
2
US
Hey Guys,

I have an Excel file embedded into my VB form. I want to change the value of cells in this Excel file at run time. How do I do this?

Thanks!
 
When you say you have embedded I assume you have it embedded as an OLE object.

The best way (or at least from an easy programming point of view) is to add a reference to Excel through project (that way you get to see most of the methods, properties etc of the objects)->references and select Excel Object Library.

Then include the following code:


Dim AppXL As Excel.Application, sFile As String, sPass As String
Dim AppXLWorkSheet As Excel.Worksheet
sFile = "c:\book1.xls" 'replace with your filename

Set AppXL = CreateObject("Excel.Application")
AppXL.Workbooks.Open FileName:=sFile, ReadOnly:=False, password:=sPass
AppXL.Application.DisplayAlerts = False
Set AppXLWorkSheet = AppXL.Workbooks(1).Worksheets(1)
AppXLWorkSheet.Cells(1, 1) = "Hello"
AppXL.Workbooks(1).Save
AppXL.Workbooks(1).Close
Set AppXL = Nothing

This will open a workbook called c:\book1.xls and set cell 1, 1 to be "Hello", if you need anything else please let me know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top