MSWord Search in a macro - vba

When I search without specifying the range, the search starts at the active page:
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "house"
.Replacement.Text = "apartment"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = True
End With
Selection.Find.Execute
If I use a range, it always starts at the beginning of the range
ActiveDocument.StoryRanges(wdMainTextStory).select
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "house"
.Replacement.Text = "apartment"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = True
End With
Selection.Find.Execute
Anyone know a way to start the search in the active page while using a range?
By active page, I mean the page with the cursor.
Also, I need to search footnotes and endnotes. That's the reason for using storyranges, which seems to force the search to start at the beginning of the document.

For example:
Dim findRange As Range
Set findRange = Selection.Range
With findRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "house"
.Replacement.Text = "apartment"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = True
.Execute
End With
Please note that as you have set .Wrap = wdFindContinue it makes no difference where Find starts as it will search the entire document anyway.

Related

How do I limit the find and replace procedure to execute only on certain pages?

I am using Microsoft Visual Basic on Word. I want to limit the find and replace function to operate only on certain pages of the document. How do I do it?
As of now, it will execute it the whole document which is not preferred.
Sub X_entity()
'
' Replaces lower and greater than symbols to html entity
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ">"
.Replacement.Text = ">"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "<"
.Replacement.Text = "<"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Instead of using Selection (which is poor practice anyway), you should point to the range of each page in turn, using Find/Replace on the designated range only. For example:
Sub ProcessPages()
Application.ScreenUpdating = False
Dim i As Long, ArrPgs()
' Define the pages to process
ArrPgs = Array("7", "4", "3", "2")
For i = 0 To UBound(ArrPgs)
' Process the defined pages
With ActiveDocument.Range.GoTo(What:=wdGoToPage, Name:=ArrPgs(i)).GoTo(What:=wdGoToBookmark, Name:="\page").Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWildcards = False
.Text = ">"
.Replacement.Text = ">"
.Execute Replace:=wdReplaceAll
.Text = "<"
.Replacement.Text = "<"
.Execute Replace:=wdReplaceAll
End With
Next i
Application.ScreenUpdating = True
End Sub
With the above code, the pages are processed in reverse order in case the Find/Replace messes with the pagination.

How can I make this code better / quicker?

i have a collection of working codes, that removes words from a word table, and reformats the size of the table. I know i'm repeating, in the code, so i'd like to make this more streamlined, and in the hope that by doing this, the code will become a little quicker to run.
I'm a complete noob to vba, so I've scoured the web, and ad-hocced the working code together. The table im working on is 150 rows, by 10 columns, but the rows will change on a weekly basis.
Option Explicit
Sub alterRota()
Dim manager
manager = "Manager"
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = manager
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim bar
bar = "Bar"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = bar
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim kitchen
kitchen = "Kitchen"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = kitchen
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim lead
lead = "Lead"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = lead
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim cleaning
cleaning = "Cleaning"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = cleaning
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim floor
floor = "Floor"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = floor
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim timeoff
timeoff = "Time Off"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = timeoff
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim startoff
startoff = "04:00 - 00:00"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = startoff
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim endoff
endoff = "00:00 - 04:00"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = endoff
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim linebreaks
linebreaks = "^p"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = linebreaks
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim employee
employee = "Employee"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = employee
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Dim tr As Row
For Each tr In ActiveDocument.Tables(1).Rows
tr.HeightRule = wdRowHeightExactly
tr.Height = 9
Next tr
End Sub
Your code can be re-written as follows...
Option Explicit
Sub alterRota()
Dim searchFor As Variant
searchFor = Array("Manager", "Bar", "Kitchen", . . . ) 'add your other words accordingly
Dim i As Long
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
For i = LBound(searchFor) To UBound(searchFor)
.Text = searchFor(i)
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
Next i
End With
Dim tr As Row
For Each tr In ActiveDocument.Tables(1).Rows
tr.HeightRule = wdRowHeightExactly
tr.Height = 9
Next tr
End Sub

