VBA Macro for MS Word to Ignore Spelling Errors in Selected Block of Text - vba

VBA Noob here. I take my python programming notes in a word document since I can import images into it and align/format text quickly. Any code pasted into this document comes up as a spelling error. I'm trying to find a way to ignore spelling errors within a selected text area so I don't have to deal with ignoring each spelling error line of code individually. I don't want to turn off spell check in the document.
Ideally, I'd able to write a macro that read:
Selection.ShowSpellingErrors = True
but ShowSpellingErrors() can only be used with ActiveDocument. I was able to a record a macro that ignored spelling errors with:
Selection.LanguageID = wdEnglishUS
Selection.NoProofing = True
However, any new text I type into this also doesn't get proofed, which is something I don't want. I want to be able to write new text and see any errors I make. Thanks for any help!

Not a VBA Macro, but I think this answer may be relevant to your problem anyway.
Try creating a style for code which does not include spell check. Anything with this style does not get spell checked, while the rest of the document does. Sometimes I find the code shows the red underline, but if you run spell check it should just disappear without needing to be 'fixed'.
Create a new style, in the modify formatting dialog, go to Format > Language:
Tick the 'Do not check spelling or grammar' checkbox:
Highlight your code and use the new style. Any text not in this style will still be spellchecked:

Related

macro to return active document format for IF/Then statement

I am creating a document template for a report for my staff to use and I have a command button at the bottom that will delete all of the command buttons in the report and protect it as read only to close out the report.
I don't want someone accidentally making these changes to the template if they happen to open that instead of a new document based on it.
So I would like a string of code that checks the active document, if it is .dotm I want it to display a message box and exit. if it is a .docx I want it to continue with the rest of the code I have written.
I have been unable to return the format or use it in an IF/THEN statement. I have been unable to find anything on the net on this either. Is it impossible? or should I be checking for the file extension? If so how would I use that as a value in an IF/THEN Statement?
The document may have been based on the template, but not yet saved. In which case it would be called "Document1", etc., without a dot.
If InStr(ActiveDocument.Name,".") = 0 Then
'it is a new document, based on a template
ElseIf InStr(ActiveDocument.Name,".dotm") > 0 Then
'it is a/the template
This of course assumes that the ActiveDocument is the correct one. If they click a button in the document then this is correct, but if they use the Macros dialog then you may want to include additional checks.
I would use the following, which ignores differences in case (.dotm, .DOTm):
If InStr(UCase(ActiveDocument.Name), ".DOTM") > 0 Then
'it is a template..
Else
'it's just a document
End If
Checking ActiveDocument.AttachedTemplate.Name can also be useful, to confirm if the active-document is one based on your template.

Edit the style of all hyperlinks in a Word document

I have a word document that is littered with hyperlinks. The links themselves work fine, but for some reason, most of them are not blue anymore! All I'm really looking to do is try to find a way to use a macro or something to go through the document and add the "Hyperlink" style format to each hyperlink.
I tried to edit some macro code myself (one that changes all the links URLs), but I keep making the problem worse! I used to be good at VBScript, but it's been ages since then.
Any easy solution that doesn't involve manually changing each style?
As a side note, all of them currently are in "Normal" style, for some reason.
Try to execute this VBA script (best in debugging mode using the F8 key - have VBA and Word windows side by side so you can see what's happening):
Sub FormatLinks()
Dim H As Hyperlink
For Each H In ActiveDocument.Hyperlinks
H.Range.Select ' (A)
Selection.ClearFormatting ' (B)
H.Range.Style = ActiveDocument.Styles("Hyperlink") ' (C)
Next H
End Sub
This will
cycle through all hyperlinks in your document (A),
remove any formatting on the underlying text (B) and
assign the undrelying text to style "Hyperlink" (C)
(C) is not strictly necessary, as (B) should already sanitize your document, but maybe better to have hyperlinks really assigned to style "Hyperlink" because you may want to change the style later on.

Word 2003 vba cannot change bookmark font

The following code does not work
objDoc.Bookmarks("SomeBookMark").Range.Font.Bold = True
objDoc.Bookmarks("SomeBookMark").Range.Text = GetSomeText()
objDoc.Bookmarks("SomeBookMark").Range.Font.Bold = True
When I run the code the text that is retrieved by GetSomeText() still has the default font.
Why don't you just style the bookmarks?
I searched for this in the past and also didn't find a proper solution back then.
However, since a bookmark represents a fixed field in a text template, usually the format shouldn't change so easily.
I'm sorry to put this in the reply field, but I can't find the "comment" button :-s

Make Word's automatic spell-checking pick up dictionary changes

In MS Word it's possible to add words to a custom dictionary so they're recognized. If a word is not recognized, Word automatically puts a red squiggly line underneath it. If you add that word to the custom dictionary, this line disappears. What I'd like to do is perform this process automatically via a macro. It appears that one has to manually open the dictionary file and write the new word, as there is no method on the Word Dictionary object to add words to a given dictionary. This is no problem, except that Word doesn't automatically pick up the new word and remove the red squiggly lines underneath the newly-added word. I've even tried clearing the custom dictionaries and adding them back in, but it doesn't seem to reload the dictionary until you manually run a spell check. Sample code for this follows:
Dim x As Dictionary
Dim fname As String
fname = "C:\Users\me\AppData\Roaming\Microsoft\UProof\md.dic"
' code to add word to dictionary goes here
With CustomDictionaries
.ClearAll
.Add fname
.ActiveCustomDictionary = CustomDictionaries.Item(fname)
End With
Is there any way to make Word recognize the newly added word(s) in a custom dictionary without running the interactive spell check? It does this silently if you manually add words, but I can't seem to replicate this behavior in VBA. I'd like the red lines to go away automatically just like they do when you manually add words.
I haven't exactly solved the problem, but I think I figured out a work around. You can get a collection of Range objects which represent spelling errors using ActiveDocument.SpellingErrors. I'm going to search this collection for text matching the word I've added to the dictionary, and then set .NoProofing = True on the object. This appears to make the red lines go away, and having added the new word to the dictionary will prevent them from coming back the next time I open the document. I haven't fully tested this approach, but it looks promising.
EDIT
This approach is flawed, as additional instances of the word which are entered during the same session will have the red squigglies underneath them because they haven't been explicitly ignored and the spell check isn't yet using the updated dictionary. If you just pull up the custom dictionary dialog manually and click OK, something happens in the background to re-read the dictionary. I just can't figure out how to do this in code.

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.