What I am trying to create is a writing assistant for MS word that gives me advice during writing. In order for that, I need to check whether the letter or number keys on a keyboard are pressed and if so, run a macro that shows the assistant popup and do the background work.
The problem is that I can't get the keypress detection to work. I tried multiple things like the two examples below but they don't give me the desired effect.
Private Sub document_KeyPress(KeyAscii As Integer)
If KeyAscii > -1 Then
MsgBox ("You Pressed a key")
End If
End Sub
Private Sub 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"
End Sub
The first one is not working at all and the seconds overwrites the keybinding which makes the spacebar in this example useless. With the second example, I also have to assign a binding for each character.
Any advice would be appreciated.
Since your question implies that you are doing this for yourself and not trying to deploy this macro to others, you can simply assign a keyboard shortcut to the macro from Word Options and you don't have to write custom keyboard trapping VBA to do this.
From Word's File tab select Options
Click Customize Ribbon
Click Keyboard shortcuts: Customize...
Select Macros from the All Commands Categories list
Locate your custom Macro and Assign a custom keyboard sequence
Via links that were shared in the post, I came across a program called AutoHotKey. After some research, I set up a system where AutoHotkey does the detecting part and then calls the macro in word. I use the following hotkey script:
Keybinding for a-x before this
y::
send y
StartLabel("a")
return
z::
send z
StartLabel("a")
return
Space::
send {Space}
StartLabel("a")
return
Backspace::
send {Backspace}
StartLabel("a")
return
^Backspace::
send ^{Backspace}
StartLabel("a")
return
StartLabel(x)
{
word:=ComObjActive("word.application")
word.run( "Writeass" ) <-- macro name. No need to define the module first
}
This script calls the macro every time one of de defined keys is pressed.
With a simple bat file I can start and close the script (to make sure it does not run in the background when wordt is not open).
Bat code to start the script:
start C:\path to Autohotkey script\WriteAssistant.ahk
Bat code to end the script (killing the autoHotKey process)
taskkill /im AutoHotkey.exe
I use the following vba script to start and close the hotkeyscript from a word macro (in my case I made a button in the ribbonbar to start the macro and the first thing it does is start the autoHotKey script):
Path = "C:\path\start.bat"
Shell Path
And best of all, I didn't notice any speed loss.
Related
I have an Excel project with a lot of macros.
What I want is assign macros to buttons in the ribbons.
For that I created macros like:
Sub R10_parallel_device()
help.helpON ("parallel_device ")
Call sub_novelty_claims("parallel_device")
End Sub
So the three conditions are met:
a) the macro SUB R10_parallel_device() do not accept parameters
b) it is not private
c) It is not hide
when I go to the list of macros I see all the list of macros named Rnn but all THE BUTTONS ARE GREY OUT EXCEPTFOR CREATE.
Now if I click in that sub for instance SUB R10_parallel_device() I can not edit it and if I click "create" excel sends me to a new created module where
Sub R10_parallel_device()
End Sub
appears.
&
When I go to file/options/ribbon if I want to add that macro to a button it is listed and it is possible to assigne the macro to a button but it would not run, giving the error that such a macro is not found.
NOTE: I checked which macro are listed thisworkbook/all/ etc.
note2: this did not help me. this neither
thx
Confirmed it's the macro names - go to Developer -> View Code (or type Alt + f11), then select the module with your code in it and rename them. For whatever reason it's that R# syntax:
I have been trying to do this myself, but I just dont know how to define the problem. I have been writing a Macro for LibreOffice and it includes several dialogs. When I Run the macro I want to execute a Function after the dialog is visiable. I could not find the solution to this so I made another dialog which only shows "Loading, Wait..." and I inserted at the beggining and end of that function, dialog.Execute() and dialog.endExecute(). I guess the program stops at .execute() and im stuck at "Loading, Wait..." sign if I press "X" in the corner the program continues normally.
Best solution would be if I could run a function after the dialog is visible. So is there sort of a trigger ?
You can load the dialog and make it visible, but this would not activate the functions (buttons etc.), as this is what execute does
' StarBasic
' Tools
With GlobalScope.BasicLibraries
If ( Not .isLibraryLoaded("Tools") ) Then
.LoadLibrary( "Tools" )
End If
End With
sampleDialog = LoadDialog( "Standard", "Dialog1")
sampleDialog.setVisible(TRUE)
Does this help you?
So first, when one clicks on the "View Macro" button, this pops up:
What I want to know is, is there some code that I can run on workbook open (and then "unrun" on workbook close) that grays out that run button (like the others underneath it are) ONLY until the password is entered in the VBA project (using Alt+F11 to open the editor)?
I don't want the users to be able to run any of these subs manually.
If you declare the sub so that it needs input, even optional input it will not show in the list either.
sub Test(optional a as string)
Declare the subs as private and they won't show up in the Alt+F8 dialog box.
Declare them as public (the default) and they will.
You can use vba to edit the vba code of another module.
Is it possible in Excel VBA to change the source code of Module in another Module
You can change one line or search through the lines and comment/uncomment whole blocks of code. Capturing the event when vba is unlocked may be the hard part. You may have to run a sub that does this after unlocking vba.
I think you have the wrong approach to this and it would be better to structure your code more properly.
The first two on that sheet are called from other macros that are run with buttons on my main worksheet.
OK. So attach these to a form control/button, and use Bigtree's suggestion to include an optional argument in these subs. They will not display in the Macros menu. YOU can run them at least three different ways:
either from the VBE by finding the procedure and pressing F5, or
by entering the name of the procedure in the Immediate window in the VBE, or
by pressing the buttons you have provided.
The middle two are called when the sheet opens and closes
Sounds like this should be a private subroutine (or, use the method above from Bigtree) and CALL these from the one or more of the appropriate event handlers (at the worksheet level perhaps: Worksheet_Activate, Worksheet_Deactivate; or at the workbook level SheetActivate and SheetDeactivate depending on your needs)
You can always run the procedure manually from the Immediate window in VBE, or by manually invoking the event procedure, etc.
and the last two I manually call when I want to edit my main sheet
Again, call from the Immediate window or manually from the VBE. You only have 6 subroutines here, it can't be that difficult to locate the ones you frequently need. Put them in a new module and organize your modules so you know where these are.
Alternatively, put some boolean in the subroutine like:
Sub SheetLock()
If Not Environ("username") = "YOUR_USERNAME" Then Exit Sub
'other code to do stuff is below...
End Sub
UPDATE FROM COMMENTS
The Immediate Window is like a console/command line, it is where the result of your Debug.Print statements would appear, etc. It is very handy for debugging and evaluating expressions.
There are several ways you could invoke the subroutine depending on whether it is public/private, including
Application.Run "MacroName" or simply MacroName if public
Application.Run "ModuleName.MacroName" or Call ModuleName.MacroName if private
I did not want to use a private sub,
I used the shape name to determine if from a button
On Error GoTo noshapeselected
shapeis = ActiveSheet.Shapes(Application.Caller).Name
' I manually set the shape name in the page layout tab selection pane
' below I test for the desired button
If shapeis = "your button name" then
goto oktogo
else
goto noshapeselected
endif
noshapeselected:
msgbox ("must run from a button")
goto theendsub
oktogo: 'continue if desired shape was selected
theendsub: 'a button was not pushed
For those macros without buttons, but called by another macro,
set a variable to 'OK' to run a macro, then the called macro tests for 'OK' ('OK' means initiated by another macro 'NOK' means not initiated by another macro)
This is related to this question
How do I programmatically/automatically change the text formatting for the text of a Visio shape after I am done with the editing?
There I tried to use some code that would be executed when the text of a shape is edited
For some reasons, as described there, in some situations which I could not isolate the code is executed endlessly.
That is probably because the script calls itself in a loop. The code is supposed to be executed every time the text of the shape is edited BUT the code itself changes the color of the text. I guess that would count as a new text change and so on.
I am thinking about using the event called TextChanged. According with the documentation this should execute the code when the user finished the editing
http://msdn.microsoft.com/en-us/library/office/ff768749.aspx
I could not find a example of using such event in VBA and here is where I need your help
thanks in advance
Uni
This is how I tested:
I used CallThis('ThisDocument.warning") to call the procedure below from "TheText" event of that shape (available via the shapesheet)
Sub warning(oShape As Visio.Shape)
MsgBox ("Text edited")
End Sub
This gets executed like three times if I star editing the text of a shape and I press the space key (adding a space to the existing text)
Ok here is how you do it:
Open the Code editor
In the Project explorer select "ThisDocument"
Next in the ObjectWindow select Document
Then in the Method dropdown box select ShapeExitedTextEdit
You will see a skeleton procedure like below
Private Sub Document_ShapeExitedTextEdit(ByVal oShape As IVShape)
End Sub
There is where you add the code that you want to be executed every time when the editing if finished
I have a form that I want refreshed when the submit button is clicked. Preferably those that have default values will be restored and those without will be blank.
The submit button has an attached OnClick Macro that, checks to make sure all fields are filled, if so an action query runs that inserts a new row into a table.
So its after this action query that I want the refresh to occur. I have tried researching and come across suggestions that suggest using VBA code Me.Requery or Me.Refresh. I'm not 100% on how to incorporate this into the macro. The RunCode command doesn't recognize the function I put the code in, and The convert macro to VBA option in the top left is grey'd out.
I'm new to Access and just not seeing where the communications are for code and macros if someone could please elaborate for me I would be greatly appreciated.
Please consider my solution as a brief introduction to VBA. If you care to learn the language, you will find there is very little you cannot do.
Within your submit button properties, there should be an 'Event' tab. In this tab, the On Click should be set to [Event Procedure]. Click the three dot button just to the right of that, and it will launch the VBA editor.
All you need to have here between the Private Sub and End Sub lines are these lines of code:
DoCmd.RunMacro ("Mac_1")
Me.TextBox1.Value = "Null"
Me.CombBox1.Value = "Null"
Me.Refresh
MsgBox "Your Save Was Successful.", vbOKOnly, "Saved"
"Mac_1" is the name of the macro you want to execute. the ME.Refresh executes as soon as mac_1 finishes, and will refresh the page. Be sure to enclose it in a proper quote (") and not a double tick ('').
The Requery command can be used from both within VBA code or a macro. It sounds like you are using the macro designer so you can use the Requery macro action instead of VBA.
Just add this to your macro after you have inserted your new data.
This macro action lets you specify a control to requery. The parameter can be left blank to requery the source of the active object. More information can be found here.
Edit
In response to your comments, I think you should try experimenting with the SetProperty macro action (more details here).
I have attached this macro to a button click event and the value from the TextBox called txtInputValue is cleared. The Value field is left blank as you want to fully remove the value from the text box.