I'm trying to work on my first (and hopefully the last) Macro on Word and I have come to a dead-end. As I am working a lot between Word and Word Online the formatting tends to change so I need to create a Macro that will save me the trouble of re-adjusting it manually.
My problem is that I need to have different spacing on my body text based on the font used (for Arial to have spacing before 6 and for Courier New that we use for code spacing before 0). I have tried to create the code using the Macro Recorder and the Replace function but the recorder gives me a generic code that doesn't specify the font I need (although I choose it) so when I run both I end up with the Arial formatting on both. Here is the code I get for the Courier New:
Sub FormatBodyCode()
Application.ScreenUpdating = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.ParagraphFormat
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceMultiple
.LineSpacing = LinesToPoints(1.15)
.Alignment = wdAlignParagraphLeft
.OutlineLevel = wdOutlineLevelBodyText
.LineUnitBefore = 0
.LineUnitAfter = 0
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
Application.ScreenUpdating = True
End Sub
How can I specify the font to make it work?
One quick code change should restrict the search for a single font:
...
With Selection.Find
...
.Format = True
.Font.Name = "Courier New" ' <== Add this - Or whatever font you want
...
The easiest way to do multiple fonts is to copy the whole function to another function with a different name and change the font name and spacing in the other function.
Related
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'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 am editing some text files written in Coptic language using non Unicode fonts. I am converting the files to Unicode.
The files I were written using three fonts to represent pronunciation dialects.
I want to create a macro to search and replace the non Unicode characters with Unicode ones.
I want to limit the search and replace to certain fonts. I tried to record a macro, but the font choices did not record.
The end result will be replacing the characters in one of the fonts (that had dialect marks built into the character) with a Unicode character in addition to the dialect mark separately.
Is there a way to modify the code below to limit the search and replace function to the characters written in a particular font?
Sub aaaa()
'
' aaaa Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "n"
.Replacement.Text = ChrW(11419) & ChrW(769)
.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 Replace:=wdReplaceAll
End Sub
Where to choose the font
The end result
The solution:
Sub twoo()
'
' twoo Macro
'
'
With Selection.Find
.Text = "n"
' Clear all previously set formatting for Find dialog box.
.ClearFormatting
' Set font to Find for replacement.
.Font.Name = "EXISTING FONT NAME"
' Clear all previously set formatting for Replace dialog box.
.Replacement.ClearFormatting
' Set font to Replace found font.
.Replacement.Font.Name = "NEW FONT NAME"
.Replacement.Text = ChrW(11419) & ChrW(769)
.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 Replace:=wdReplaceAll
End Sub
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.