Run macro at key press - Space key - vba

I have a Word 2013 Macro which quickly runs a simple spell check dialogue.
I want to run it every time I type a word.
One way of doing this is by running the macro every time I press space.
Therefore, I tried to use the Options>Customize Ribbon>Keyboard Shortcuts method but that did not work for the space key.
How can I run a macro every time press the "space" key?
Thank you in advance.

You should be able to do this using KeyBindings. I've not tried it with spacebar specifically, but I use this with tab, backspace, etc. The basic idea is:
in a sub that you run at startup or document open:
'This line may differ depending on whether you are running this from a document template or an add-in.
Application.CustomizationContext = ThisDocument.AttachedTemplate
' Create the keybinding.
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeySpacebar), KeyCategory:= _
wdKeyCategoryMacro, Command:="MyMacro"
Then make sure your macro is named to match whatever you put in Command.

Related

How to send a new line without using `n `r? without an enter

i know that n means "ENTER", but when i'm using n, i dont want to press enter. I just want to put the text in a new line, but what "n" does, it puts in a new line but it also press enter.. i don't want to press enter...
For example
::pergunta::Hello, n How are you? n what are you doing? n bye...
How can i fix that issue?
First, you need to determine what keystrokes your application uses for this purpose, and then you can implement that in Autohotkey.
For example, in Excel, multiple lines of text can be typed into a single cell by pressing ALT-ENTER at the end of each line (instead of ENTER).
So, the following AutoHotkey script would work for Excel:
:X:pergunta::Send, Hello,!{Enter}How are you?!{Enter}what are you doing?!{Enter}bye...
The ":X:" at the beginning means "run a script", which in this case is the "Send" command. The !{Enter} part means ALT-ENTER.
You might also want to use SetKeyDelay to speed up the typing of text:
SetKeyDelay, 0
:X:pergunta::Send, Hello,!{Enter}How are you?!{Enter}what are you doing?!{Enter}bye...

Find and Replace/Insert 'CR', del Space wherever a user Clicks in a Word document

Short Version
In MS Word, is there a way to insert some text and remove some(simultaneously) at the point of blinking cursor or wherever a user clicks repeatedly.
This (Insertion/Deletion) can be achieved in the following ways:
Mouse click
(Ctrl, Alt or Shift) + Mouse click
Ctrl/Alt/Shift+Mouse click + (Any Key)
Long Version
I have text copied from various sources in different word files which I want to format in a particular style so as to match a given layout. Basically the text needs to be broken down into new lines/paragraphs at specific places which is given in the instructions. Instead of having to press 'Enter' and then remove/delete spaces at every line, I wish to do that with just one click per line using macro.
These files could be 500+ in number, so modifying all in one go using Macros is the preferred way.(Although I find it impossible when the point of insertion of 'Enter' can only be done manually on each file.) I have used Macros to achieve 90% of that however need one last step where a Macro can do the following:
Where ever the user clicks on the page, all space(regular, non-breaking space, white-space) until the next printed character is deleted.
An 'Enter' or Carriage-Return is inserted, so that the text moves to the next line.
Steps 1 and 2 are repeated at every click the user makes on the text.
Example Text
This is an example, of my text, which I need to split up, into different lines.
Expected Text
This is an example, of my text,
which I need to split up,
into different lines.
Notice that as 'Enter' is pressed at the end of these lines although they come in next line but usually a space character is there in front of those lines. Another approach is to click, delete the required spaces until the next character and then hit enter. I want either of these to be done automatically with just a mouse click or a minimum of key+mouse combo.
Something like Find ^w and Replace with ^p running at every place a user clicks, but with Macros.
Any help is appreciated.
This would be possible by defining a shortcut to your macro. So you would need to click at a position and use your shortcut to run the macro.
Eg. Add this to a module in your document
Option Explicit
Public Sub DeleteSpacesAndInsertEnter()
Application.ScreenUpdating = False
Selection.SetRange Selection.Start, Selection.Start + 1
If Asc(Selection.Text) = 32 Or Asc(Selection.Text) = 160 Then
Do While Asc(Selection.Text) = 32 Or Asc(Selection.Text) = 160
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
Selection.SetRange Selection.Start, Selection.Start + 1
Loop
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
End If
Application.ScreenUpdating = True
End Sub
and assign a shortcut to it.
It deletes all spaces & non breaking spaces right from the current cursor position and then inserts an enter.
Earlier, I thought of using Find and Replace at the place of cursor, Finding ^w and Replacing with ^p but this required me to Constantly lift the shortcut keys I assigned with my macro, otherwise it would just break the complete text at every word into paragraphs. Just pressing Ctrl and click would highlight the text between cursor and mouse click. All in all, I had to press (Ctrl+D), move the cursor to the new point using Click, AGAIN press Ctrl+D. Thus lifting 3 fingers for every change, making it impractical.
Pᴇʜ Macro helped
Ctrl+D+Click still doesn't work seamlessly. Works in 3 steps repeating.
Alt+D+Click worked almost flawlessly, with just one step repeating.
Applying this macro, even if I keep Alt+D(my shortcut) pressed all the time, simply clicking at the desired place does my job. Although Alt + Click anywhere in Word 2007 brings up a Research Window which updates as the text is clicked but in no way interferes with the job being done.
Thanks a lot everyone and especially P for all your help, really appreciate it.
Cheers

