I need to delete all of new lines if is consecutive, i know macros in excel but not in word, how can i read the value of an entire line?
I read all the lines in the doc with this:
For i = 1 To 10
Selection.MoveDown Unit:=wdLine, Count:=1
Next i
Is there other way of read each line or how to know the total lines in a word to put this in the for?
Thanks
I need to delete all of new lines if is consecutive
Each blank line is actually a paragraph, so:
Sub RemoveBlankParas()
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
If Len(para.Range.Text) = 1 Then
'only the paragraph mark, so..
para.Range.Delete
End If
Next para
End Sub
However, if there are only two consecutive blank paragraphs then using ReplaceAll is easier and quicker. Here's a recorded macro that can be tidied up:
Sub Macro2()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.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
Word's Find/Replace feature uses a primitive form of regular expressions, so the following reduces two or more consecutive paragraph marks. NB MatchWildcards = True to use regex:
Sub Macro2()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(^13)\1#"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Word regular expressions
Related
I am not familiar with VBA at all.
I want to search for text I select (rather than a given list of words or typing that text in a box), and then change its format (preferably make it bold or change its color).
I tried to change a few macros that I found.
The VBA code for this can be rather simple. For example:
Sub MakeBold()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Font.Bold = True
.Text = Selection.Text
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
For Mac macro installation & usage instructions, see: https://wordmvp.com/Mac/InstallMacro.html
This will do what you want. Copy/paste into your VB editor window.
Sub HighlightWords()
Dim Word As Range
Dim WordCollection(2) As String
Dim Words As Variant
'Define list.
'If you add or delete, change value above in Dim statement.
WordCollection(0) = "you"
WordCollection(1) = "or"
WordCollection(2) = "Word document"
'Set highlight color.
Options.DefaultHighlightColorIndex = wdYellow
'Clear existing formatting and settings in Find feature.
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
'Set highlight to replace setting.
Selection.Find.Replacement.Highlight = True
'Cycle through document and find words in collection.
'Highlight words when found.
For Each Word In ActiveDocument.Words
For Each Words In WordCollection
With Selection.Find
.Text = Words
.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
Next
Next
End Sub
Before:
After:
I already have the code to do a search and replace in Microsoft Word using a VBA macro, but at the moment, my code is copying and pasting the same function multiple times.
What I would like to do is have an dictionary of words that need to be replaced, so for example:
old_word: new_word
Then I have the VBA script to loop through and make the necessary changes in the document.
My code for now is and repeats for each new word pair.
Sub change_words()
With Selection.Find
.Text = "optimize"
.Replacement.Text = "optimise"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "utilized"
.Replacement.Text = "utilised"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Many thanks in advance
Sub change_words(ByVal findWord, byVal replaceWord)
With Selection.Find
.Text = findWord
.Replacement.Text = replaceWord
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
If you change your function to accept parameters as shown, you can call it in a loop, and (for example) pass in the values you want like so:
Dim myDict : Set myDict = CreateObject("Scripting.Dictionary")
myDict("optimize") = "optimise"
myDict("utilized") = "utilised"
For myLoop = 0 to myDict.Count - 1
change_words myDict.Keys()(myLoop), myDict.Items()(myLoop)
Next
That should allow you to reuse the code without repeating it.
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.
I have a Word document with blocks of text marked with styles called "Princple" and "BusinessRule". These blocks of text are scattered throughout the document. I would like to find and copy these blocks to a separate document in the order in which they appear. The code I am using is:
Dim SelStyle As String
'SelStyle = "Principle"
SelStyle = "BusinessRule"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Style = SelStyle
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Copy
Windows("Document1").Activate
Selection.TypeParagraph
Selection.PasteAndFormat (wdFormatPlainText)
Selection.TypeParagraph
Windows("mainDoc.docx").Activate
Selection.MoveRight Unit:=wdCharacter, Count:=1
As you see, this is a manual process: first un-comment the Principle, extract all of those, then comment Princple and uncomment BusinessRule. Not great. Is there any way to search for .Style="Principle" or .Style="BusinessRule" so I get them all in sequence? (And, secondarily, if you have a suggestion for looping through the whole document to do this, I would be additionally grateful. :-))
Thanks - Bill
Why not store the values in an array?
Sub Sample()
Dim SelStyle(1) As String
SelStyle(0) = "Principle"
SelStyle(1) = "BusinessRule"
For i = 0 To 1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Style = SelStyle(i)
'
'~~> Rest of your code
'
Next i
End Sub
fwiw, I changed my approach on this. Instead of searching for styles, I go through each paragraph in the document and see if the style is "Principle" or "BusinessRule". If true, then I copy/paste the paragraph. So not only do I get the "or", but I also get them in order.
I make a several page word document every week. I copy text from a PDF and paste it into a word document, I then format the text that I pasted.
This takes a long time and i would like to automate it.
I need a macro or some code to select specific text, then make that text bold. The specific text i need to bold is what i call a scrap code.
There are 60 different codes. For example "FIPS", or "LILL".
Something like this:
Sub A()
'
' a Macro
'
'
Dim A(3) As String
A(1) = "code1"
A(2) = "code2"
A(3) = "code3"
For i = 1 To 3
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.Font.Bold = True
.Execute FindText:=A(i), ReplaceWith:=A(i), Format:=True, _
Replace:=wdReplaceAll
End With
Next i
End Sub
HTH!
Edit
To switch dollar amounts to bold
Sub a()
'
' a Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = "$([0-9.,]{1,})"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
I would suggest recording a macro.
Then do all the modifications and formatting.
Finally, look at the code of the macro and see how it did it.
The one thing you need to figure out is how you logically want to locate the text you want to bold.
Is it a specific line? Is it at the beginning of a known word?
Once you have that answered, you can combine it with the code of the macro and automate the task.