I cannot execute a find in VBA for word to find "If..." Word doesn't seem to like finding the "If." part. Any ideas?
Sub Macro2()
Selection.Find.ClearFormatting
Selection.Find.Highlight = False
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "If..."
.Replacement.Text = "If..."
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Chances are Word replaced the three periods with the single character ellipsis.
As a bonus, the ellipsis should have a space before and after it, it can be a half or thin space, but certainly a non breaking space so it won't be forced onto a new line.
Related
I'm trying to make sure the dates stay together and not break across lines.
The replacement text should have two non breaking spaces, one between \1 April, and one between April \2.
Sub SarahDates()
‘ Supposed to put a non breaking space between day, month and year.
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = “([0-9]) April ([0-9])”
.Replacement.Text = “\1 April \2”
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
What is the correct text in .Replacement.Text so that non-breaking spaces are shown in macro.
the following macro will reformat all your dates in one go.
Sub SarahDates()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(<[0-9]{1,2}) ([JFMASOND][abceghilmnoprstuvy]{2,8}) ([12][0-9]{3}>)"
.Replacement.Text = "\1^s\2^s\3"
.Forward = True
.Format = False
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
I'm trying to create a macro which finds a string and makes sure that there are always 3 line breaks after the string.
If Chapter One is found, there should always be 3 line breaks after it.
I need to loop through this until this is the case (so if there's 7 line breaks after "Chapter One" it needs to loop through until there are 3.
Unfortunately i'm having difficulty inserting the loop to remove more than 3 consecutive line breaks
Sub ChapterLineBreaks()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
"Heading 1,Chapter Heading")
With Selection.Find
.Text = "Chapter One^p^p^p^p"
.Replacement.Text = "Chapter One^p^p^p"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Expected:
Chapter One
Text should then start here
You may need to use wildcards like this:
Sub ChapterLineBreaks()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
"Heading 1,Chapter Heading")
With Selection.Find
.text = "(Chapter One)([^13]#)([!^13])"
.Replacement.text = "\1^p^p^p\3"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
You will find good info here, here, and here. What I did was use the wildcards to define a regular expression: "Chapter One{1 or more line breaks}Other text". I then replaced the line breaks with exactly 3 line breaks.
in a file MS WORD I wish to select a "/" and make bold all characters till the following ^p. The following code works well only for the first occurrence. In the file I have many occurrences and I am not able to apply this in the whole file. I tried several times with "for...next" and others, unfortuntely without success.
Many thanks for your help! Gianluca
Sub bold_title()
Set myRange = ActiveDocument.Content
Selection.Find.ClearFormatting
With Selection.Find
.Text = "/"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^p"
.Forward = True
.Wrap = wdFindAsk
.Format = False
End With
Selection.Find.Execute
Selection.Font.Bold = True
End Sub
The trick is to "collapse" the Selection- think of it like pressing the right-arrow key - so that the next search starts after the "found" text:
Selection.Collapse wdCollapseEnd
Usually, a Do While...Loop construct is used in order to repeat the Find until the end of the document. You'll find lots and lots of examples if you search here and elsewhere on the Internet.
For this to work successfully, make sure you set Wrap to wdFindStop (and not wdFindContinue as is currently in the code shown).
I want to record a macro such that it will replace all 'line break' symbol after the current cursor position to a blank space.
For example if the text is:
My name is Sumit. I
love
Stackoverflow
Now if the cursor is just next to 'love' and I press the macro button it should come like this:
My name is Sumit. I
love Stackoverflow
I searched internet and wrote this (but this is not working)
Sub Replace()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
I would guess that you'd want to change
.Wrap = wdFindContinue
to
.Wrap = wdFindStop
and
.MatchWildcards = True
to
.MatchWildcards = False
The first change should prevent the search from going back around and replacing content before the cursor and the second change turns off Wildcards (^p is not a wildcard character).
But it might help to know exactly how it's not working, if those changes aren't sufficient.
in my word file there are hundrens of paragraph which like the format below. There is a single letter Y here. It can be other letter except "A","T","C","G". I want to remove the white space in it then create a new line.
AAATGGGCCC CACAGAAGTG AGAATGGGTG AAGTCAGAAT TCCTGGTAAT GAAGTGCTTG
AACTTGGATT CCTCCCGACA TGTGCAGTAC AATGAGATGA TTTTCTCCTT AATGAGATTA
GGAAATTCTA TTAGCGCTCC CAGCTGCTGA CCCGATTCCA TGAGGCTGAG GCTCCAGGGC
TGAACCTGCC TGGTT
Y
AGTGTTCCTG GAAACTAGAC ACCCCACCCT TCAGATGGGC CAGGGCCTCC CCAGCTCTAC
CTAAAGCTGT GGTCTGCCCC CAGGGGTGCC CAGTTTCCTC CCTTCACCCT GTGCTCCAGA
GGAGTGTGGG GCCCTGGGCA TTCTGCAGTG TACCCCAGGA TCCTCACTCC TTCCTGCTTA
The new line's format is
AAATGGGCCCCACAGAAGTGAGAATGGGTGAAGTCAGAATTCCTGGTAATGAAGTGCTTGAACTTGGATTCCTCCCGACATGTGCAGTACAATGAGATGATTTTCTCCTTAATGAGATTAGGAAATTCTATTAGCGCTCCCAGCTGCTGACCCGATTCCATGAGGCTGAGGCTCCAGGGCTGAACCTGCCTGGTT[Y]AGTGTTCCTGGAAACTAGACACCCCACCCTTCAGATGGGCCAGGGCCTCCCCAGCTCTACCTAAAGCTGTGGTCTGCCCCCAGGGGTGCCCAGTTTCCTCCCTTCACCCTGTGCTCCAGAGGAGTGTGGGGCCCTGGGCATTCTGCAGTGTACCCCAGGATCCTCACTCCTTCCTGCTTA
Notice Y becomes [Y].
The final result will be saved as a text file. Thanks for help.
You don't need to write a program. The “Replace” tool is sufficient for this:
Replace Y with [Y] (EDIT: see the comments below, because it's a little more complex than that indeed)
Replace ^w with nothing (^w means whitespace)
Replace ^p with nothing (^p means paragraph markers)
EDIT: if you need a macro, just do the above once while recording a macro.
EDIT: by applying the method discussed in the comments, I get the following VBA macro:
Sub ProcessATCG()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "([!ACGT^13^32])"
.Replacement.Text = "[\1]"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "[^13^32]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub