How to send a right curly bracket with Dragon NaturallySpeaking's advanced scripting? - sendkeys

How to send a right curly bracket (i.e., }) with Dragon NaturallySpeaking's advanced scripting?
MSDN's document on the SendKeys Statement says:
To specify brace characters, use {{} and {}}.
Sending a left curly bracket (i.e., {), works fine with the following advanced scripting command:
Sub Main
SendKeys "{{}"
End Sub
However, when I try to do the same for the right curly bracket (i.e., }), it doesn't work:
Sub Main
SendKeys "{}}"
End Sub
Why?

This is an old glitch in Advanced Scripting.
You can use the ASCII code instead:
Sub Main
SendKeys Chr(125), True
End Sub

You don't need to escape a right brace by itself, just do:
SendKeys "}"
But if you want to send a whole "braced" expression, it gets more complicated. For example, to use a script to send "{Esc}" you need this:
SendKeys "{{}Esc}" ' {Esc}
Hth,

One inconvenient way to circumvent this issue: going through the clipboard, as shown below.
Sub Main
originalClipboard = Clipboard
Clipboard("{")
SendKeys "^v"
Wait(0.2)
Clipboard(originalClipboard)
End Sub

Related

How can I use square brackets in Application.OnKey? (Excel VBA)

I would like to use the key combination Shift + Alt + [ to call a subroutine in my VBA project. Here is the code I have tried:
Appication.OnKey "+%[", "mySubroutine"
I have had no problems using other characters with shift and alt, such as lowercase letters and numbers. However, when I try to use the left square bracket, I get the following error:
Method 'OnKey' of object '_Application' failed
I have also tried to use the left square bracket with all of the different combinations Ctrl, Alt, and Shift. They all produce the same error.
Application.OnKey "^[", "mySubroutine"
Application.OnKey "+[", "mySubroutine"
Application.OnKey "%[", "mySubroutine"
Application.OnKey "^+[", "mySubroutine"
Application.OnKey "^%[", "mySubroutine"
Application.OnKey "^+%[", "mySubroutine"
I also tried using the ASCII code for the left square bracket (91) like this:
Application.OnKey "+%{91}", "mySubroutine"
with no luck.
I also tried using Excel's built-in Chr() function with that ASCII keycode:
Application.OnKey "+%" & Chr(91), "mySubroutine"
which did not work, either.
I am running Excel 2013. Another computer in our office is running Excel 2003, and though that computer is using Excel4Macro language, it IS able to use the left square bracket to set up a keyboard shortcut.
It seems like Microsoft removed this capability in the newer version of Excel. However, if anyone can figure out a way to make it work, I would be grateful!
Application.OnKey "+%{[}", "mySubroutine"
Same codes as for SendKeys...
How to print or send braces ( ) using VBA sendkeys
https://social.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx
The plus sign "+", caret "^", percent sign "%", tilde "~", and
parentheses "()" all have special meanings and must be enclosed within
braces "{}". Square brackets "[]" must also be enclosed within braces
although they have no special meaning. To specify brace characters
themselves, use "{{}" and "{}}".

Execute specific line vba

I couldn't find this asked anywhere. In Visual Basic (excel), I can hit F8 and cycle through each line. But lets say I want to begin the sub procedure, and then after executing the first 2 lines, I'd like to skip to line 200. Until now, I've always just dragged the yellow arrow to the desired line. This is really time consuming and I was wondering if there's any command to simply say "run current line where selected" or something.
Additionally, even if I could begin to run through line by line, and quickly move the yellow selected arrow to the desired line, that would also work.
If you have a 200-liner procedure that does so many things you'd like to skip most of it, it looks like you need to refactor a bit.
Extract "things the procedure is doing" into their own Sub procedures and Function scopes. If you have banner-like comments that say things like '*** do something *** then that's a chunk to extract into its own procedure already.
Stepping through that procedure could then involve stepping over (Shift+F8) the smaller procedures that do one thing, or break and skip the call altogether.
Right click on the line you want to jump to. Hit the Set Next Statement option in the menu. That's equivalent to dragging the arrow to that line. (Ctrl-F9 is the hotkey for this action.)
If you want it to quickly execute every line up to a certain line, set a breakpoint then hit run instead of stepping through the code line by line. Do this by clicking on the gray bar to the left side where the yellow arrow appears. A dark red dot should appear and the line should be highlighted in dark red. This tells visual basic to stop when it hits that line.
You can also comment lines out by starting them with an apostrophy.
Finally, you can break code into subroutines and execute them independently of eachother.
Sub Subroutine1()
'This is a commented out line. It does nothing.
MsgBox "Do stuff here"
End Sub
Sub Subroutine2()
Subroutine1 'This will run all the code in subroutine 1
MsgBox "Do more stuff here"
End Sub
In the above example, if you run Subroutine1 you'll get one message box popping up. If you run Subroutine2 you'll get two message boxes.
Unfortunately it is not possible to do what you ask directly.
However, you may comment out the lines of code above the code you want to be executed for example:
Sub Workbook_Open()
'Application.DisplayFullScreen = True
'Application.DisplayFormulaBar = False
'ActiveWindow.DisplayWorkbookTabs = False
''ActiveWindow.DisplayHeadings = False
Application.EnableEvents = True
Password = "1234"
ActiveWorkbook.Protect
ThisWorkbook.Protect (Password = "1234")
End Sub
You may use GoTos, but however this is not considered good practice and may actively harm your code:
Sub Workbook_Open()
GoTo ExecuteCode
Application.DisplayFullScreen = True
Application.DisplayFormulaBar = False
ActiveWindow.DisplayWorkbookTabs = False
ActiveWindow.DisplayHeadings = False
Application.EnableEvents = True
ExecuteCode:
Password = "1234"
ActiveWorkbook.Protect
ThisWorkbook.Protect (Password = "1234")
End Sub
This is how I do it - basically if I know that my code up to line 200 is working properly but I'm pretty sure there's an error between 200-300 then before compiling - scroll down to line 200 and mark it (to the left of the code). Then compile it - click F5 and it will execute everything up to line 200 - then you can step through each line thereafter individually.
I normally comment out lines of code that I don't want to run with apostrophes. Alternatively, you can break up your code into smaller procedures so that you can easily pick and choose what you want to test/run.
I found adding a module for testing and copy pasting snippets of code in it as a best way for troubleshooting.

VBA MS-WORD Trouble with sendkeys

I'm having some issue with this code
Private Sub CortarSobrantes()
'Procedimiento que llama al comando "Comprimir imágenes" con parámetros
With Application.CommandBars.FindControl(ID:=6382)
SendKeys "%T%n%C{ENTER}", False ' Las letras equivalen a los accesos de teclado en la ventana, ~ para ejecucion
.Execute
End With
End Sub
If I hit Run (F5) inside the project, it runs OK, but when I try to call it from a button shortcut it doesn't catch the SendKeys. This also happens if I try to run it step by step (F8)
BTW %T%n%C is for Spanish command combination (all images, not compress, without resolution change and deleting cropped areas)
The reason I'm using SendKeys is that I'm trying to remove cropped areas within a function in order to call it from a button, so I could skip marking the options. As far as I know, there's nothing in the object model that allows this.
Am I missing something with the focus?
Not sure what you're actually trying to achieve, but in general the SendKeys() method is usually avoided in VBA because it's unreliable and buggy at best.
SendKeys() will send virtual keystrokes to whichever window has focus at the time of execution - so timing is everything.
If you know the exact text in the caption of your window you can use the AppActivate() method to force focus just before using SendKeys()
Moreover, SendKeys() is more widely regarded as a "final attempt" or workaround because 90% of the time you can use winapi to get the same result reliably although more advanced knowledge of VBA/programming is required when using Win API
I'm also having trouble with the SendKeys-statement.
I'm trying to send Alt= to a Word document. Thereby changing some mahematical formula in an equation field. For instance: ((x_1 q+x_2 p)/(p+q),(y_1 q+y_2 p)/(p+q))
After some time I found that the SendKeys-statement works as advertised only and ONLY if there's no further statement in my macro.
I know this is a bit awkward, but there it is.
Consider the following:
Public Sub test()
Dim test As String
test = ActiveWindow.Selection.Text
SendKeys "%=", True
' MsgBox test
End Sub
This works fine.
Copy/paste the formula above in a Word-document, open VBA in Word, make a new Module and copy/paste the Sub above into it.
Select the formula.
You must start the Sub form the Macro's menu (Alt+F8).
You'll notice that the formula will change in an equation-field and gets centered in the middle.
Run the macro again and the formula is changed back in standard text-format.
Now activate the MsgBox-statement and you'll notice that the SendKeys-statement doesn't work anymore.
I know this is hardly a workaround for the problem, but maybe someone will find this information useful!

What are a good No OP operation for vb.net?

Something we can just put break point on while making sure it doesn't do anything else.
In c, that would be while(false);
What to do in vb.net?
If you always need it to break there you can put
Stop or Debugger.Break()
If you really want a No-Op for some reason (could this turn into a contest for the most ineffectual single line of code?!), how about these?
System.Threading.Thread.Sleep(1) - 1ms is unlikely to have a huge impact outside of a loop
Debug.Write("") - doesn't appear to output anything.
There is a legitimate use-case for this.
When a temporary breakpoint is required after the statement of interest and this is the last line inside an if statement, an extra no-op type statement is required to place the temporary breakpoint on.
In this case I use:
If someCondition >0 Then
doSomething
Space (1) 'Dummy line to place breakpoint
End If
This returns a string containing one space, but does not assign it to anything. I use it in VBA, but it's also supported in .net
My two cents...
You can combine any series of commands onto one line with colons:
If False Then : End If
Select Case False : Case Else : End Select
I've also made it into a sub. Then it gets a recognizable name of own:
'Definition...
Public Sub noop () 'Or Private, Protected, etc.
End Sub
'Usage...
Sub Main()
If sometest Then
noop
Else
MsgBox "test is false"
End If
End Sub
Very strange question, you could place a BreakPoint about anywhere in the code. But here are some useless lines :
Do While False
Loop
While False
End While
Even the following :
Dim hello = Nothing
Or this :
Format("", "")
A no-op statement is also useful as an aid to document code nicely and make it more easily understandable. You could for example put in a statement like A = A.
For example:
If MyNumber => 100 then A = A
Else:
I know this is an old query, but for what it is worth, my preferred solution to the original question is
Debug.Assert (vbTrue)
If you wanted, you could use a variable instead of vbTrue and then enable/disable all breakpoints in your code by changing one variable
Dim bDisableBreakpoints as Boolean: bDisableBreakpoints = vbTrue
'your code here
Debug.Assert (bDisableBreakpoints)
'rest of your code
Simply change bDisableBreakpoints to vbFalse and the breakpoints will be set wherever you have used Debug.Assert
My personal favorite is
dim b=1
Then I put a breakpoint there.

Is there a DocumentAfterPrint event in MS Word?

I am working on a project where I need to return a word document back to a certain state after it is printed. I have found a DocumentBeforePrint event but I cannot find a DocumentAfterPrint event.
Is it poorly documented or is there some other workaround that exists?
Here is one workaround based on subroutine names. I do not believe there is a specific DocumentAfterPrint event like you desire. Here's the code:
Sub FilePrint()
'To intercept File > Print and CTRL-P'
MyPrintSub
End Sub
Sub FilePrintDefault()
'To intercept the Standard toolbar button'
MyPrintSub
End Sub
Sub MyPrintSub()
Dialogs(wdDialogFilePrint).Show
'Your code here, e.g:'
MsgBox "I am done printing."
End Sub
UPDATE: Please note the gotchas in Will Rickards' answer below.
Looking at the application events I don't see it.
I don't see it in the document events either.
Please note on the workaround provided above, that is using the FilePrint and FilePrintDefault methods, you should read this site. These methods replace the built-in function. So you actually need to add code in there or have it generated for you to get word to actually print.
Also background printing can cause your code to execute before it has finished printing. If you really must run something after it has printed you'll need to disable background printing.
I don't believe any of the suggested work-arounds will work in Word 2010. However, I have had success by using the application.onTime() method at the end of the documentBeforePrint event to cause another procedure to be executed a few seconds later.