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.
Related
I'm trying to code some routines in VBA for Word that use regular expression to search for certain elements in documents. I got this code (please disregard for now that it's coded using Selection, it's just a mock-up to see if the workflow is sound):
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
End With
With Selection.Find
.Text = "[0-9]{5,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
If Selection.Find.Found = True Then
Dialogs(wdDialogEditReplace).Show
End If
and it works in the sense that it does find 5+ digit numbers. However I would like the user to have the ability to edit the found number or any other part of text while the box is still active, to continue to search once the editions are finished. I'd be grateful for any tips on how to solve this issue.
The closest you will get is to use your code to set the Find parameters and then execute the Find dialog button, as shown below.
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[0-9]{5,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Application.CommandBars.ExecuteMso ("FindDialog")
This will leave the user to press Find Next to get the first match but will allow the dialog to work as normal, enabling edits.
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 have a document that contains the following text format that occurs throughout:
5:43-64
I want to search and replace so that the text reads like so:
5:43-64 indicates:
Fortunately, the - only appears in this type of text. The numbers change in each instance. So I don´t think I have to worry about some complicated search pattern. I can just search for the - character.
I want to then take whatever is after the the - and then save it as a variable then insert the text indicates afterward. I need this to loop through the whole document making these changes at any occurrence of the -.
Here is the code that I have up to this point that kind of half works:
Sub placeWordAfterDash()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "-"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If Selection.Find.Execute Then
Dim selectedString As String
Selection.Select
selectedString = Selection.Next(Unit:=wdWord, Count:=1).Text
Selection.Text = "-" & selectedString & " indicates: "
End If
End Sub
This code only makes the change in one instance and also leaves me with:
5:43-64 indicates: 64
Which isn´t quite what I want.
You don't need to use vba to do this find-replace, you can do it with a simple wildcard find-replace.
press CTRL+H, find (-<*>), Replace with \1 indicates: (make sure to check "Use Wildcards")
If you do want to use vba:
Selection.Find.ClearFormatting
With Selection.Find
.Text = "(-<*>)"
.Replacement.Text = "\1 indicates:"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
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.
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