# Using the SaveAs in Excel not working with Ruby

**URL:** <https://www.ruby-forum.com/t/using-the-saveas-in-excel-not-working-with-ruby/79142>\
**Category:** Ruby\
**Created:** [December 12, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/using-the-saveas-in-excel-not-working-with-ruby/79142 "2006-12-12T15:02:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/using-the-saveas-in-excel-not-working-with-ruby/79142/1 "2006-12-12T15:02:08Z")

</div>

Hi;  
I need to save an Excel Spreadsheet with Ruby. I used the Save AND  
SaveAs methods, but each time I get prompted “Do you want to save the  
changes you made…” How can I save the workbook without this prompt.  
In addition, is there away to add a timestamp to the save file  
like…application12122006.xls or any format that contains a time  
stamp?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2006, 3:30pm UTC](https://www.ruby-forum.com/t/using-the-saveas-in-excel-not-working-with-ruby/79142/2 "2006-12-12T15:30:17Z")

</div>

removed\_email\_address@domain.invalid wrote:

> How can I save the workbook without this prompt.

Where xl is your Excel Application object and wb is your Workbook  
object…

xl.DisplayAlerts = 0  
wb.SaveAs(xlsname)  
xl.DisplayAlerts = 1

> In addition, is there away to add a timestamp to the save file  
> like…application12122006.xls or any format that contains a time  
> stamp?

Something like this…

xlsname = ‘application’ + Time.now.strftime("%m%d%y") + ‘.xls’

Mully

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/using-the-saveas-in-excel-not-working-with-ruby/79142/3 "2006-12-12T15:40:18Z")

</div>

Thanks…that did the trick!

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/using-the-saveas-in-excel-not-working-with-ruby/79142/4 "2006-12-12T16:34:37Z")

</div>

I am guessing the (0) in close(0) refers to the number of the book that  
is open, which could mean that if you had a book open and you open a new  
book, then this will fail.

try close(“SomeFile.xls”) and let me know if that works. I know from the  
VBA that you could refer to the book by using the filename as the  
reference.  
the all open workbooks lie in a collection hence the need for a  
reference.

it’s been a long time since I have done any VBA, which is a good thing!  
ivor

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/using-the-saveas-in-excel-not-working-with-ruby/79142/5 "2006-12-12T15:44:18Z")

</div>

removed\_email\_address@domain.invalid wrote:

> 

Hey

I think you can use

book.close

passing either 1 or 0 (I can’t remember which)  
Also, I can’t remember if you need to call SaveAs first.

This should work:

book = #get workbook  
book.SaveAs “SomeFile.xls”  
book.close(0)

Hope this helps  
Gustav P.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/using-the-saveas-in-excel-not-working-with-ruby/79142/6 "2006-12-12T20:01:51Z")

</div>

The (0) tells it to not save changes, letting you bypass the dialog that  
asks you to do so.

The object browser says:

Sub Close([SaveChanges], [Filename], [RouteWorkbook])

So I’m assuming 0 = false and 1 = true. Not sure about the  
[RouteWorkbook].

VBA isn’t so bad when you can do it in Ruby! 😉

Nate

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 12, 2006, 10:00pm UTC](https://www.ruby-forum.com/t/using-the-saveas-in-excel-not-working-with-ruby/79142/7 "2006-12-12T22:00:43Z")

</div>

Ok, this code provides the date…  
xlsname = ‘application’ + Time.now.strftime("%m%d%y") + ‘.xls’  
If i want to add a time stamp to? is it ("%m%d%y%h%mm%s")???
