Autocorrect Word 2010 (VBA) - vba

For my problem I tried different things and none worked: I am flexible about the way to actually solve it, so I think it's best to begin with the purpose.
I get long word files and I need to do some quality checks and proofreading. There are a lot of words in the file that are not capitalized correctly. I want to get this in automatic (no a normal grammar software/add-on won't do)
I have all the entries in my auto correct file, in word files (quite huge better not use them) and in an excel spreadsheet (wrong entry - correct entry).
Refreshing the document doesn't do the trick. If I pick a word from the list and select it and press the space-bar then it gets changed so I "wrote" a Macro for that
Sub Try()
For Each sentence In ActiveDocument.StoryRanges
For Each w In sentence.Words
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.TypeText Text:=" "
Selection.TypeBackspace
Next
Next
End Sub
The thing "worked" i.e. it runs and you can see the cursor speeding through the screen but no words get changed. So I am thinking that Autocorrect get's somehow disabled while Macros are running.. Anyone has a hint? ideally knows a way around it?
Worst case I thought of putting the text in an excel file and writing a macro that for each line of text checks each word against my original list and "if match then substitute" but it doesn't seem too easy (at least for me)
Cheers
I hope that I could explain myself

This Pseudo Code / Outline should get you started:
In Word you can create an instance of Excel and open your spreadsheet:
https://stackoverflow.com/a/24196120/3451115
You can then Loop through your list of WrongWords:
https://stackoverflow.com/a/1463308/3451115
For each WrongWord do a Find and Replace:
VBA to find and replace a text in MS WORD 2010

Related

Find selected words from Word document

I'm trying to find selected words from Word document using Word VBA but I'm getting stuck e.g. "25to30" anysuggestions below are the code used in wordvba
Selection.find.Execute FindText:="([0-9]{1,})([To])([0-9]{1,})", MatchWildcards:=False, Forward:=True
Dpedending on whether there is a space either side of 'To':
FindText:="(<[0-9]#)(To)([0-9]#>)
or
FindText:="(<[0-9]#)( To )([0-9]#>)
And, unless you're going to do something with the individual components of what you find, there's no point having the parentheses.

In VBA Word, how to check if a paragraph contains any bold?

I've been teaching myself to use macros and VBA in work. I'm not code-minded so bear with me.
I need to be able to find paragraphs that are wholly bold (such as a subheading), but I've been struggling to figure it out or find any help online. I've seen similar questions but the responses are too complicated for me to really know what I'm replicating, despite trying to use parts that seem relevant.
My end goal is to, after finding a wholly bold paragraph, delete the character immediately following it (usually an empty line).
I did find something that suggested 0 being regular, -1 being entirely bold and 9999999 being partially bold, but I've been unable to get anything like that to work as I couldn't tell what code was relevant to me. I'd really appreciate any help, thanks.
The help that can be offered at this stage of your endeavour doesn't go beyond showing you how to help yourself.
The red line of your programming should be to find bold text in your document and determine if it is a whole paragraph. If it is then delete the blank line following it (and then continue searching for the next such paragraph?), and if it isn't, do nothing (and continue searching for the next bold text?).
At the heart of this plan is VBA's Find method. You can look it up on MSDN and/or you can do the search manually while recording your key strokes with the macro recorder. Either of these ways will be very confusing, perhaps the latter more than the former although it promises to be the shorter way to your target. Actually, much depends upon how you define your target. If you want to learn VBA the questions you meet will be challenges you can master. Others have done it before you.
I prefer the method that you were first considering. Create a Word file, and make sure that some text is bold and some is not. Highlight some plain text, or some bold text, or a range that includes plain and bold, and run the following routine.
Sub WhatTheHeck()
If Selection.Font.Bold = True Then MsgBox "All is Bold"
If Selection.Font.Bold = False Then MsgBox "None is Bold"
If Selection.Font.Bold = wdUndefined Then MsgBox "Some is Bold"
End Sub
When you get the hang of that, learn (or ask again) some of the many ways to select text programmatically (with the .Select method), and you'll have everything you need.
(For example, as somebody else suggested, you will be able to do a .Find with any complex criteria, combined with a .Select, and then call WhatTheHeck()).
NOTE: It is worth mentioning that many true/false properties in VBA can be true, false, or wdUndefined, and possibly other values. In general, wdUndefined means 'a combination of true and false'. I know that the Selection.Font.Hidden property can also be wdUndefined, meaning a combination of both.

MS Word Macro to replace all hyperlinks with a single word

I prepare a MS Word file with a large amount of news articles, each followed by a full, long clickable hyperlink (e.g. http://www.newssite.com/this-is-the-name-of-the-article.html).
I need to replace all the hyperlinks in the file with a single word: "Link", while keeping their url adresses and click functionality. I can use MS Word "change hyperlink" dialogue to do that, but doing it by hand takes incredible amount of time.
The hyperlinks, however, are not always formatted as 'hyperlink' style. Is there any condition for MS Word Macro replacement that will lookup hyperlinks by their functionality, not by their style or text?
Despite this task seems to be quite common, I could not find anything like it in the web.
Since you have the hyperlinks already created. It should just require looping through the documents hyperlinks collection and changing the 'TextToDisplay' property. This should get you started:
Public Sub ChangeHyperlinksText()
Dim hlink As Hyperlink
For Each hlink In ThisDocument.Hyperlinks
hlink.TextToDisplay = "Link"
Next hlink
End Sub

Copy ComboBox from a Word Document to another Word Document with vba

I have a combo box (combo1) in a word document and I would like to copy it to another word document. (end game will be looping through 100's of docs).
I can't for the life of me work out how to select and/or copy the combo box, although its easy enough to do outside of vba.
So far I've tried turning it into a bookmark, seems to select ok, but won't copy.
ActiveDocument.Bookmarks(combo1_bm).Select
Selection.Copy
I thought it would be able to done as an inline shape (as that is how they are added?), however again the selection appears to work, but the Copy doesn't.
ActiveDocument.InlineShapes(combo1).Select
Selection.Copy
Any ideas on what I can try next?
Cheers,
Michael
Your attempt with bookmark was quite good. You just need to extend your code a bit:
ActiveDocument.Bookmarks("combo1_bm").Range.Copy
....
Selection.Paste 'or different pasting procedure
Keep in mind that you don't need to select object before copy it. Just try doing as I show above. Moreover, don't miss quotation marks for names or use bookmark index to work with appropriate one. Copy method will copy inside range of the bookmark and keep original bookmark in place, unchanged.
This should do the trick.
Set ComboBox1Range = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("combo1_bm").Range.Start - 1, _
End:=ActiveDocument.Bookmarks("combo1_bm").Range.End)
ComboBox1Range.Expand Unit:=wdParagraph
ComboBox1Range.Copy

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.