Checking if a macro is called in another macro

I have a lot of macros in my excel. I don't know whether they are used in that workbook. I don't know whether it is called as a procedure inside other macros.
Is there any way to find out whether the macro is used inside another macros or used in different sheets?
Probably the easiest way - Ctrl + F.
Write the name of the "Macro" and select search in Current Project. Then start counting how many times it will show up.
Another way is to write debug.print "I am used" after the Sub line of the "Macro". Then count how many times has it popped up on the immediate window.
put “Option Explicit” at the beginning of every module
comment out your “Macro
start debug
And it’ll point you to any occurrence of “Macro” call

In TOAD, is there a way to comment out arbitrary blocks of code (i.e not whole lines)?

The shortcut to comment out code in TOAD (11.6.16) is CTRL+B
However, the problem is that it does not let you comment out an exact selection within a block of code. So, when I press CTRL+B it simply comments out all the lines of the code , so I can't comment out a specific column with /* .. */ via keystroke.
Is there a way to run the arbitrary /* .. */ commenting ?
Not exactly, but you can get there by a lesser known feature. The Search and Replace macro. In the Editor hit Ctrl+R to get to the replace dialog. On the toolbar click the right-most button to get to the macro editor. Copy the entire contents, below, and paste into that dialog within the left side tree. It'll create a macro for you that I made. Hit close, etc. to get out of that dialog. Now, in the editor whenever you make your selection you can invoke this by using the little dropdown next to the replace button on the Editor toolbar. It's not exactly as easy as using keyboard shortcut, but it works and Search/Replace macros are super powerful for more complex work. Here's the dropdown I'm referring to.
Here's the code to copy/paste. Grab everything from (and including) "object" through to (and including) the "end"
object TComponentCollection
Items = {
545046300F5474645265706C6163654D6163726F000B446973706C61794E616D
65060D436F6D6D656E7420426C6F636B0D49676E6F72654661696C757265080A
4D6163726F47726F7570080C5365617263684F726967696E070D736F456E7469
726553636F70650C577261705365617263686573080F50726F6D70744F6E5265
706C616365080A5465787446696E6465720A9C000000545046300F5474645465
78745265706C61636572000D4361736553656E7369746976650811526567756C
617245787072657373696F6E090A5365617263685465726D0606283F73292E2A
0F536561726368446972656374696F6E07097364466F72776172640A57686F6C
65576F726473080B5265706C6163655465787406062F2A24302A2F135265706C
6163655769746854656D706C617465080000054974656D730A32000000545046
301F547464437573746F6D5365617263685265706C6163654D6163726F4C6973
7400054974656D730A0000000000000000}
end

In batch file, how to get focus on a specific window (opened file)

A very simple question I suppose, but I reached a deadlock with this:
I have to use a .bat file to imput plain text data into the right cells an excel sheet with lots of graphics content, vba parts, macro that deactivate "normal" EXCEL buttons and functions, password to protect the pre-typed functions, sudden and unexpected changes in the version of the "taget file", and many other complications...
My need is to be absolutely sure that the .bat is sending the sequence into the right version of the .xlsm file.
To do that I want to store the last known filename (that include the file version) in the .bat file, and I want to take focus on the excel window in wich the data have to be written ONLY IF the title of the excel window is exactly the same.
So Here is the question: How to get the focus on a specific open file from a .bat file?
You can't. If you wanted to use vbscript or jscript you could do what you want in a command prompt in an unreliable way (but it will work most circumstances).
Excel has it's own forms.
Put column headers in a row. Put selection in same row. Alt + D, O.
Plus you can make Excel only allow entries on some cells, like a invoice form.
Right click cell, Properties, Protection, Unlock. Then Alt + T, P, P.
Word has it's own forms similar to Excel (Word is also a spreadsheet).
Excel VBA language (and VBScript too) has input box command.
Sub main()
Sheet1.Range("A1").Value2 = InputBox("enter Value")
End Sub