I am trying to search an ActiveX TextBox (TextBox1) to replace a phrase with nothing...
I have this code that seems to just wipe the entire box rather than the phrase in isolation.
Private Sub CommandButton3_Click()
TextBox1 = Selection
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "This is the text to remove!"
.Replacement.Text = ""
.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
With some additions (like selecting all shapes in the active document)- the code works with an ordinary TextBox - and with the rest of the document too... just not the ActiveX box (which is what I want!!)
Please help!
You can simply use the built-in VBA Replace function:
Private Sub CommandButton3_Click()
TextBox1.Value = Replace(TextBox1.Value, "This is the text to remove!", "")
End Sub
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 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
I have mail merge field in my Word 2010 document that contains a check box control that is checked or not depending on the content from the input list.
{IF NEW = "NEW" "☒" "☐"} where those boxes are actually check box controls
However, after the mail merge is complete, the check box control is replaced with a symbol for the checked or unchecked box, as appropriate. As a result, one can no longer switch from checked to unchecked in the final document as you would for a check box control.
A similar question was asked here but was unresolved (one response with a solution does not work for me).
I am looking for a simple way to find the checked or unchecked symbol in the output document and replace it with a check box document control in the appropriate state.
I'm not big on programming, but if you could point me in the right direction, I'll try my best. I have played around with VB Macros in the past (very amateur), so I tried that again and got this far as a "proof of concept":
Sub Checkbox()
' ChrW(9744) is unchecked box; 9746 is checked box
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ChrW(9744)
.Replacement.Text = ChrW(9746)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
End Sub
I also found out the way to add a checkbox control:
Selection.Range.ContentControls.Add (wdContentControlCheckBox)
But I haven't figured out how to marry that last piece of code into the 'replace' line, nor how to define the checkbox as checked or not, based on the search.
Thanks for the help.
You can do the following:
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Do
With Selection.Find
.Text = ChrW(9744) ' ChrW(9746)
.Forward = True
.Wrap = wdFindStop 'wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If Selection.Find.Execute = False Then Exit Do
' This adds the checkbox at the selected position
Set f = ActiveDocument.FormFields.Add(Range:=Selection.Range, _
Type:=wdFieldFormCheckBox)
'f.CheckBox.Value = True ' Add this for checked checkboxes
Loop
For Office 2010, a more advanced checkbox control is available which supports formatting. The code for this would be:
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Do
With Selection.Find
.Text = ChrW(9744)
.Forward = True
.Wrap = wdFindStop 'wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If Selection.Find.Execute = False Then Exit Do
' This adds the checkbox at the selected position
Set f = Selection.Range.ContentControls.Add(wdContentControlCheckBox)
f.SetCheckedSymbol CharacterNumber:=254, Font _
:="Wingdings"
f.SetUncheckedSymbol CharacterNumber:=168, _
Font:="Wingdings"
f.Checked = False' Change this for checked checkboxes
Loop
Am a beginner in VBA. I need to store only italic text from a specific paragraph into a variable. How to do it?
you can use the following example to look for a text range with the desired formatting and then process them by storing them in the variable used each time you can append the new text with the previous content of the variable.
Eg:
selection_range.Style = _
ActiveDocument.Styles("Text_Format_To_Search")
where
Text_Format_To_Search will be replaced by the text formatting you need to search.
reference from:- http://www.vb-helper.com/howto_format_code_in_word.html
Thanks to KazJaw. Finally I got the solution and I have pasted below
sub test()
ActiveDocument.Paragraphs(2).Range.Select
Selection.Find.Font.Italic = 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
str_italic = Selection.Range.Text
End Sub
If you looking to find italicized text and store only the text in a variable, then you can try:
Sub findItal()
Dim italText As String
'Find italic text
With Selection.Find
.ClearFormatting
.Font.Italic = True
.Wrap = wdFindStop
.Execute
If .Found = True Then
italText = Selection.Range.Text
End If
End With
End Sub
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.