i am trying to find all bold and then change their formatting so i am trying to do it with two loops one for find bold words and then other for changing formatting. can someone please tell me how to do it/ any help is much appreciated.
'thank you
Sub SearchBoldText()
Dim rng As Range
Set rng = ThisDocument.Range(0, 0)
With rng.Find
.ClearFormatting
.Format = True
.Font.Bold = True
While .Execute
rng.Select
rng.Collapse direction:=wdCollapseEnd
Wend
Do Until rng = 0
With Selection.Font
.Name = "Times New Roman"
.Size = 20
.Bold = True
.Color = RGB(200, 200, 0)
End With
Selection.Find.Execute
Loop
End With
End With
Set rng = Nothing
End Sub
There is no need of 2 loops for this matter. Let's say your range is A1 to F40, do as follow. Note that i didn't change the formatting but I've made the cell's background red. You could adjust to your needs.
For EXCEL :
Sub SelectBold()
Dim Rng As Range
Dim WorkRng As Range
Set WorkRng = Range("A1:F40")
For Each Rng In WorkRng
If Rng.Font.Bold Then
Rng.Interior.Color = RGB(255, 0, 0)
End If
Next
End Sub
For WORD:
Sub findBold()
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Size = 20
Selection.Find.Replacement.Font.ColorIndex = wdYellow
Selection.Find.Replacement.Font.Name = "Times New Roman"
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Related
How do you find all instances of the highlighted text in the active document and remove the highlight formatting with mark-up?
I found a macro in https://learn.microsoft.com/en-us/office/vba/api/word.find.highlight. But I want it to remove the highlight formatting with mark-up. I tried to add " ActiveDocument.TrackRevisions = True" to turn on the Track Changes but in vain.
Sub A()
Dim rngTemp As Range
Set rngTemp = ActiveDocument.Range(Start:=0, End:=0)
With rngTemp.Find
.ClearFormatting
.Highlight = True
With .Replacement
.ClearFormatting
.Highlight = False
End With
.Execute Replace:=wdReplaceAll, Forward:=True, FindText:="", _
ReplaceWith:="", Format:=True
End With
End Sub
Then I tried to record a Macro and edited it as follows:
Sub Macro1()
Selection.Find.ClearFormatting
Selection.Find.Highlight = True
With Selection.Find
.Text = ""
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Options.DefaultHighlightColorIndex = wdNoHighlight
Selection.Range.HighlightColorIndex = wdNoHighlight
Selection.Find.Execute
End Sub
The second one can only change highlighted text to no highlight with mark-up one by one. It is not convenient since I have at least 200 highlighted texts to decide whether they should be corrected in a document. How can I edit it to automatically select all highlighted text and then remove their highlights with mark-ups?
Sub FindRemoveHighlighting()
Dim findRange As Range: Set findRange = ActiveDocument.Content
ActiveDocument.TrackRevisions = True
With findRange
With .Find
.Highlight = True
.Text = ""
.Format = True
End With
Do While .Find.Execute() = True
.HighlightColorIndex = wdNoHighlight
.Collapse wdCollapseEnd
Loop
End With
End Sub
Is there a way for vba to find a string of text with a particular style and select until the style is used again? Or is there a way to find a string of text in a numbered list and select until the next number? The heading I want to find and select content under is "SUBMITTALS," but it is not always in the same numerical order in the document, but the style is always the same. Any advice would be appreciated, here is what I have tried:
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 2")
With Selection.Find
.Text = "SUBMITTALS"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
End With
Selection.Find.Execute
Selection.Collapse wdCollapseStart
Dim r1 As range
Set r1 = Selection.range
'v = Selection.range.ListFormat.ListValue
'lstring = Selection.range.ListFormat.ListString
Selection.Find.Style = ActiveDocument.Styles("Heading 2")
With Selection.Find
.Text = "SUBMITTALS"
End With
Dim r5 As range
Set r5 = Selection.Next(wdSection).Next
'If Selection.Find.Execute Then
'Selection.Collapse wdCollapseStart
'Else
'Selection.WholeStory
'Selection.Extend
'Selection.Collapse wdCollapseEnd
'End If
Dim r2 As range
Set r2 = ActiveDocument.range(r1.Start, r5.Start)
r2.Select
For example:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "SUBMITTALS"
.Style = wdStyleHeading2
.Replacement.Text = ""
.Format = True
.Forward = True
.Wrap = wdFindStop
.Execute
End With
If .Find.Found = True Then
Set Rng = .Paragraphs(1).Range
Set Rng = RngHd.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
MsgBox Rng.Text
End If
End With
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
How can I delete the text between <de> and the end-of-cell marker throughout my Word document?
I have reached the following code from my previous question. It appeared not to be working because my lines end with end-of-cell markers. So now I need to update this to delete the text between <de>and the end-of-cell marker.
I am unable to implement that in this code.
Sub FindTheDeleteToEndOfLine()
Dim searchTerm As String
Dim bFound As Boolean
searchTerm = "<de>"
Selection.HomeKey wdStory
'Basic Find settings
With Selection.Find
.Forward = True
.wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
'Execute the Find
Do
With Selection.Find
.Text = searchTerm
bFound = .Execute
If bFound Then
Selection.MoveEnd wdLine, 1
Selection.MoveEnd wdCharacter, -1
Selection.Delete
End If
End With
Loop While bFound
End Sub
Update: I worked out a new code less complicated, but again i am unale to define the arng.words in order to delete last part after <de>:
Dim arng As Range
Dim i As Long, j As Long
With Selection.Tables(1)
For i = 1 To .Rows.Count
For j = 1 To .Columns.Count
Set arng = .Cell(i, j).Range
arng.End = arng.End - 1
'Text = "<de>"
If Right(arng.Words, 1) = "<de>" Then
arng.Words.Last = Left(arng.Words, Len(arng.Words) - 1)
End If
Next j
Next i
End With
Try, for example:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "<de>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
If .Information(wdWithInTable) = True Then
.End = .Cells(1).Range.End - 1
.Delete
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
The problem you're encountering here is due to how Word reacts to a selection inside a cell: as soon as it reaches the end of the cell it selects the entire cell.
I've modified the code (which makes it more "complicated") to take this into account. In order to deal with this, there is now a Range object in the code that stores the original "Found" point in the document. After extending the selection to the end of the line, and moving it back one character (which means the entire cell is selected), the starting point is re-set to the "found" position.
Sub FindTheDeleteToEndOfCell()
Dim searchTerm As String
Dim bFound As Boolean
Dim rngFound As Word.Range
searchTerm = "<de>"
Selection.HomeKey wdStory
'Basic Find settings
With Selection.Find
.Forward = True
.wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
'Execute the Find
Do
With Selection.Find
.Text = searchTerm
bFound = .Execute
If bFound Then
Set rngFound = Selection.Range
Selection.MoveEnd wdLine, 1
Selection.MoveEnd wdCharacter, -1
Selection.Start = rngFound.Start
Selection.Delete
End If
End With
Loop While bFound
End Sub
I am trying to find all dashes that are alone in table cells and center them. What i have built below thus far will center all dashes in the document. How can I encapsulate this to center only the dashes in cells by themselves?
Sub Macro9()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.Alignment = wdAlignParagraphCenter
End With
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
End Sub
Since you want to restrict your searches to only a single dash in a single cell, you're better off only checking cell contents. Otherwise if you use .Find in the range of a table, you'll have to check all sorts of special cases to make sure the "found" dash is all alone in the cell. I think this works pretty efficiently:
Option Explicit
Sub Macro9()
Dim tbl As Table
Dim tCell As Cell
Dim r, c As Integer
Dim cellContents As String
For Each tbl In ActiveDocument.Tables
For r = 1 To tbl.Rows.Count
For c = 1 To tbl.Columns.Count
Set tCell = tbl.Cell(r, c)
'--- trim the cell delimiter off the end, then whitespace
cellContents = Left(tCell.Range.Text, Len(tCell.Range.Text) - 2)
cellContents = Trim(cellContents)
If cellContents = "-" Then
tCell.Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End If
Next c
Next r
Next tbl
End Sub
#PeterT what are you thoughts on this? (a collegue and I pulled it together)
Sub Macro1()
Dim CurrentText As Range
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "-"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Set CurrentText = Selection.Cells(1).Range
CurrentText.End = CurrentText.End - 1
If (CurrentText.Text = "-") And Not (Selection.Information(wdAlignParagraphCenter)) Then
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End If
End If
Loop
End Sub
Code to highlight italic text:
Sub Bold_Italic()
Dim rng As Range
Set rng = ActiveDocument.Range
rng.Collapse Direction:=wdCollapseStart
rng.Find.ClearFormatting
rng.Find.Font.Italic = True
rng.Find.Replacement.ClearFormatting
rng.Find.Replacement.Highlight = True
rng.Find.Replacement.Font.Color = wdColorRed
With rng.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
rng.Find.Execute Replace:=wdReplaceAll
End Sub
How can i highlight the whole paragraph or make selected for a paragraph?
Actually, i want to copy and paste paragraph by paragraph to another document.
I believe you're looking for range.select
Sub Macro()
Dim i As Integer
For i = 1 To ActiveDocument.Paragraphs.Count
Dim range As range
Set range = ActiveDocument.Paragraphs(i).range
'range.Characters.Count > 1 means there is text in the paragraph
If range.Characters.Count > 1 Then
'Handle the desired operation with the paragraph text
range.Select
MsgBox (range.Text)
End If
Next i
End Sub