It seems that "sendkeys" is only good for turning off your numbers lock. It does not appear to do anything else. I've tried many different ways to simply write random characters to notepad with no success. My only hunch is this ability has been disabled by a recent Windows update as it used to work fine until last week. Below is some example code. Does anyone know how to get it to send keys again?
Public Sub WriteToNotePad()
Dim vReturn As Variant
vReturn = Shell("NotePad.exe", vbNormalFocus) 'open notepad
AppActivate vReturn 'ensure notepad is active
Application.Wait (Now() + TimeValue("00:00:03")) 'wait a few seconds
SendKeys "You should see this in notepad", True
End Sub
I used a computer that didn't get the Office 365 update & the code worked fine. So I'm certain that's the problem. To fix it, I used John Coleman's suggestion to write a VBS file instead & then call that script from the VBA code.
From the above example, change this:
SendKeys "You should see this in notepad", True
To this:
Call Shell("wscript C:\writeToNotepad.vbs", vbNormalFocus)
And the VBS file would be saved in that location with that name & with this content:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "You should see this in notepad"
Related
Im currently trying to write a "controller" File with a macro, that opens other files and just calls 3 Macros in each file.
All these files have the same structure.
My Problem is that I can't see the VBA structure, but I do know the Macronames as they are shown to me.
But everytime I try to call these macros im getting the following Error Message:
Runtimeerror 1004 The Makro !xxxx can't run. It's maybe not available
in this file or all macros have been deactivated.
Macros are activated. If I click the button in that File the macro that gets triggered works perfectly fine.
Any suggestions welcome
Thanks
I've tried everything with wrong filenames with false characters and single commatas.
The Opening of the File via
Set wb = Workboos.open
works perfectly fine, so the file name probably can't be a part of the problem.
You need to specify the name of the workbook you are calling the sub from.
Suppose you have these subs in both workbooks 1 & 2:
Public Sub macro1()
MsgBox "macro1 launched from " & ThisWorkbook.Name
End Sub
Public Sub macro2()
MsgBox "macro2 launched from " & ThisWorkbook.Name
End Sub
Now you can call the following sub from your controller macro:
Sub launcher()
Dim wb As Workbook
Set wb = Workbooks.Open("C:\Users\<username>\Desktop\Workbook1.xlsm")
Application.Run wb.Name & "!" & "macro1" 'the distant macro which you know the name
End Sub
Works perfectly
Answer to the riddle: Even tho I thought that I checked the File Name with 'Filename.xlsm', the problem resolved when I removed all dashes from the Filenames!
I'm trying to open an excel file, print it and then close it by clicking on a button.
It opens, it prints, it closes my file and then excel just crashes.
No warning, it justs closes everythting (2 files) and restart by opening the "version 1" of my files.
Workbooks.Open "Thenameofmywb.xlsm"
Workbooks("Thenameofmywb").PrintOut
Application.CutCopyMode = False
Application.Wait (Now + TimeValue("0:00:05"))
Workbooks("Thenameofmywb.xlsm").Close SaveChanges:=True
Here is a bit of my code, I tried the Doevents method and a lot of others small things but none worked.
Maybe you'll be able to help me.
I found it. I don't think it was code related or not in the code I wrote.
The file that I was opening must have been corrupted.
I tested with "clean" files and it worked.
So I copied and pasted informations from the "corrupted" file to a new one and now it runs well.
Now I don't know how they got corrupted but that's for an other time.
Thank you all for your help
You are not making any changes to the file, just accessing it, printing and closing it. So last part should be SaveChanges := false
Also "On Error Resume Next" at the beginning of the sub procedure.
The below code works on my side, using Nathan's idea of using an object.
Sub qqq()
Workbooks.Open "C:\Users\info\Documents\HHH.xlsx"
Set wb = Workbooks(2)
wb.PrintOut
Application.CutCopyMode = False
Application.Wait (Now + TimeValue("0:00:05"))
wb.Close
End Sub
I tried even the following code and it works:
Sub hhh()
Workbooks.Open "C:\Users\info\Documents\HHH.xlsx"
Set wb = Workbooks(2)
wb.PrintOut
'Application.CutCopyMode = False
'Application.Wait (Now + TimeValue("0:00:05"))
wb.Close
End Sub
I commented out 2 lines of code. I am testing with xlsx as a file that needs to be printed. If you are using xlsm, there might be some code there that you have that could be causing an impact. You could still use xlsm but in a way that code from 2 workbooks open at the same time could cause an issue.
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
I'm trying to simulate this action:
activate another application,
send keystroke ctrl+c,
go back to Excel,
send key stroke ctrl+v
and have that value in a cell.
It's a DOS window style application, so keystroke is the only way to manage it.
I managed to activate and to input keystrokes such as ENTER into that DOS style application, but when I try ctrl+C it is not seen to do anything.
I tried simulating it in Excel VBA with:
Range("E7").Select
SendKeys "^c"
Range("G7").Select
SendKeys "^v"
The E7 value is not copied, but G7 (paste destination) is highlighted as if it was selected for copying.
Note: I am not trying to copy things from Excel to Excel, but to execute keystrokes using Excel.
I ran into the same issue today.
I solved it by waiting a bit after sending CTRL-C to the application. It seems nothing is copied if you immediately execute another script line.
SendKeys "^c"
WScript.Sleep 100 ' wait a bit after CTRL+C (otherwise nothing is copied)
In fact, the same issue seems to happen when switching to another app and sending CTRL+V.
Again, I solved it by waiting a bit:
AppActivate "Microsoft Excel"
WScript.Sleep 100 ' wait a bit after making window active
SendKeys "^v"
The problem is that copying and pasting takes some time and VBA is not waiting for it.
First you need to specify second argument of send keys method which is "Wait". Set it to true. Thus VBA execution is waiting for until sending keys is completed.
Secondly you need to wait until process of copying data to clipboard is completed. "Wait" in sendkeys is not doing it because it's not about sendking keys by VBA but it's about Windows working with clipboard. To do it please use my function IsCopyingCompleted.
Here's how final can look like:
SendKeys "^a", True 'Select all
SendKeys "^c", True 'Copy
Do
DoEvents
Loop Until Me.IsCopyingCompleted()
YourSheet.Paste
Function IsCopyingCompleted() As Boolean
'Check if copying data to clipboard is completed
Dim tempString As String
Dim myData As DataObject
'Try to put data from clipboard to string to check if operations on clipboard are completed
On Error Resume Next
Set myData = New DataObject
myData.GetFromClipboard
tempString = myData.GetText(1)
If Err.Number = 0 Then
IsCopyingCompleted = True
Else
IsCopyingCompleted = False
End If
On Error GoTo 0
End Function
I had this same problem - I developed code and it worked on my computer - SendKeys "^v"...but it did not work on another user's computer! I was trying EVERYTHING...delays, appreciate, accessig access from Excel, having the user check various settings, selection.paste...no good. Finally I noticed different syntaxes...we were using the same office version, I'm not sure about Windows. But I entered 6 different syntaxes and had it skip over if it failed....SendKeys with a capital v, enclosed in squigley brackets, each component in squigleys separated by a plus sign, etc. IT WORKED! 2 of the commands put the ^V in the body of outlook...I remove those 2 and I'm golden.
Currently I'm writting VB functions and save them as an Excel addin .xlam file.
I want to have a .bat script so as to quickly deploy those addins.
Currently, to activate my .xlam addins, I have to Open Excel - File - Option - Addins - Browse to addin files... as below screenshot. This is absolutely manual, repeated & tiring thing to do.
So my need is to automate the activation process.
I was looking for exactly the same sort of thing this morning. I will eventually try something like this out, but I haven't yet. So, here is what I have come to so far:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.addins2.add.aspx
This is an example about how to use Excel automation from C#. From what I see, all these automation interfaces are really COM interfaces, so you are not restricted to C# or Visual Basic (maybe you can use some fancy scripting of Windows to work with them? what I will try is to use python with pywin32, but that's only because it suits my taste).
Then, for registering the addin(s), check this method:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.addins2.add.aspx
I actually saw an example somewhere about how to use it, but I can't find it right now.
Anyway, these are just ideas. I'm very interested on knowing how it all ends ;-)
you can insert this code in your *.xlam in the sheet "ThisWorkBook" this code install and activate the current AddIns, just by opening
Private Sub Workbook_Open()
Dim oXL As Object, oAddin As Object
URL = Me.Path & "\"
normalUrl = Application.UserLibraryPath ' Environ("AppData") & "\Microsoft\AddIns"
AddinTitle = Mid(Me.Name, 1, Len(Me.Name) - 5)
If URL <> normalUrl Then
If MsgBox("Can you Install AddIns ?", vbYesNo) = vbYes Then
Set oXL = Application ' CreateObject("Excel.Application")
oXL.Workbooks.Add
Me.SaveCopyAs normalUrl & Me.Name
Set oAddin = oXL.AddIns.Add(normalUrl & Me.Name, True)
oAddin.Installed = True
oXL.Quit
Set oXL = Nothing
End If
End If
End Sub
After one manually added time, we can update the addin by copy the addin file to Excel addin lair. Here is the .bat script to do it.
set fipAddin=".\FIPphase2.xlam"
set excelAddinLair="%APPDATA%\Microsoft\AddIns"
copy %fipAddin% %excelAddinLair%
Hope it helps!