Find selected words from Word document - vba

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.

Related

Autocorrect Word 2010 (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

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.

VBA: Is it possible to use .MoveStartUntil with the cset is a word?

Is it possible to use .MoveStartUntil with the Cset is equal to a word? Like for example,
With Selection
.MoveStartUntil cset:="Why", count:=wdBackward
End With
The problem is that it only go backward until any of the letters in the word "Why" is encountered so if it encounter other words that has a "W", "h" or "y" in it, it stop there. Is there a way to make it move to start until it reaches the specific word and not by the letters of the given word? Thank you in advance.
Programming documentation is like a statue law. It says only what it means.
Moves the start position of the specified range or selection until one of the specified characters is found in the document. If the movement is backward through the document, the range or selection is expanded.
Any observed behaviour, not documented, is not to be used.
It's is a contract. Follow the rules and your program should work forever.

Using VBA in Word, how can I find a specific piece of text and continue numbering in an outline

Using VBA in Microsoft Word, how can I automatically search for a specific piece of text, remove that piece of text and make Word continue a previous list. I can record a macro of the action of clicking the list button in Word, and it gives me some code involving Selection.Range.ListFormat.ApplyListTemplateWithLevel. I would like to be able to figure out how to find a piece of code and then automatically continue the previous list.
Here's what I have before the code starts:
First sentence
Second sentence
Third sentence
Fourth sentence
*/R*Fifth sentence
Here's what I want to have after the code finishes:
First sentence
Second sentence
Third sentence
Fourth sentence
Fifth sentence
Can you provide some more context to why you're looking to do this? This code will accomplish what you asked for (as long as the list already exists, and */R* is inside the list), but I imagine there's a better way.
With Selection.Find
.Text = "*/R*"
.Wrap = wdFindContinue
End With
If Selection.Find.Execute = True Then
Selection.TypeParagraph
End If

How can I find the number of highlighted chunks of text in an MS Word Document

I am writing a macro in MS Word which should find all highlighted text in a document and perform some action on each. I am planning a loop to do the search and manipulation part and have no problem with this portion of the code.
But I don't know how to find how many iterations I'm going to need. Is there a way to determine the number of highlights in VBA?
Many thanks in advance.
With ActiveDocument.Range.Find
.Highlight = True
While .Execute
Debug.Print .Parent.Text
Wend
End With
There's no need to calculate the number of matches up-front. You can execute the search in a loop and it will stop once there are no more matches.
Make sure you apply the search to the right part of the document.I used ActiveDocument.Range, but any Range object will do. Maybe something more specific is better for your case.
Also, check out the many other properties of the Find object and set them to sensible values, this is better than going with the defaults (nobody can remember all defaults for all options, plus the Find object might already be set up by some earlier search).