This is a bit tricky to explain, but let me try.
I have 6 UserForm in a VBA Project.
The execution of the macro will hide each form and show the next one in line.
In the first UserForm, on the click event of the Next button there is some code that modifies the currently opened Excel file, then hides the current UserForm, show the next one AND open saplogon.exe after a 5 second timeout.
It all works perfectly, BUT saplogon doesn't open until after i closed the 6th UserForm. Whereas i want it to open in the second, which is where the code to open the file is at.
I'm not really sure what i'm doing wrong. Following is the code i use in UserForm1.
Application.ScreenUpdating = False
'My Code
UserForm2.Show
Dim sFullPathToExecutable As String
sFullPathToExecutable = "C:\Program Files(x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
Application.Wait (Now + TimeValue("0:00:05"))
Shell sFullPathToExecutable
End Sub
Why isn't the EXE opening at UserForm2?
Thanks!
If by any chance someone else find themselves in a similar situation where you have to open a program in the background and show the next userform without losing focus on excel, Place the shell("location of the .exe") line into your next userform's Activate event:-
Private Sub UserForm_Activate()
Application.Wait (Now + TimeValue("0:00:02"))
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
End Sub
Related
Here the context : I'm working on a local workbook (c:) that directly opens a UserForm within the Workbook_Open() Sub. Then, when UserForm is closed, Excel is also automatically closed. Nothing special there.
The problem : Even with a correct setup in my Trust Center Settings (Disable all macros WITH notification), the warning security message pops up again each time I reload that workbook.
Here my code :
Private Sub Workbook_Open()
' modal display of my form
myUserForm.Show vbModal
' upon return from my form, save and close that workbook
Me.Close savechanges:=True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' also quit Excel app if no more workbook is opened
If Application.Workbooks.Count = 1 Then Application.Quit
End Sub
I'm pretty sure that's related to the fact that Excel is automatically closed by program, and not by user himself.
For example if I reduce the code to the strict minimum, like :
Private Sub Workbook_Open()
myUserForm.Show vbModal ' modal display of my form
End Sub
In that situation, so without any Close or Quit, I just fall into my main worksheet when the UserForm is closed, then I just manually close Excel, with or without saving, and that's it, warning popup will not appear next time I'll open that workbook.
I don't know if there is a solution without playing with Trust Locations or All macros enabled.
Any ideas ?
Thanks for your help
I am working on screenshotting a dialog box in my macro that is then printed onto the workbook to record the specific dialog inputs. I am running into an issue where I use this code:
Application.SendKeys "(%{1068})"
Application.Wait (Now() + TimeValue("00:00:05"))
DoEvents
Sheets("Summary Report").Range("B5").PasteSpecial
Application.CutCopyMode = True
and it's printing the entire screen, instead of just the dialog box that holds the inputs. This code is put in right after prompting the dialog box/window to appear, so I assumed that doing the
Application.SendKeys "(%{1068})" would function the same as alt+printscreen so only the box is copied and then pasted using:
Sheets("Summary Report").Range("B5").PasteSpecial
I see that SendKeys technically has a printscreen syntax, but it can't be used on individual applications according to the excel syntax page https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/sendkeys-statement
Any info or suggestions would be greatly appreciated!
Final workbook in a chain of 4 workbook's open and closing each other will not close via VBA macro when run in the chain (but works fine when run alone and the others close fine).
I tried all these codes (which all work, when I open just one file, but not when its the last workbook). No errors or dialogue boxes pop up, it seems to complete smoothly in that sense, however the window remains open. No close action done.
Windows("WorkbookName.xlsm").Close
Workbooks("WorkbookName.xlsm").Close SaveChanges:=False
If ActiveCell.Value = "Close this" Then
ThisWorkbook.Saved = False
ThisWorkbook.Close
End If
Call WorkbookClose
(tried all three above in a separate sub and called it)
The process for this is pretty much along these lines..
Workbook opens and auto runs a private sub which calls a macro in the workbook via "Run MacroName" which does like open a CSV, resave as XLSX.
Opens them via
Workbooks.Open Filename:= _ (directory) (This works)
Then the main workbook is opened and follows these steps
ThisWorkbook > Private Sub > Calls macro
The macro called, closes the last workbook via:
Windows("WorkbookName.xlsm").Close
'This works
Runs some simple code which opens another file, saves as XLSX, closes it.
Opens another file which does the same thing i.e. auto runs and closes previous workbook, runs a bit of code, saves, opens another workbook which closes the last. However when it reaches the fourth workbook (the last one) it stops working. The code does not close that workbook.
Also to mention if I add any code after these lines, they execute. Its as if Excel just skips the close commands without error and keeps going until the end of the code.
Windows("WorkbookName.xlsm").Close
Workbooks("WorkbookName.xlsm").Close SaveChanges:=False
If ActiveCell.Value = "Close this" Then
ThisWorkbook.Saved = False
ThisWorkbook.Close
End If
Call WorkbookClose
No error messages pop up.
I just want the final workbook to close.
I have an excel file that opens a messagebox when the file is opened . However, when I open the file, because of the messagebox, the file and its messagebox do not come to the front of my windows gain focus (they appear on my taskbar and I have to click on it from there). After the messagebox is completed, the file gains focus as normal.
In its simplest form, this code seems to trigger this
Sub Auto_Open()
MsgBox ""
End Sub
I have tried adding code in to give the window focus before executing the messagebox, but to no success. I don't mind this issue but other people will be using this document. Is there a solution to this?
The legacy Auto_Open auto-macro is going to run regardless of the state of Application.EnableEvents, which may be a problem if another macro-enabled workbook needs to open that workbook.
I can't reproduce the issue on Excel 2010, with this code in Module1:
Sub Auto_Open()
MsgBox "Auto_Open()"
End Sub
And this code in ThisWorkbook:
Private Sub Workbook_Open()
MsgBox "Workbook_Open()"
End Sub
I get two successive message boxes, first from Workbook_Open, then from Auto_Open, both show up right where I'd expect them - right in the middle of an empty worksheet:
Try putting the message box in the Open event handler of ThisWorkbook, see if it helps.
Hi I the following code in Excel VBA,
Sub A ()
Workbooks.open ("A.xls")
ActiveWorkbook.Worksheets("1").Select
ActiveSheet.CommandButton1.value = true
End Sub
I'm opening another workbook (the code inside is protected so I can't modify the workbook "B") and clicking a run button on the worksheet, and it returns a MsgBox with OK button.
I want to know how I can use VBA to close this MsgBox or clicking "OK" ...I tried to use,
`application.DisplayAlert = false`
before opening the workbook "B" but this does not work..
Thanks for your help!
There might be an approach using SendKeys -- but SendKeys is notoriously temperamental. Here is a more reliable approach:
1) If you don't already have it organized like this, have the click-event handler of the button a 1-line sub that looks like:
Private Sub CommandButton1_Click()
Process
End Sub
Where Process is the sub which actually does the heavy lifting. In general, I think it a good idea to have event handlers mostly functioning as dispatchers to subs.
2) Change the code for Process (or whatever you choose to name it) in the following way:
a) Modify the first line to look like
Sub Process(Optional Verbose As Boolean = True)
b) Where Process has something like
MsgBox "Processed!"
replace it by
If Verbose Then MsgBox "Processed!"
3) In the code you gave above, replace the line
ActiveSheet.CommandButton1.value = true
by the line
Application.Run "A.xls!Process", False
This approach will bypass the button entirely and run the code which the button normally triggers, but run it in silent mode.
On Edit: To use SendKeys you could do the following. Put the line
Application.SendKeys "~"
before the line
ActiveSheet.CommandButton1.value = True
~ is the character shortcut for Enter. SendKeys doesn't itself send the keystroke, instead it puts something on the Windows Message Queue. VBA doesn't have any direct control about exactly when this message will be processed. In this case the lack of control is a benefit. The VBA interpreter moves onto the next line, which triggers the MsgBox. By the time the SendKeys message is processed the default Okay button on the message box has the focus hence receives the enter key. This can even happen before the box is painted, making it seem that the MsgBox was never there -- but that is better to think of it as being destroyed before you have time to see it.
The reason why it is necessary to have the SendKeys line before the line which clicks the button is that once the message box appears it will cause the VBA interpreter to wait until it is closed -- hence the calling code will suspend its execution until after the message box is closed, hence the SendKeys wouldn't be processed until it is no longer needed.
I don't really trust SendKeys. I suspect that sometimes when you run the code what will happen is that A1 in the newly activated sheet will receive the enter key (shifting the selection from A1 to A2) before the message box appears. I'm not sure if this can happen, but if it does a workaround might be to move the SendKeys to a VBScript program. Launch this program (with window minimized and not waiting for return) before the button is clicked. The VBScript program can have say an 0.5 second pause before it uses SendKeys. The script will be running in a different thread so it won't be blocked by the message box.