Cannot make active Excel window when it is minimized - excel

Part of my code is:
Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate("Book1 - Excel")
Now i have opened afew applications in desktop and when Excel is behind others this part makes its window active. But if i minimize excel and run this part, it does not restore it.
Any ideas?

Drop that approach altogether. SendKeys is a very, very poor way of automating things and should not be used at all. If you need to automate MS Office applications use the respective COM object:
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbooks.Add
...
wb.Close
xl.Quit

Try this. It works fine for me.
Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate("Book1 - Excel")
WScript.Sleep 500 'sleep is required here to make this work
objShell.SendKeys "% x" ' to maximize

Related

Turning Display Alerts off in PowerPoint from Excel

I am writing a macro that opens and closes a PowerPoint Presentation from Excel.
Now I have the issue that when I am trying to save the PowerPoint file I get a pop up Message Box. To take care of that issue I used:
Application.DisplayAlerts = False
However this works in some cases, as I loop through multiple Presentations but not in all. So I tried instead this:
pptPres.Application.DisplayAlerts = False
But this just caused the Display Alerts to always pop up.
So I tried to replicate that using
Application.DisplayAlerts = True
which did not work. So I am quite confused what I am doing wrong. I need to turn the DisplayAlerts off otherwise my macro is stopped.
The issue occurs as I am opening a PowerPoint with macros so a *.pptm file which on saving I am asked whether or not to save with macros.
This is my current code, maybe you can replicate the issue:
Dim pptPres As PowerPoint.Presentation
Set pptApp = CreateObject("powerpoint.Application")
Set pptPres = pptApp.Presentations.Open(strPfad & strDat, False, True, True)
pptPres.Application.DisplayAlerts = False
strFirma = "Test123"
pptPres.SaveAs strPfad + "\Berichte" & "\" & strFirma & ".pptx"
pptPres.Close
Any help is greatly appreciated.
Powerpoint's Application.DisplayAlerts is slightly different. It has two options: ppAlertsAll and ppAlertsNone.
Try pptApp.DisplayAlerts = ppAlertsNone. Note that you have a mix of early- and late-binding and probably should be consistent. If you go with late-binding, ppAlertsAll's corresponding value is 2, and ppAlertsNone's corresponding value is 1.
Thanks to #Matthieu Guindon for pointing out that since you're running this from Excel, Application refers to Excel.Application, which is not your instance of PowerPoint. Hence Application.DisplayAlerts will not affect PowerPoint's alert setting at all. You want to work with pptApp, the instance of PowerPoint.

Calling Workbooks.Open

