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
Related
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.
in a file MS WORD I wish to select a "/" and make bold all characters till the following ^p. The following code works well only for the first occurrence. In the file I have many occurrences and I am not able to apply this in the whole file. I tried several times with "for...next" and others, unfortuntely without success.
Many thanks for your help! Gianluca
Sub bold_title()
Set myRange = ActiveDocument.Content
Selection.Find.ClearFormatting
With Selection.Find
.Text = "/"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^p"
.Forward = True
.Wrap = wdFindAsk
.Format = False
End With
Selection.Find.Execute
Selection.Font.Bold = True
End Sub
The trick is to "collapse" the Selection- think of it like pressing the right-arrow key - so that the next search starts after the "found" text:
Selection.Collapse wdCollapseEnd
Usually, a Do While...Loop construct is used in order to repeat the Find until the end of the document. You'll find lots and lots of examples if you search here and elsewhere on the Internet.
For this to work successfully, make sure you set Wrap to wdFindStop (and not wdFindContinue as is currently in the code shown).
I've done some googling and was not able to find a way to create a VBA macro code for my specific need. What I'm trying to accomplish is having the macro search through some XML code I paste into Word, find the value between <CID>*</CID> and <FirstName>*</FirstName>, extract the values, then replace it with the new element format: <FName id="*">*</FName>, where * represents a wild card search for any value between the two element tags. So in my XML code example below, I want the macro to extract "59" and "John", delete the whole code and past the extracted values to new element format: <FName id"59">John</FirstName>. My code and VMBA macro:
***XML Code***
<CustIDandName>
<CID>59</CID>
<FirstName>17</FirstName>
</CustIDandName>
Changing it to <FName id="59">John</FName>
***Word VBA Macro Code I have so far...***
With Selection.Find
.Text = "\<FirstName\>*\<\/FirstName\>"
.Replacement.Text = "<FName id="59">John</FName>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
You can achieve this using a Word wildcard search and replace pattern:
Find text:
\<CID\>([0-9]*)\</CID\>*\<FirstName\>(*)\</FirstName\>
Replacement text:
<FName id="\1">\2</FirstName>
As the replacement text contains quotes, you must make sure that the automatic replacement of straight quotes with typographic quotes is disabled.
Dim AutoFormatAsYouTypeReplaceQuotes As Boolean
Dim AutoFormatReplaceQuotes As Boolean
' remember auto correct options
AutoFormatAsYouTypeReplaceQuotes = Options.AutoFormatAsYouTypeReplaceQuotes
AutoFormatReplaceQuotes = Options.AutoFormatReplaceQuotes
' disable auto correct of quotes
Options.AutoFormatAsYouTypeReplaceQuotes = False
Options.AutoFormatReplaceQuotes = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\<CID\>([0-9]*)\</CID\>*\<FirstName\>(*)\</FirstName\>"
.Replacement.Text = "<FName id=""\1"">\2</FirstName>"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
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