I want to have a VBA script perform a find / replace in a selection and then assign the macro to a button on the Quick Access ToolBar, to save having to click through the usual Find/Replace procedure.
I recorded a macro while doing this and this is what I get:
Sub FindReplace()
'
' FindReplace Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
But when I run this macro on a selection to change paragraph marks into spaces it continues on to change the whole document, not just the selection.
I'm no stranger to VBA but I can't see how to fix it so it stops when the selection is done.
You are going to need to use
.Wrap = wdFindStop
Instead of
.Wrap = wdFindAsk
Related
I am developping an excel macro and i need to replace a specific word (beacon) in my .docx by a bookmark, i found this on the microsoft site.
Sub BMark()
' Select some text in the active document prior
' to execution.
ActiveDocument.Bookmarks.Add _
Name:="tableauxvdd", Range:=Selection.Range
End Sub
But i dont know how to fix the range, i have the idea to select the word with smthg like that :
With word_fichier.Application.Selection.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
.Execute FindText:="#tableauxvdd"
End With
But it need a range and not a selection.
For example:
With word_fichier.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "#tableauxvdd"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
If .Find.Found = True Then .Bookmarks.Add Name:="tableauxvdd", Range:=.Duplicate
End With
Note: As I commented in your other thread, and as demonstrated above, there is no need to use 'Selection' at all for what you are doing.
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'm trying to use a find and replace macro that finds a selection based on a custom style.
I tested what I'm doing with the built-in styles such as Heading 1 or Subtle Quote and had no problem running the find and replace. You would just simply replace the words InlineAR with Heading 1 after ActiveDocument.Styles (".
However, my issue is that when I made a custom style called 'InlineAR' and refer to that name in VBA (at ActiveDocument.Styles ("InlineAR"), my macro doesn't add the "\textArabic"snippet before any text styled with the InlineAR custom style.
Code at Issue:
Sub LaTeXStylingTextArabic()
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("InlineAR")
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "\textArabic{^&"
.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
I want to record a macro such that it will replace all 'line break' symbol after the current cursor position to a blank space.
For example if the text is:
My name is Sumit. I
love
Stackoverflow
Now if the cursor is just next to 'love' and I press the macro button it should come like this:
My name is Sumit. I
love Stackoverflow
I searched internet and wrote this (but this is not working)
Sub Replace()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
I would guess that you'd want to change
.Wrap = wdFindContinue
to
.Wrap = wdFindStop
and
.MatchWildcards = True
to
.MatchWildcards = False
The first change should prevent the search from going back around and replacing content before the cursor and the second change turns off Wildcards (^p is not a wildcard character).
But it might help to know exactly how it's not working, if those changes aren't sufficient.
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.