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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write data to an Excel Template?

Status
Not open for further replies.

CVL1975

Programmer
Aug 26, 2002
2
US
Hello Everyone,

I am trying to write data to an existing Excel template using VB 6.0 as front end but I have trouble of putting data in the cells. For example
I have template call (TEST.xsl) and all the layout already built and I want to insert data to a certain cell but I seem can not get it to work. So I hope someone could help me on this.

Thanks much in advance.
 
I have a program that will import a table to Excel but haven't tried to go to a particular cell. If you want it I can e-mail it to you. Or try to put all of it here. It isn't all that big.
 
Could someone explain to me how this is done? I am really at a loss for sending data to an excel file.
 
Oh yeah, and does the file already have to exist or can you create a new file to place it in within your program. I want to send data to a file so that I can store it for future use. Is it possible to do this without having to have the user save the file. As in it stores the data without the user of the program viewing.
 
There is a good demo on MSDN:


There have also been a few extended examples here if you find a specific difficulty - come back if you need more.


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
i do something like this, i hope it helps you:

Code:
Dim oExcelApp   As Excel.Application
Dim oWs         As Excel.Worksheet
Dim oWb         As Excel.Workbook
Dim path As String

'get the name of the new file (the one that will be created)
CommonDialog1.Filter = "Archivos Excel|*.xls"
CommonDialog1.ShowSave
path = CommonDialog1.FileName

If path = "" Then
    MsgBox "wrong file name"
    Exit Sub
End If


Set oExcelApp = CreateObject("EXCEL.APPLICATION")
oExcelApp.Visible = True
'open the template
oExcelApp.Workbooks.Open FileName:=App.path & "\template.xls", ReadOnly:=True, ignoreReadOnlyRecommended:=True
Set oWs = oExcelApp.ActiveSheet
Set oWb = oExcelApp.ActiveWorkbook



With oWs
    .cells(5, 1)="Hello"
    
   'copy contets of a flexgrid into the excel sheet
Dim n As Integer
      
      n = 8   'The row of the cell i want to start
   
        For i = 1 To Grid.Rows - 2
            .Cells(n, 1).Value = Grid.TextMatrix(i, 1)
            .Cells(n, 2).Value = "'" & Grid.TextMatrix(i, 2)
            .Cells(n, 3).Value = Grid.TextMatrix(i, 3)
            .Cells(n, 4).Value = Grid.TextMatrix(i, 4)
            .Cells(n, 5).Value = Grid.TextMatrix(i, 5)
            n = n + 1
        Next

      'save as the new file
    .SaveAs path
        
End With

Set oExcelApp = Nothing
Set oWs = Nothing
Set oWb = Nothing

i hope it helps You..

Eli
 
Thanks so much johnwn and elibb. Went to both and I completely understand now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top