Opening excel spreadsheet from within a VBscript

D

Disco

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
 
C

chandlm

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
 
C

chandlm

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
 
D

disco

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
 
K

KJTFS

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
 
C

chandlm

Disco,

You can get round this by using

ActiveWorkbook.Close False
' closes the active workbook without saving any changes
instead of

activeworkbook.save


HT
 
A

Andy

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"
 
K

KJTFS

Andy said:
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


> > >
Don't forget to
Set xlObj = Nothing

at the end......

Keith
www.kjtfs.com
 
Joined
Jan 31, 2009
Messages
3
Reaction score
0
hi,

this code generally works, and gets the job done
however, when the process is complete, i get the error in the picture as attached

anyone pls help?




KJTFS said:
Andy wrote:
> 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
>
>
> > > >
> >
> > Don't forget to
> > Set xlObj = Nothing
> >
> > at the end......
> >
> > Keith
> > www.kjtfs.com



---
Message posted from http://www.ExcelForum.com/
 

Attachments

  • untitled.JPG
    untitled.JPG
    14.8 KB · Views: 2,103

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top