How to select one by one particular string in word vba script?

I need selection particular content each paragraph in ms word 2013. I try to select content using by vba script..
Sub RepalaceStrong()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "<Strong"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Extend
With Selection.Find
.Text = "</Strong>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub
But i tried this code, i can't select one by one text.
Input:
In general, a vector field is a function whose domain is a set of points in <Strong> a vector field is </Strong> a vector field is <Strong>function</Strong> whose domain is a set of points
>In general, a vector field is a function whose <Strong>domain</Strong> is a set of points
Is it possible to select one by one all strong elements...
You need to specify the correct font formatting in your find operation (note the .Font.Bold = True part below):
With Selection.Find
.ClearFormatting
.Font.Bold = True
.Replacement.ClearFormatting
.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Then, of course, it depends what you want to do with the bold text. Currently the code above just configures the Find object to search for bold text.
It's not strong you're looking for, it's Font.Bold
Option Explicit
Sub FindBold()
Dim myDoc As Document
Set myDoc = ThisDocument
Dim searchRange As Range
Dim foundRange As Range
Set searchRange = myDoc.Range(0, myDoc.Range.End)
With searchRange.Find
.ClearFormatting
.Forward = True
.Font.Bold = True
.Execute
Do While .Found
Set foundRange = searchRange
foundRange.Select
foundRange.Collapse direction:=wdCollapseEnd
MsgBox "Found bold text."
.Execute
Loop
End With
End Sub
My ans:
Sub RepalaceStrong()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "<Strong"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Extend
With Selection.Find
.Text = "</Strong>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Collapse Direction:=wdCollapseEnd
End Sub
I got it ....

Edit and wrap search result in parenthesis

I want to find all occurences of the string "no." + 1-2 integers between 1 and 9.
Then to delete the "no." and wrap the integer/s between paranthesis: "(4)" or "(67)". E.g. "no. 34" should become "(34)".
I seem to have multiple issues (Word 2010):
The code only substitutes one integer. How do I make it find both one or two integers?
How do I make the .Replacement.Text contain the numbers but not the word "no." (I've just put in XXXXX this far).
My code does add paranthesis, but at the beginning and end of the active document. How do I make it wrap the numbers instead?
With Selection.Find
.Text = "n[or]. [1-9]"
.Replacement.Text = "XXXXX"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.InsertBefore ("(")
Selection.InsertAfter (")")
Selection.Find.Execute Replace:=wdReplaceAll
Try with following solution:
With Selection.Find
.Text = "(No.)( )([1-9]{1;2})"
.Replacement.Text = "(\3)"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Cleaning up messy paragraph breaks in a dictionary in MS Word

I have a dictionary in MS Word format which I'd like to have cleaned from any paragraph breaks within dictionary entries, and keep only paragraph breaks that separate any two dictionary entries. This is how the layout of the dictionary looks now:
First picture http://img43.imageshack.us/img43/6476/snapshotpr.jpg
I'd need a macro or a regular expression that would first remove all the paragraph breaks, from the document, which would produce this layout:
Second picture http://img824.imageshack.us/img824/5219/snapshot1i.jpg
and then in the next step would add paragraph breaks only before the dictionary entries, which means only before bold phrases followed by the phonetic transcription in square brackets, to get this layout:
Third picture http://img849.imageshack.us/img849/2003/snapshot2qf.jpg
I used this site to help me with the paragraph markers.
Again, I recorded a macro with something did manually with 4 find/replace (two steps were used to make sure that a word followed by a square bracket was matched). Here's the macro:
Sub Separator()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = "\["
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[a-z\-]# \["
.Replacement.Text = "^p^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Bold = False
.Italic = False
End With
With Selection.Find
.Text = "\["
.Replacement.Text = "["
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Let me know if there's anything to tweak and I'll try to change it :)
EDIT: Part added for hyphens.