Solved

Activating a currently open workbook in vbs

Posted on 2008-06-23
5
2,856 Views
Last Modified: 2008-06-24
This shouldn't be a difficult question but I am trying to automate an autosaving excel 2007 workbook. This workbook is open 24/7 btw. I am going to create a vb script that does this and just put it in the Task Scheduler but I am having an issue with the script. The code below is what I have so far (it works) but I don't want to 'open' the workbook everytime. I just want to activate it. Thanks in advance!
Set args = WScript.Arguments

Set objExcel = CreateObject("Excel.Application")

objExcel.DisplayAlerts = False

Set objWorkbook = objExcel.Workbooks.Open("C:\test.xls")

objExcel.AlertBeforeOverwriting = False

objWorkbook.Activate()
 

objExcel.ActiveWorkbook.SaveAs 

objExcel.ActiveWorkbook.Close

objExcel.quit
 

WScript.Echo "Finished."

WScript.Quit

Open in new window

0
Comment
Question by:no1leftypitcher
  • 3
5 Comments
 
LVL 13

Assisted Solution

by:nike_golf
nike_golf earned 150 total points
ID: 21849835
In that case, assuming Excel is already open you would getobject instead of createobject... remember to relase the objects when finished.

NG,

Set args = WScript.Arguments
Set objExcel = GetObject("Excel.Application")
objExcel.DisplayAlerts = False
Set objWorkbook = Activeworkbook
objExcel.AlertBeforeOverwriting = False
objWorkbook.Activate()
...

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 200 total points
ID: 21850761
Hi, with the combination of GetObject, if Excel was already open, you could iterate through the open windows to see if your workbook is already open.

Regards,

Rob.
Set args = WScript.Arguments

On Error Resume Next

Set objExcel = GetObject("Excel.Application")

Err.Clear

On Error GoTo 0

If objExcel Is Nothing Then Set objExcel = CreateObject("Excel.Application")

objExcel.DisplayAlerts = False

For Each objWB In objExcel.Workbooks

	If objWB.Name = "Test.xls" Then

		Set objWorkbook = objWB

	End If

Next

If objWorkbook = Nothing Then Set objWorkbook = objExcel.Workbooks.Open("C:\Test.xls")

objExcel.AlertBeforeOverwriting = False

objWorkbook.Activate()

objWorkbook.SaveAs 

objWorkbook.Close

objExcel.Quit

WScript.Echo "Finished."

WScript.Quit

Open in new window

0
 
LVL 24

Assisted Solution

by:purplepomegranite
purplepomegranite earned 150 total points
ID: 21851161
But instead of a script to do this... As it is one file you want to save, why not add a macro to that one workbook that is called at intervals to save itself?

Adding the following code to a module in the workbook itself will automatically start a timer when it opens and then save itself every five minutes.
Private Sub Workbook_Open()

   ' Set timer to call save macro in five minutes

   Application.OnTime Now TimeValue ("00:05:00"), "SaveWorkbook"

End Sub
 

Sub showAlert()

  ThisWorkbook.Save

  Application.OnTime Now TimeValue ("00:05:00"), "SaveWorkbook"

End Sub

Open in new window

0
 
LVL 24

Assisted Solution

by:purplepomegranite
purplepomegranite earned 150 total points
ID: 21851167
At least... it would had I got the sub name right... attached code is correct!
Private Sub Workbook_Open()

   ' Set timer to call save macro in five minutes

   Application.OnTime Now TimeValue ("00:05:00"), "SaveWorkbook"

End Sub

 

Sub SaveWorkbook()

  ThisWorkbook.Save

  Application.OnTime Now TimeValue ("00:05:00"), "SaveWorkbook"

End Sub

Open in new window

0
 
LVL 24

Assisted Solution

by:purplepomegranite
purplepomegranite earned 150 total points
ID: 21851294
I was possibly premature with my code again... The Workbook_Open sub needs to go in the ThisWorkbook module, not in a standard module.  The SaveWorkbook goes in a standard module.  Additionally, it will want to be cancelled when the workbook is closed (otherwise it'll be opened again when the timer is hit i.e. every five minutes!).

The attached code will do this (code goes in module as stated).  The advantage I think of doing it this way is that if you want to run the spreadsheet on another computer, it will automatically still save at the required interval - you won't need to set up schedules or anything like that.
Module ThisWorkbook

-------------------
 

Private Sub Workbook_Open()

   ' Set timer to call save macro in five minutes

   SetTimer true

End Sub
 

Private Sub Workbook_BeforeClose(Cancel As Boolean)

   SetTimer false

End Sub
 

Standard module (e.g. Module1)

------------------------------
 

public sub SaveWorkbook

   ThisWorkbook.Save

   SetTimer true

end sub
 

public sub SetTimer(blnActivate as boolean)

   static dtNextRun as date

   if blnActivate then

      dtNextRun=Now + TimeValue("00:05:00")

      Application.OnTime dtNextRun, "SaveWorkbook"

   else

      if dtNextRun<>CDate(0) then Application.OnTime dtNextRun, "SaveWorkbook",,false

      dtNextRun=CDate(0)

   end if

end sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now