Anyone know where I mght find some code which shows me how to open up an excel template (xlt) in code, write values into a specific worksheet and then save it as an xls file?
Printable View
Anyone know where I mght find some code which shows me how to open up an excel template (xlt) in code, write values into a specific worksheet and then save it as an xls file?
Try the macro recorder......
Steve
Code:' I don't have a lot of experience in excel but I've
' handled littel jobbies like this
' I create a macro to do my excel work for me
' I then would call the file holding the macro
'
' of course if you need to enter information gathered
' on the fly throught your app and write it in this
' macro business is not what your are after
'
' then I would run this from my VB Application
'
'create and object (Excel SpreadSheet)
Dim oXL As Object
Set oXL = CreateObject("Excel.Application")
' Open the workbook that contains the macro to run.
oXL.Workbooks.open "C:\Folder\MyFile.xls"
'
'as object opens invisible, make visible if needed, if not omit
'the line oXL.visible=true
'
oXL.Visible = True
'
' Run the macro.
oXL.Application.Run "MyFileName.xls!MyMacroName"
'
' Quit Microsoft Excel.
oXL.Quit
'
' Free the object from memory.
Set oXL = Nothing
'
One problem with using a macro imbedded within Excel is that it gives the standard, pain-in-the-butt, dialog box about Virii. If you're using Excel in hidden mode or if you're opening and closing a lot of workbooks, it becomes a nuisance real fast. The best thing to do, in this instance is move the macro into your VB code, making sure you set the appropriate objects when you modify the macro to make it work with VB.