Eliminate space in word file - vba

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

Related

Loop through find and replace for line breaks

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.

Word VBA Find text of a certain color and insert a space ahead of it

I am new to VBA in MS Word, and only somewhat comfortable with VBA in Excel.
I am trying to process a Word file that has some special information attached to many words. The information is conveyed by changing the color, size, and position of the text, but there is no space between the word and the information.
In order to do some other processing, I need to add a space between the information text and the actual word:
Using VBA I am able to find the informational text by color, using a slight variation on the method described here. I can replace it with "Test". But what I want to do is replace it with a space plus the infromational text. Or perhaps just magically insert a space right before the informational text.
Ideas?
This code:
Sub ChangeColorWithReplace()
Selection.Find.ClearFormatting
Selection.Find.Font.Color = RGB(120, 48, 191)
Selection.Find.Replacement.ClearFormatting
'Selection.Find.Replacement.Font.Color = wdColorRed
With Selection.Find
.Text = ""
.Replacement.Text = "test"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Produces this result:
A neat thing with Word's Find/Replace functionality is that you can use special characters to perform actions like this. Click the "More" button, the "Special" to see a list.
In this case, what you want is to retain the "Find text" in your replacement, which is the character set: ^& which I've added to your sample code, below.
Sub ChangeColorWithReplace()
Selection.Find.ClearFormatting
Selection.Find.Font.Color = RGB(120, 48, 191)
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "^&test"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Microsoft Word 2013 Macro - Adding Content Controls to Multiple Items

What I need to do is add a Rich Text Content Control to all "xx"'s in a large word document.
I saw a similar thread on highlighting multiple instances. I based the solution that I've tried so far on this:
Options.DefaultHighlightColorIndex = wdYellow
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "target1"
.Replacement.Text = "target1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Now just testing in word to see what command it uses to add the content controls individually I found this command:
Selection.Range.ContentControls.Add (wdContentControlRichText)
What I came up with was:
Sub StandardLanguageVariableSearch()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Range.ContentControls.Add (wdContentControlRichText)
With Selection.Find
.Text = "xx"
.Replacement.Text = "xx"
.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
Swapping out Selection.Range with Selection.Find simply resulted in a "method or data field not found" or something like that. I think Range refers to the currently highlighted range of characters, whereas find refers to whatever the with block... well, finds.
Whatever find is doesn't appear to have the ability to throw some content controls onto it.
You were quite close:
Sub StandardLanguageVariableSearch()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "xx"
'.Replacement.Text = "xx"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
'Selection.Text = "" 'uncomment if you want to remove xx
Selection.Range.ContentControls.Add (wdContentControlRichText)
Loop
End Sub

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

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.