Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 18th, 2003, 03:54 PM
Registered User
 
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel automating problem

Hello
  I'm doing a project which uses the excel object. I have tried out that i can create a new excel application, get the sheet and make some change, then save it to my harddrive. I can find the new file with all the changes I made. My problem is that I have to open an exiting .xls file(because the format is required), and fill out it with new information, then save it as a new file. There is my code
Dim oExcel, oBook, oSheet As Object
        Const sSampleFolder = "F:\ExcelFile\"

        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Open(sSampleFolder & "accounting.xls")
        oSheet = oBook.Worksheets(1)

        oSheet.Range("D9").Value = "The Graduate School"
        oSheet.Range("D10").Value = "Lawson Accounting Unit"
        oSheet.Range("D11").Value = "Setup & Upload Spreadsheet"

        Dim tic, filename As String
        tic = DateTime.Now.Ticks.ToString()
        filename = "acctsetup" + tic + ".xls"

        oBook.SaveAs(sSampleFolder & filename)

        oSheet = Nothing
        oBook = Nothing
        oExcel.Quit()
        oExcel = Nothing
        GC.Collect()

My problem here is that I can create the new excel file and save it with the same format(some of the fields are pre-filled), but all the changes I made are nowhere to be found
Can somebody be so kind and tell me what's wrong with my code?
Thanks in advance
Tony

 
Old September 18th, 2003, 11:56 PM
Registered User
 
Join Date: Aug 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Untested ... but try
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
' if not on a separate line excel assumes it is a variant
        Const sSampleFolder = "F:\ExcelFile\"

set oExcel = CreateObject("Excel.Application")
set oBook = oExcel.Workbooks.Open(sSampleFolder & "accounting.xls")
set oSheet = oBook.Worksheets(1)

set oSheet.Range("D9").Value = "The Graduate School"
set oSheet.Range("D10").Value = "Lawson Accounting Unit"
set oSheet.Range("D11").Value = "Setup & Upload Spreadsheet"

        Dim tic, filename As String
        tic = DateTime.Now.Ticks.ToString()
        filename = "acctsetup" + tic + ".xls"

        oBook.SaveAs(sSampleFolder & filename)

set oSheet = Nothing
set oBook = Nothing
set oExcel.Quit()
set oExcel = Nothing
        GC.Collect()


' the set command needs to be used liberally with objects. Refer to the help doco.


Hope it helps.

Kieran
 
Old September 19th, 2003, 03:41 AM
Registered User
 
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Kieran
 Untested ... but try
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
' if not on a separate line excel assumes it is a variant
        Const sSampleFolder = "F:\ExcelFile\"

set oExcel = CreateObject("Excel.Application")
set oBook = oExcel.Workbooks.Open(sSampleFolder & "accounting.xls")
set oSheet = oBook.Worksheets(1)

set oSheet.Range("D9").Value = "The Graduate School"
set oSheet.Range("D10").Value = "Lawson Accounting Unit"
set oSheet.Range("D11").Value = "Setup & Upload Spreadsheet"

        Dim tic, filename As String
        tic = DateTime.Now.Ticks.ToString()
        filename = "acctsetup" + tic + ".xls"

        oBook.SaveAs(sSampleFolder & filename)

set oSheet = Nothing
set oBook = Nothing
set oExcel.Quit()
set oExcel = Nothing
        GC.Collect()


' the set command needs to be used liberally with objects. Refer to the help doco.


Hope it helps.

Kieran
Thanks a lot

 
Old September 19th, 2003, 01:37 PM
Registered User
 
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Kieran
  My vs.net will not let me use "set", the error message says that "let" and "set" are no longer supported. Any idea? And one more question, why everytime I save a new .xls file, the system automatically make a copy of the file, and a copy of the new file under F:Thanks
Tony






Similar Threads
Thread Thread Starter Forum Replies Last Post
Automating Excel from Access Zaff VB.NET 2002/2003 Basics 6 May 11th, 2006 09:02 AM
javascript automating through excel... darkhalf Javascript 0 March 3rd, 2006 01:26 PM
Automating Assembly Trust Ron Howerton General .NET 1 October 20th, 2005 09:19 AM
Automating Outlook Task PC User Access VBA 0 March 6th, 2005 04:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.