This bit of code has worked for years and now as of today it won't do anything. All I'm trying to do is highlight every instance of a superscript in the document.
Sub mcrHighLightSuperSript()
'
' mcrHighLightSuperSript Macro
'
Selection.Find.ClearFormatting
With Selection.Find.Font
.Superscript = True
.Subscript = False
End With
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
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
Did Word do some update that makes this code invalid?
Thanks!
Instead of using the long chain of property and method calls, for example:
Selection.Find.Replacement.ClearFormatting
The Selection.Find method returns a Find object that contains the criteria for a find operation. So, you could retrieve the object instance once in the code and then re-use every time you need to set up a property or call a method.
Related
I have the following macro. It changes numbers of the form x.x to x,x. It was recorded and I added the IF statement to make sure a text is selected to prevent the user from doing it on the whole document.
Sub fixComma()
'
' fixComma Macro
'
'
If (Selection.Start <> Selection.End) Then
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.LanguageID = wdEnglishUS
With Selection.Find
.Text = "([0-9]).([0-9])"
.Replacement.Text = "\1,\2"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Else
MsgBox "Nothing is selected, Macro terminated"
End If
End Sub
The problem is it is changing the whole document and not just the selection.
Changing
Selection.Find.Execute Replace:=wdReplaceAll
to
Selection.Find.Execute Replace:=wdReplaceOne
Will get it so the first instance of x.x in a selection will change to x,x and not the whole document.
Edit: if you want all items in a selected area only to change then keep:
Selection.Find.Execute Replace:=wdReplaceAll
But change
.Wrap = wdFindAsk
to
.Wrap = wdFindStop
How to find all bold fonts with VBA? Without any editing, I just want to select all these bold fonts. I can do it with Word's own "Find and Replace" function, but recording a macro has no effect.
The desired effect is as follows.
The following code is the recorded macro, which is not valid.
Sub Macro1()
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = 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
End Sub
Although it should not be difficult to implement this function, I have searched the web for many days without finding the right answer.
I have a vba code in ms word that performs a find operation.
It finds a line with specific color. goes to the begining of that line paste from clipboard go to end of the line.
Selection.Find.ClearFormatting
Selection.Find.Font.Color = wdColorDarkRed
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.PasteAndFormat (wdFormatOriginalFormatting)
I dont know much coding. just want to perform this find operation until all the lines are found and there is nothing left. Maybe an if or while loop?
Copy whatever it is you want to replicate to the clipboard, then use an ordinary Find/Replace, where:
Find = your font colour
Replace = ^c^&
and choose Replace All.
No code required, though you could record it as a macro. No looping required, either.
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
I am trying to build a macro (as a noob) to find certain words and then change the formatting for that word (i.e. make it bold or italic)
This code 'sort of works.' It will find some words and change them but not change others. The weird thing is it works until I add another sub then it stops formatting on some words, while formatting others. The routine never breaks and runs until the end without error.
Can anyone teach why this is happening and what I am doing wrong? I am not a programmer. Thanks
Sub Macro2()
'
' Macro2 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Printer"
.Replacement.Text = ""
.Replacement.Font.bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "Parameter Values"
.Replacement.Text = ""
.Replacement.Font.bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection.Find
.Text = "Use All Applicants Indicator"
.Replacement.Text = ""
.Replacement.Font.bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection.Find
.Text = "Next Section"
.Replacement.Text = ""
.Replacement.Font.bold = True
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
I would program the search macro as a separate sub, like this.
Private Sub FindAndReplace(ByVal Txt As String, _
Optional ByVal NewTxt As String, _
Optional ByVal Fmt As Boolean = False, _
Optional ByVal BldFmt As Boolean = False)
With ActiveDocument.Content
With .Find
.ClearFormatting
.Text = Txt
.Format = Fmt Or BldFmt
With .Replacement
.ClearFormatting
.Text = NewTxt
.Font.Bold = BldFmt
End With
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
.Find.Execute Replace:=wdReplaceAll
End With
End Sub
All the optional parameters you may use but don't have to.
Then I would call the sub repeatedly with different parameters, perhaps like this:-
Sub MakeReplacements1()
FindAndReplace "Printer", BldFmt:=True
FindAndReplace "Parameter values", BldFmt:=True
FindAndReplace "Use All Applicants Indicator", BldFmt:=True
FindAndReplace "Next Section", BldFmt:=True
End Sub
or even like this:-
Sub MakeReplacements2()
Dim Fnd() As String
Dim i As Long
Fnd = Split("Printer|Parameter values|Use All Applicants Indicator|Next Section", "|")
For i = 0 To UBound(Fnd)
FindAndReplace Fnd(i), BldFmt:=True
Next i
End Sub
Selection is an object comprising the part of the document currently selected.
Find is a property of the Selection object defining the Find object (same name but one is a property, the other an object). The Find object has properties such as Text, Forward, Wrap, etc. and it has methods like ClearFormatting or Execute. All of this you can read up in the MSDN library.
Now, when you define the Find object you are describing something you want to find. With the Execute command you start looking for it. Your code is missing this command in some places.
The search is limited to the Selection. If you have selected nothing Word will presume you want to search the whole document. But Selection.Find will change the Selection to highlight the found item. Therefore, if you want to continue searching the whole document you would need to reset the Selection after each search with, for example, Activedocument.Content.Select.
In a nutshell, if you clear the Find object after each use, set a new description before each repeated use, define the Selection object for each search and don't forget to Execute each separate search your code should work just as you intend it to work.