Running VBScript file using another file - excel

I am trying to connect two different files to run by clicking on one. For instance, I want oven.xls excel file to open in the background when I click on test.rcp. Then when i click on test2.rcp, it closes oven.xls and opens oven2.xls. Is this possible using vbscript, or any other method for that matter? Any input would be appreciated. thanks.

I'm not sure what a "rcp" file is, but here are two vbs files that do what you need.
' test.vbs
With CreateObject("Excel.Application")
.Visible = True
.Workbooks.Open "oven.xls"
End With
' test2.vbs
Set e = GetObject(, "Excel.Application")
If Not e Is Nothing Then
e.Workbooks.Close
e.Workbooks.Open "oven2.xls"
End If

Related

VBS to Open an Excel Workbook Not Displaying

I am trying to run a small VBS script to open an Excel workbook, but every time I run my script, Excel will only load as a background application (confirmed in Task Manager) but will not display. I feel like I am missing something very silly, but I have browsed form after form talking about VBS scripts launching Excel, and I can't seem to get any of them to work. Any input you might have would be greatly appreciated, thanks :)
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open("FilePath", 0, True)
Application.Caption = "OnMax"
*Note I replaced the actual file path with just Filepath to make sure it fit and one line and no one had to scroll, but incase it matters the file path is: M:\Projects\Onmax II\Documents\R&D\Standalone App\VB Script Test\Test.xlsm
The script is doing exactly what you asked it to do it started an instance of Excel.
You just didn't tell it yet to make the application visible.
Adding the following code should help
xlApp.Visible = true

Unknown runtime error when running macro

I needed a way to schedule automatically opening an Excel file, refresh the contents then save and close it.
I have done this before but I can no longer open the file as opening it causes the macro that refreshes then saves and closes the file to run.
I considered VBScript after trying several ways of doing it. I found this code on here.
Dim objExcel, objWorkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\...\Finances.xlsm")
objExcel.Visible = True
objExcel.Run "Refresh"
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
WScript.Quit
The VBScript code opens the Excel file and triggers the macro Refresh inside. The macro refreshes the data then saves and closes the file.
But I'm getting the following error despite the code seeming to run ok.
If you don't want anything to run automatically when you open the workbook, put the line
objExcel.EnableEvents = False
before you open the workbook. You could set it back to True later if you need to. You should still be able to run Refresh. Whether or not that will fix your unknown error, I don't know.
You are quitting the excel.application object without closing the objWorkbook open workbook. Saved or not, this is going to generate an error crashing out of the open workbook.

Open Excel and Place on top of windows using VBS

I am currently using the below script to open Excel. The script does as it suppose to but what i need it to do is place excel on top of all windows. I currently had a script within the excel project to place userforms ontop of all windows but when i get to a certain point within my project it causes excel to crash, so i had pinpointed it to the script within the excel project. here is the script i am using to open excel file:
Dim AppExcel
Set AppExcel = CreateObject("Excel.application")
AppExcel.Workbooks.Open "C:\Users\****\Desktop\ App Tool\App.xlsm"
AppExcel.Visible = False
Based on comment to minimize every open window and then opening Excel.
Set shell = wscript.CreateObject("Shell.Application")
Shell.MinimizeAll ''Comment me if you don't want to minimize everything
'Then rest of your code
Dim AppExcel
Set AppExcel = CreateObject("Excel.application")
AppExcel.Workbooks.Open "C:\Users\****\Desktop\ App Tool\App.xlsm"
AppExcel.Visible = True
'And in the end Release the Shell object
Set shell = Nothing

VB script unable to access file

I am trying to schedule a vba script. I am working through the company server and am wondering if I can not access this due to administrative restrictions? here is the code:
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("\\Computer\denfs1\data\1EpUSRgltry\Permitting - Wells & Sundries\WEST (CO-MT-ND)\Schedules & Status\Planning and Permiting Spread sheet(West) Permit and Planning spread sheet.xlsm", 0, True)
xlApp.Run "Click"
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Here is the error I am getting
What does the macro "Click" do? If it make changes, close it without saving (change to True if you want to save changes by the Click macro).
xlBook.Close False
You may also want to debug from Excel by putting xlApp.Visible = True before opening the .xlsm workbook.
There might be a hidden Excel.exe locking that same file from previous script execution (say you press Ctrl-C before the macro "Click" finishes). Close all visible Excel windows and ensure there are no Excel.exe running in task manager.
If you keep pressing Cancel when Excel asks you to save changes to it, it will stuck in background even the vbs did not pick up any error.
The problem was a syntax error with the name of the file... Make sure you have the whole string accurate :/

Unhide Excel Application Session

I have an Excel VBA method (I didn't write it) that runs and one of the first things it does is hide the Excel session Application.Visible = False.
However, when the method has finished, it does not unhide the Excel session so it remains open and listed in the Task Manager but is hidden and seemingly unusable.
Does anyone know, without have the VBE open (so one can access the Immediate Window and run Application.Visible = True), how to unhide this Excel session? At the moment, I'm simply having to kill the session using the Task Manager.
This isn't a massive deal but I'm just interested if anyone knows how to resurrect such a session.
Like I said, it's not a big deal but was just interested if anyone knew of shortcut key or anything to bring it back.
There is no shortcut as such that I am aware of but you can do this.
Open MS Word and paste this code in the VBA Editor. Close all open instances of Excel which are visible and then run and this code. This will make a hidden instance visible. Manually close the instance and repeat the process if there are more instances.
Option Explicit
Sub Sample()
Dim oXLApp As Object
'~~> Get an existing instance of an EXCEL application object
On Error Resume Next
Set oXLApp = GetObject(, "Excel.Application")
On Error GoTo 0
oXLApp.Visible = True
Set oXLApp = Nothing
End Sub
I am not deliberately using a loop as the hidden instance can have a workbook which you might like to save?
If you want you can convert the above code to a VB Script document which you can directly run from the desktop.
Unfortunately, I don't have the control to make the changes required.
What do you exactly mean? Is the VBA Password Protected? If no then my suggestion is still the same as earlier
This is a case of poor programming. Even if we give a code to close
all hidden Excel instances, that won't help you. Because next time you
run that macro, you will face the same problem again. Why not edit the
existing code and add Application.Visible = True at the end? Is the
VBA password protected? – Siddharth Rout 28 mins ago
A good solution!
Open up Word, assuming you have it, and open the VBA Editor there, then open the Immediate Window (Ctrl+G) and type:
Getobject(, "Excel.Application").Visible = true
and press enter.
I had a similar problem and solved it with code line reordering.
Look for a line like this ActiveWorkbook.Close that might be the reason you cannot unhide the session.
If you can find it, put Application.Visible = True just before it and voila.
as code:
sub runthis()
dim xl as object
set xl = new excel.application 'create session
xl.workbooks.open filename:= "«yourpath»" 'open wb in the new session
xl.visible=true 'this is what you need, show it up!
'rest of the code
end sub
No need for word macro at all.
Open up another excel workbook.
Hit Ctrl+F11 to go to the VBA editor and there yoy will see the running but hidden excel file on the left.
Search the code of the hidden application file for Application.Visible = False and comment it out. Save and restart the file.
Alternatively you can get back the application to show without closing if you type Application.Visible = True in the immediate window (Ctrl+G)

Resources