SendKey string + enter at the same time - vba

Is it possible to send a string and an enter with SendKeys?
I tried this:
Application.SendKeys "String? {ENTER}"
What am I doing wrong?

You should be fine submitting each key seperately, at least that is how i have done it when i have used send keys recently;
Application.SendKeys "MyString"
Application.SendKeys "{ENTER}"

I'm using excel 2013 and this works for me
Sendkeys "12345" & "{ENTER}"
It will output the 12345 and do a carriage return..
Makes the code a little cleaner to have all the text on one line for putty purposes

Previously I have succesfully used a variation of the code below. My code did not the Application. code. This should concatenate the two strings.
Application.Sendkeys "myString" + "{ENTER}"
Tested
SendKeys "myString" + "{ENTER}"
on Excel 2016 and it does work.

Related

Sending hotkey strokes to Tablecurve 2D

I need to process about 3000 sets of data through TableCurve 2D, to get the correlation coefficients of each data set, using the same equation model.
I have used autohotkey with success, but would rather use one program language (vba) instead of both vba and AHK.
With the below code, I am able to 1) Open TableCurve, 2) Import data from clipboard, and 3) Load the data for modelling.
Sub excel_TC2D()
TC2D = Shell("C:\Program Files (x86)\TableCurve2Dv5.01\TC.EXE", 5)
Application.SendKeys ("%FC~")
End Sub
But when I add more sendkeys to the one SendKey statement, it ignores them. I tried adding an application.wait, thinking that TableCurve 2D needed some pause time to complete the hotkey sequence, to no avail.
I tried adding one extra hotkeys, per the below schedule
Application.SendKeys ("%FC~%") ---> worked
Application.SendKeys ("%FC~%P") ----> worked
Application.SendKeys ("%FC~%PA") ----> worked
Application.SendKeys ("%FC~%PAL") ----> fails
I also tried splitting into 2 sets of Application.sendkeys as in the below, but it failed as well.
Sub excel_TC2D()
TC2D = Shell("C:\Program Files (x86)\TableCurve2Dv5.01\TC.EXE", 5)
Application.SendKeys "%FC~%PA", True
AppActivate TC2D, True: Application.SendKeys "L", True
End Sub
Any thoughts on why this and how to make it work.
Thanks,

using sendkeys VBA to compress images in Visio as part of a sub process

I routinely produce visio documents with images on each page that I paste in from another app. The files are large so need compressing, which isn't fun by hand. I want to be able to loop through each page and compress any images a set amount (75%). So far I've got a sub that selects all images on a page and I've got a sub that uses send keys to bring up the image compression window and run compression at 75%. The problem is the two won't run together. SendKeys compression works fine if I select all images first then run compression separately. But executing in the same sub doesn't work. Code I'm using below. All I can think is that something is breaking the sendkeys rhythm because instead of compressing I'll get mc 75 inserted as text below the image, which is the sendkeys keys. Or I'm stringing them together in the wrong way.
If I run the following separately they work. ie manually executing each one
'selects all images on a page
Sub SelectImagesOnPage()
Dim vsoSelection As Selection
'add all pictures to selection
Set vsoSelection = ActivePage.CreateSelection(visSelTypeByType, visSelModeSkipSuper, visTypeSelBitmap)
ActiveWindow.Selection = vsoSelection
End Sub
'uses sendkeys to bring up the compress image dialogue
Sub compressImageSelection()
'should check an image is selected.
SendKeys "%(jp)", True 'holds ALT while pressing JP
SendKeys "m", True
SendKeys "c"
SendKeys "{TAB}{TAB}"
SendKeys "75{ENTER}"
End Sub
But putting the two together like this doesn't work.
Sub compressPicturesOnPage()
'add all pictures to selection
Call SelectImagesOnPage
'compress
Call compressImageSelection
End Sub
Update: Turns out that for the context sensitive 'Compress Image' button and 'Picture Format' tab to appear on the ribbon menu all Sub's have to complete. ie. visio has to 'regain control'. Blows a hole in what I'm trying to do but at least I know now! Possible solution by Jon Fournier in the comments to try.

