![]() |
Opening excel spreadsheet from within a VBscript
I need to open an excel spreadsheet from within a
VBscript and then run a macro within that spreadsheet before closing it back down again. Can anyone provide some code that would do this. Thanks |
Opening excel spreadsheet from within a VBscript
DISCO.
Try Set xl = CreateObject("Excel.application") xl.Application.Workbooks.Open "yourworkbookpath\yourworkbook.xls" xl.Application.Visible = True xl.Application.run "'yourworkbookname.xls'!macronametorun" Set xl = Nothing This should do exactly what you need. HTH --- Message posted from http://www.ExcelForum.com/ |
Opening excel spreadsheet from within a VBscript
Just an extra note to the last post.
Make sure that you add the code to close the workbook and application in the excel sheet macro so that the focus is then returned to the rest of your vbscript. I normally use ActiveWorkbook.Save Application.Quit HTH --- Message posted from http://www.ExcelForum.com/ |
Opening excel spreadsheet from within a VBscript
Hi,
This code helps but I now have the problem that the workbook I am closing is asking if I would like to save the changes that have been made by the macro. The answer is no but I'm not sure how to put this into the code. Do you know how i can do this ?? Thanks -----Original Message----- Just an extra note to the last post. Make sure that you add the code to close the workbook and application in the excel sheet macro so that the focus is then returned to the rest of your vbscript. I normally use ActiveWorkbook.Save Application.Quit HTH --- Message posted from http://www.ExcelForum.com/ . |
Opening excel spreadsheet from within a VBscript
you can use the
application.displayalerts = false that will make excel use defualts instead of ask you what you want to do. If you do not want it to save the sheet then do xl.close false Hope that helps. Keith www.kjtfs.com --- Message posted from http://www.ExcelForum.com/ |
Opening excel spreadsheet from within a VBscript
Disco,
You can get round this by using ActiveWorkbook.Close False ' closes the active workbook without saving any changes instead of activeworkbook.save HT -- Message posted from http://www.ExcelForum.com |
Opening excel spreadsheet from within a VBscript
I assume you really mean VBScript and not VBA, so here goes;
Here is a sample VBScript. Save it in a standard text file with a .VBS extention. This one creates an Excel object, opens a workbook, sets a value, runs a VBA routine, then tides up and closes down. Set xlObj = CreateObject("Excel.application") xlObj.Workbooks.Open "C:\Byg\textfile\TextFileReformat.xls" xlObj.Range("NoOfCharsPerLine").Value = 50 xlObj.Run "ReadFromTextFile" xlObj.ActiveWorkbook.Saved = True xlObj.ActiveWindow.Close xlObj.Quit -- Regards Andy Wiggins www.BygSoftware.com Home of "Save and BackUp", "The Excel Auditor" and "Byg Tools for VBA" "Disco" wrote in message ... I need to open an excel spreadsheet from within a VBscript and then run a macro within that spreadsheet before closing it back down again. Can anyone provide some code that would do this. Thanks |
Opening excel spreadsheet from within a VBscript
Andy wrote:
[b]I assume you really mean VBScript and not VBA, so here goes; Here is a sample VBScript. Save it in a standard text file with a .VBS extention. This one creates an Excel object, opens a workbook, sets a value, runs a VBA routine, then tides up and closes down. Set xlObj = CreateObject("Excel.application") xlObj.Workbooks.Open "C:\Byg\textfile\TextFileReformat.xls" xlObj.Range("NoOfCharsPerLine").Value = 50 xlObj.Run "ReadFromTextFile" xlObj.ActiveWorkbook.Saved = True xlObj.ActiveWindow.Close xlObj.Quit [b] Don't forget to Set xlObj = Nothing at the end...... Keith www.kjtfs.com --- Message posted from http://www.ExcelForum.com/ |
All times are GMT +1. The time now is 08:25 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com