wdReplaceAll empty replacement text doesn't work - vba

I have taken over a macro on a word document which has the following function:
Private Sub ConvertTildeToBullets()
With Selection.Find
.Text = "~ "
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.style = ActiveDocument.Styles("ieMR table bullet 1")
.Replacement.Text = " "
.Execute Replace:=wdReplaceAll
End With
End Sub
So it converts a sentence starting with a tilde (~) to bullet style. The program that outputs the original word document puts a space after the tilde and at the bottom of the function it replaces the tilde and space with a space. However my boses don't want that space and as soon as it is changed to .Replacement.Text = "" it doesn't replace any of the tildes.
How to I run wdReplaceAll with an empty replacement text?

Try with changing three lines of your code:
....
.Text = "(~ )(*)"
....
.MatchWildcards = True
....
.Replacement.Text = "\2"
....
It works for quick test & try I just made. Hope it will work for you as well.

Related

Automate Word Macro code to replace string of operators like "^=" with a hyphen?

I am trying to find all instances of strings "^=" or "=^" or "^+" and replace them by " - " (space-hyphen-space). So far I have tried using the Regex code from another one of the stackoverflow posts but I am really struggling and trying to match expressions. Please find my attempts and share how I can write VB macro code to automate the process. Thank you for your help!
With Selection.Find
.Text = "^+"
.Replacement.Text = " -- "
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
You probably don't need a macro for that. Try a wildcard Find/Replace where:
Find = [^94=][^94=+]
Replace = ^32-^32
To lengthen the hyphenation, you could use and of:
Replace = ^32--^32
Replace = ^32^=^32; or
Replace = ^32^+^32,
the latter two of which change the hyphen to an en-dash or em-dash, respectively.
If you want to keep the hyphenation together with whatever precedes it, change the first ^32 to ^s.
Or, as a macro:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[^94=][^94=+]"
.Replacement.Text = " - "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
As with manual wildcard Find/Replace you could lengthen the hyphenation with any of:
.Replacement.Text = " -- "
.Replacement.Text = " ^= "
.Replacement.Text = " ^+ "
Again, if you want to keep the hyphenation together with whatever precedes it, change the first replacement space in the code to ^s.

Search for text and add text after

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

Word-macro to replace all instances of a particular string in current line

I'm trying to create a macro in word to find particular cells in a table and replace certain strings there. For example:
(word1 or word2 or word3).ab,ti.
Should be replaced by
word1[TIAB] or word2[TIAB] or word3[TIAB]
So, what I've done so far is a simple replaceAll to delete the initial brackets and replace the suffix ").ab,ti." by "[TIAB]. But that doesn't append the endings to word1 and word2, of course.
Sub Makro6()
'
' Makro6 Makro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ".ab,ti."
.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
I guess what I need is to embed a loop in the replaceAll sub, which runs from the first position to the end of the current row and replaces the string " or " by "[TIAB] or ". However, I'm completely new to VBA so I somehow can't figure out how to do this. Any suggestions?
Thanks for your help!
Leni
This code performs the actions you want:
Sub Makro6()
Dim maxCount, curCount As Integer
maxCount = 3
curCount = 0
Do
curCount = curCount + 1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = Chr(40) & "word" & curCount & Chr(41) & ".ab,ti."
.Replacement.Text = "word" & curCount & "[TIAB]"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
Loop While (curCount < maxCount)
End Sub
Note that I had to rely on ASCII codes (Chr(40) & Chr(41)) to account for the parenthesis because surprisingly (at least, for me), the macro wasn't able to find the target string. I did some tests and the problem only happens with parenthesis followed by another character (?!).

Cannot get word to find "If..."

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.

Eliminate space in word file

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