Only paste last piece of code in column

I am relatively new to this software and could really use a hand. I am using Ditta to store multiple items on a clipboard and have given them a shortcut. With this script I am trying to get it to paste all copied items at once, using a shortcut key. I have tested each block of code individually and it works. But when I try to run it together, it only runs the final block. Does anyone have any idea why this is happening?
Sub Data()
ActiveCell.Select
SendKeys "^4", True
Application.Wait (2000)
ActiveCell.Offset(0, 1).Range("A1").Select
SendKeys "^3", True
Application.Wait (1000)
ActiveCell.Offset(0, 1).Range("A1").Select
SendKeys "^2", True
Application.Wait (1000)
ActiveCell.Offset(0, 1).Range("A1").Select
SendKeys "^1", True
Application.Wait (1000)
End Sub
I don't know where exactly the issue is, but it seems that it is all about waiting for Ditto to insert the values. Your code is just not waiting until the paste is done.
I wrote a workaround for that:
We have a loop now that runs from 4 to 1 counting backwards (this is to send the 4 keys. This is easier than having 4 times the same code.
The main idea is to clear the cell before we paste, and after the paste we wait until the cell is not empty anymore, which means the paste was successful. Note this is just a workaround to wait an amount of time until the paste is done.
Option Explicit
Public Sub InsertData()
Dim i As Long
For i = 4 To 1 Step -1 'loop from 4 to 1 backwards
ActiveCell.Clear 'clear active cell (so we know it is empty for sure)
SendKeys "^" & CStr(i), True 'send keys for paste
'wait until active cell isn't empty anymore.
'this means wait until paste is done
Do While IsEmpty(ActiveCell)
DoEvents 'give Excel some time to handle other events
Loop
'move over to the next cell
ActiveCell.Offset(0, 1).Select
Next i
End Sub
Just a note to your original code
Your waiting was wrong anyway Application.Wait(2000) does not what you expect it to do. The correct way would be:
Application.Wait(Now + TimeValue("0:00:02"))
To make it wait 2 seconds. The given time is not the amount of time to wait but the absolute time until the macro waits. For more see Application.Wait Method.

Excel VBA - Macro error - The macro may not be available - Auto Refresh

I have a macro enabled excel file in which I am trying to create a macro to run functions every 2 minutes.
The following is my code:
Sub macro123()
Application.SendKeys ("%{A}")
Application.SendKeys ("R")
Application.SendKeys ("A")
Call test
End Sub
Sub test()
Application.OnTime Now + TimeValue("00:02:00"), "macro123"
End Sub
The macro123 gets executed the first time I run it.
After 2 minutes it tried to run it again, that's when I am getting the following error.
[![enter image description here][1]][1]
The macro settings seem to be greyed out, probably due to domain settings of organization?
[![enter image description here][2]][2]
Is there any other way for me to execute those statements every n minutes?
If your macro is in the code module ThisWorkbook, then you should specify it including the code module's name.
Try this:
Application.OnTime Now + TimeValue("00:02:00"), "ThisWorkbook.macro123"
' ^^^^^^^^^^^^^^

Open a spreadsheet in the middle of macro

Currently, I have a sub which downloads an Excel file from the Internet. I would like to open the spreadsheet and run more commands on it prior to the sub ending.
The issue is that the spreadsheet does not open until the macro has finished executing thus it cannot find the workbook.
With appIE
.Navigate "https://www.alerian.com/wp-content/uploads/AMZmembers.xls"
.Visible = False
Application.Wait Now + TimeValue("00:00:03")
SendKeys "{Enter}"
End With
Application.Workbooks("AMZmembers.xls").Close
SendKeys "{Enter}" opens the sheet but it will not open until the sub has finished; therefore, Workbooks.close cannot find the sheet because it isn't open yet.
Appreciate help!
Use Workbooks.Open "path_and_filename" instead of the SendKeys. There are a bunch of other parameters in the Open method such as whether you want it opened read-only, etc but the simplest way is as shown.