Read undo history in VBA - vba

I have a fairly simple bit of VBA in Word 2003 that changes the document's font to an 'eco font' (long story), and brings up the Print dialog.
When a user hits Cancel or OK, the code does an 'undo' to revert the change.
The problem is, sometimes when I press "OK" to print the document, two actions need to be undone ('font change', and 'update fields'). I can't seem to predict when this will happen.
Is there any way of reading the last item in Word's undo buffer? That way I can just keep pressing undo until the font change has been completed.
Edit:
Final code (cut down):
ActiveDocument.Range.Bookmarks.Add ("_tempEcoUndoStart_")
ActiveDocument.Content.Font.Name = "Nanonymus Eco Sans"
Dialogs(wdDialogFilePrint).Show
While ActiveDocument.Bookmarks.Exists("_tempEcoUndoStart_")
ActiveDocument.Undo
Wend

You can use a trick to have a 'transactional'-like undo in Word: At the beginning of the macro place a special bookmark on your entire document. You should remove this bookmark again when you are done with your macro. Now, when calling the Undo command, repeat the undo while there is your special bookmark in the document.
The following related question has the details:
Can I create an undo transaction in Word or Excel? (VSTO)

I have not tested, but perhaps you can use UndoClear before your actions, and a count on undo after?
ActiveDocument.UndoClear
ActiveDocument.Undo 2

Related

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

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

Run macro at key press - Space key

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.

Recheck Document For Spellings Not Same as VBA Code

I want to recheck the spellings in a document after adding a dictionary. The problem is the following code (mostly from the macro recorder)
CustomDictionaries.Add FileName:="c:\test_dictionary.dic"
Application.ResetIgnoreAll
ActiveDocument.Range.SpellingChecked = False
ActiveDocument.Range.GrammarChecked = False
does not produce the same results as from the Word 2007's Word Options|Proofing|Recheck Document button. The button does recheck the document and you can see the newly added words get removed as misspellings. The code does not have any noticable affect.
What am I overlooking?
I've had to do the same thing for a project, an this workaround made it happen for me:
'spellcheck the document
ActiveDocument.Range.LanguageID = wdFrenchHaiti
ActiveDocument.Range.LanguageID = wdEnglishUS
When you change the language, Word rechecks the range for spelling errors.
This is the way Word works. Adding a CustomDictionary will not trigger it to be used right away, there are only a few way ways to trigger it. One of which is the Proofing dialog (i.e. you don't have to click "Recheck Document", you just need to click "OK" and it will recheck). Another trigger is to manually type in text and then a seperator (like a space or paragraph). Yet another trigger is the Spell Check dialog. Unfortunately, there don't seem to be any really good options I can see.
But here's a bad option, which I haven't tried. (Note: SendKeys doesn't work well on Vista/7, there is a replacement out there). After you've added your custom dictionary, bring up the proofing dialog and then programmatically click OK. Again, I haven't tried it really, so I'm not sure if this will produce the desired results.

vb.net word document

I can add a bullet in word document through vb.net by using applybulletDefault() method but how can i remove it in next line??
Selection.Range.ListFormat.RemoveNumbers NumberType:=wdNumberParagraph will set you back to a normal paragraph.
When I'm writing VB code write to a Word Document I often use Record Macro in Word to do the actions as a user would, Stop Macro recording, Edit the macro, copy and paste into VB and massage we required.