This VBScript errs as "unknown runtime error", on the VBScript line that calls the application object's RUN method, which I'm quite sure I'm using correctly, syntactically.
With only one workbook open under the application object, I probably don't even need to prefix it with the Workbookname.
It appears the previous line (which sets the wb variable to the application object's Workbooks.Open method, does not open. Nor does it err, nor does it "eventually" open. The path is correct, the filename is correct, there is no On Error Resume Next in my VBScript.
How can the workbook not open and not error?
I hope I am wrong in thinking there is a "wait/pause" issue. Acting directly on the Excel object model, it seems like the VBScript code waiting until the excelapp object has opened the workbook should be a given?
dim excelapp, wb
set excelapp = createobject("excel.application")
excelapp.enableevents=true
excelapp.visible=true
set wb = excelapp.workbooks.open("C:\Users\John\Desktop\Scheduled Jobs from Isaac\Availability.xlsb")
'MSGBOX "SHOULD BE OPEN"
excelapp.windowstate = -4137 'value for constant xlMaximized acccording to msdn
excelapp.caption = "Running AVAILABILITY - Please WAIT..."
excelapp.run "Availability.xlsb!ChangeAndCopyFile"
final vbscript code i used, for better or worse:
dim excelapp, wb
'set excelapp = createobject("excel.application")
'excelapp.enableevents=true
'excelapp.visible=true
'set wb = excelapp.workbooks.open("C:\Users\John\Desktop\ScheduledJobsfromIsaac\Availability.xlsb")
'although I don't know why the above line is silently failing, i'm going to use an alternate method I read on S.O.:
set wb = GetObject("C:\Users\John\Desktop\ScheduledJobsfromIsaac\Availability.xlsb")
set excelapp = getobject(,"excel.application")
excelapp.enableevents=true
excelapp.visible=true
excelapp.windowstate = -4137 'value for constant xlMaximized acccording to msdn
excelapp.caption = "Running AVAILABILITY - Please WAIT..."
excelapp.run "Availability.xlsb!ChangeAndCopyFile"
wb.close(true)
excelapp.caption = "10 seconds to close - please WAIT..."
wscript.sleep 10000
excelapp.displayalerts=false
excelapp.quit
probably, it helps if you can describe where and how you execute your vbscript above? is this coded in a .vbs file and execute with wscript.exe or cscript.exe, or embedded in a HTML or HTA?
If this is scripted in HTML, what browser are you using to view that? Please take note that almost all modern browsers will just ignore the vbscript silently.

WinWrap CreateObject("Excel.Application") Multiple Instances

Not sure how many people know about WinWrap Basic language which supports the .Net framework. But that is where my problem is. Hoping someone out there know about the language.
I am using it to communicate with Excel 2010. Basically I am extracting data for a proprietary data format and spitting it out to Excel. The following execute perfectly when no additional instances of Microsoft Excel 2010 are active during script run.
xExcel = CreateObject("Excel.Application")
xBook = xExcel.Workbooks.Open(XLFilePath)
xSheet = xBook.Worksheets(“Sheet1”)
xExcel.Visible = False
xSheet.Cells(1,1).Value = "Study Name"
However, if an additional Excel 2010 instance is activated manually by the user during the following script run, script errors out.
xExcel = CreateObject("Excel.Application")
xBook = xExcel.Workbooks.Open(XLFilePath)
xSheet = xBook.Worksheets(“Sheet1”)
xExcel.Visible = False
Do
xSheet.Cells(1,1).Value = "Study Name"
Loop
The guys at WinWrap say that CreateObject() in WinWrap is a simple code that calls the CoCreateInstance API. I am sort of lost there. Can someone help me understand how create multiple Excel instances.
Edit
I get the Runtime 50290 Application Specific Error.
Have you tried GetObject?
Dim objExcel As Object
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
With objExcel
'do something
End With

How to run VBScript GetObject while the computer is locked?

For a few reasons discussed here I can't open an excel worksheet like this:
Set excel = CreateObject("Excel.Application")
Set workbook = excel.Workbooks.Open(file_path.xls)
excel.Visible = True
So I'm trying to open it like this:
CreateObject("WScript.Shell").Run "excel.exe"
WScript.Sleep 5000
CreateObject("WScript.Shell").Run "excel.exe"
WScript.Sleep 5000
Set excel = GetObject(,"Excel.Application")
Set workbook = excel.Workbooks.Open( excel_file_path, 3)
excel.Visible = True
This runs just fine when my computer isn't locked. And usually VBScripts can run even when a computer is locked. However, only when the computer is locked, at the line GetObject(,"Excel.Application") I get the following runtime error:
Error: ActiveX component can't create object: 'GetObject'
Code: 800A01AD
Source: Microsoft VBScript runtime error
I tried changing my Windows 7 settings to never lock after some inactivity, but my company's IT department has enforced that setting.
Is there a way to run this script from a locked screen, or is there an alternative you recommend?
Edit:
It has something to do with the way the excel file is being opened... If I open a new blank excel page than lock the screen, GetObject will work just fine getting that document, but if I open it using the shell then log out, it won't...
In the section that does not seem to work, add this line after setting visible = TRUE, and see if your calculations then work as expected:
CreateObject("WScript.Shell").AppActivate objExcel.Name
Not sure if it will work, but I am doing similar things in some code and this is what's there.
I must have copied or read about it somewhere (wish I could give credit to where).
FYI - I have a vbscript that opens Excel, starts a new workbook, reads a file in as text via a querytable object, then does some automated edits. The code that works for me is:
...
set objExcel = CreateObject("Excel.application")
' Commented out the following line, so the user can see something is happening.
' objExcel.application.screenupdating = false ' Don't update the screen while shucking and jiving is going on.
objExcel.visible = true
objExcel.application.cursor = 2 ' change cursor to the hourglass.
CreateObject("WScript.Shell").AppActivate objExcel.Name
' Create a new workbook and worksheet
objExcel.workbooks.add
objExcel.worksheets.add
etc
Try the code:
CreateObject("WScript.Shell").Run "excel.exe """ & excel_file_path & """"
WScript.Sleep 5000
CreateObject("WScript.Shell").Run "excel.exe"
WScript.Sleep 5000
Set excel = GetObject(,"Excel.Application")

How to automate the forced closure of excel, followed by a restart?

Occasionally excel freezes or crashes while performing some long running VBA code (runs 24hr/day) and I'm examining ways to handle the restart of excel programmatically. Any advice on best way to go about this would be appreciated.
One idea i have is to write a dotnet app that might be potentially triggered by, say, a windows logging error event such as 'Event Name: APPCRASH' and 'Faulting application EXCEL.EXE', but i have no idea if this is feasible/sensible way to go about it.
If it happens to be a sensible approach, i would envisage the app potentially performing the following tasks;
1) Auto close/cancel any windows error popup boxes such as "Do you want to send more information about the problem?", "Microsoft Excel has stopped working", or "Microsoft Excel is trying to recover your information."
2) Force close excel if still open (eg if hanging), as well as killing any excel.exe processes
3) Restart excel
Is is possible to write such an app in dotnet? What references/tags should i be looking for to find further information?
Thanks,
Yug
After some digging, I pieced together the following solution. The vbs code below will force taskkill any outstanding excel.exe processes, and then opens the stated excel file (from which vba code can be restarted automatically from a worksheet_open event).
1) Turn off Windows error reporting:
Start, search 'Problem Reports and Solutions', change settings, advanced settings, Turn off problem reporting
2) Disable auto recovery for excel:
Within the WB in question, click; file, options, save, disable file recovery for this WB only
3) Under windows event viewer, application logs, highlight the error (faulting excel app) , right click 'create a basic task', run application/script, and enter the chosen name of the file in 4).
4) Paste the following code into text file and save as a vbScript file (.vbs)
Option Explicit
KillProcesses
ExcelRestart
Sub ExcelRestart()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open("C:\...\test.xlsx")
xlApp.visible = True
xlBook.activate
Set xlBook = Nothing
Set xlApp = Nothing
End sub
Sub KillProcesses
On error resume next
Dim objWMIService, WshShell
Dim proc, procList
Dim strComputer, strCommand
strCommand = "taskkill /F /IM excel.exe"
strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\"& strComputer & "\root\cimv2")
Set procList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'excel.exe'")
For Each proc In procList
WshShell.run strCommand, 0, TRUE
Next
End sub

Resources