VB form to fill Excel Template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobT
    New Member
    • Aug 2010
    • 14

    VB form to fill Excel Template

    My project is moving along smoothly- I've gotten a lot of info from this forum. One thread that helped me understand what I was doing is here. But I have a question I can't find an answer to.

    My form fields will populate an Excel workbook. No problems there. My question is how do I call it to enter the data into an Excel template I have already created?

    The location of the template sits in my C drive- C:\DailyLogs\Da ilyLog.xlsx

    Here's my code so far:

    Code:
            ' Here we go. All the data in the form to an Excel sheet. Cross your fingers...
            Dim oExcel As Object
            Dim oBook As Object
            Dim oSheet As Object
    
            'Start a new workbook in Excel- this isn't really what I need though, I need it to open a template and populate it.
            oExcel = CreateObject("Excel.Application")
            oBook = oExcel.Workbooks.Add
    
    
            'Add data to cells of the first worksheet in the new workbook
            oSheet = oBook.Worksheets(1)
            oSheet.Range("A4").Value = Start1.Text
            oSheet.Range("B4").Value = End1.Text
            oSheet.Range("C4").Value = Time1.Text
            oSheet.Range("D4").Value = CallType1.Text
            oSheet.Range("E4").Value = Description1.Text
            'etc....
    
    
            'Save the Workbook and Quit Excel
            oBook.SaveAs("C:\DailyLogs\DailyLog.xlsx")
            oExcel.Quit()
    Any ideas?

    Thanks,

    Rob
  • RobT
    New Member
    • Aug 2010
    • 14

    #2
    I got it!

    Change line 10 of code to:

    Code:
    oBook = oExcel.Workbooks.Open("C:\DailyLogs\DailyLog.xlsx")
    That worked perfectly.

    I just thought I'd share my findings with others.

    Comment

    Working...