Change font on selected text in macro for MS Word - vba

I am having to add some functionality to an existing macro for MS Word. This particular macro finds a specific text "#code_bar#" in an existing MS Word document and replaces it with a different text i.e. 3541589479.
Now, once this text is found and replaced (which the macro is already doing correctly), it is meant to change the font to "Free 3 of 9 Extended",which is already imported into Word.
This is the code, which is working properly except the part where I try to change the font and the size, which is actually not taking place. Could anyone help? Thanks.
Public Function sustituirCodigoBarras(codigo_barras)
Dim codigoDeBarras As String
Set codigoBarras = ActiveDocument.Content
'#barras_pedido# codigo de barras del pedido
codigoDeBarras = "#code_bar#"
With obj_Word.ActiveWindow.Selection.Find
.Text = codigoDeBarras
.Replacement.Text = "*" & codigo_barras & "*"
.Replacement.Font.Name = "Free 3 of 9 Regular"
.Replacement.Font.Size = 34
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
End Function

After some extra digging and a good-night sleep here's the answer:
.Format needs to = true in order for those changes to actually take place
The font name needs to be correct. In my case I was trying to use the Free 3 of 9 Extended (which was imported into my computer) and not the Regular (which was not imported and therefore not found) as in the code above.
Rookie mistake.
Hope this can help someone else

Related

Find the exact digits in a Word Document

I am trying to find specific digits in a Microsoft Word Document which contains text and digits, with VBA.
For example the text in the document is as follows;
(1) 52.203-19, This is a some text here
(2) 52.204-23, Quick brown fox jumped over the lazy dog 52 times.
(3) 52.204-25, I tried to search for a solution 52.204 times.
(4) 52.2, Could not find any luck though
(5) 52.203, this is blowing my mind away with mac 2.36
I wish to find the exact digits "52.2" as a whole.
I don't want to find instances where 52.2 is a part of another number like 52.203 or 52.204.
Also when I would like to find 52.203 then I want to exclude all instances like 52.203-xx where xx could be any two digit number.
In short I would like to find the exact number only as a whole and not in between the numbers, just like Excel's EXACT function.
Should I use RegEx or should I use Word's Advanced Find function with wildcards through VBA?
What I have finds all instances which I don't want.
Selection.Find.ClearFormatting
With Selection.Find
.Text = "52.2"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Regular expressions seems like the way to go for this.
First, go to Tools > References in the VBA editor and make sure that there is a check next to the Microsoft VBScript Regular Expressions 5.5 library.
The following code worked for me on your sample text to remove only the '52.2' after the '(4)' without affecting any of the surrounding characters:
Sub removeNumber()
Dim regExp As Object
Set regExp = CreateObject("vbscript.regexp")
With regExp
.Pattern = "\b52.2\b"
.Global = True
Selection.Text = .Replace(Selection.Text, "")
End With
End Sub
\b means word boundary so will not match any digits before or after the '52.5'.
No need for RegEx. You can use Find with wildcards. For explanation see https://wordmvp.com/FAQs/General/UsingWildcards.htm
The solution proposed by Mr. #TimothyRylatt worked for me perfectly specially after the addition of [!-] to avoid the hyphen containing numbers. However, I needed to implement this solution through a VBA Macro so I modified my code a little bit like this.
The working of Modified Code & the Code itself
Sub find_numbers()
Dim Str As String
'Create Search String for WildCard Search
Str = "<" & "52.203" & ">" & "[!-]"
Selection.Find.ClearFormatting
With Selection.Find
.Text = Str
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True 'make this option true to use WildCards
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub

How to add text to empty bullet points in Microsoft Word with VBA?

I am trying to add text to empty formatted bullet points in a word document, however I can't seem to find any successful way of doing so. I'm not very good at VBA, I just use it to automate reoccuring reports.
This is the format of VBA subs I've been using to find and replace text, I just can't find a way to adjust for adding to bullet points:
Private Sub FixedReplacements()
Dim Rng As Range
Dim SearchString As String
Dim EndString As String
Dim Id As String
Dim Link As String
Set Rng = ActiveDocument.Range
Rng.Find.ClearFormatting
Rng.Find.Replacement.ClearFormatting
With Rng.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Rng.Find.Execute Replace:=wdReplaceAll
End Sub
The goal I have set for empty bullet points is for them to display something along the lines of "No further information." << Just an example.
How this can be done depends very much on how the bullets were inserted. There is no way to specifically search the bullet, itself. A comment mentions
bullet points are the formatted bullet points found straight out of
word.
In that case, the default setting would be to format the paragraphs with the List Paragraph style. If that's the case, here, then Find can search for paragraphs using that style. The Find code in the question would then look as follows. (Note also the changes to the Format and Wrap properties.)
With Rng.Find
.Text = "^p"
.Replacement.Text = "No further information.^p"
.Forward = True
.Style = "List Paragraph"
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If a different style has been used to apply bullets, then that style name can be searched. Keep in mind that style names are case-sensitive.
There is an option in File/Options/Advanced, Editing options section: Use Normal style for bulleted or numbered lists. If this has been activated then things become very difficult. Best you can do is to try to match the applied paragraph formatting (indents, etc.).

Quote in Microsoft Word find and replace

There seems to be a problem when trying to get different kinds o quote characters when replacing text:
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\{QUOTE:(*):QUOTE\}"
.Replacement.Text = Chr(147) & "\1" & Chr(148)
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
When the document contains: {QUOTE:abc:QUOTE}
the code gives: ”abc”
but expected result is: “abc”
Versions of Word: “Microsoft Office Professional Plus 2013” and “Microsoft Office 365 ProPlus”.
The problem does not happen when File->Options->Proofing->AutoCorrect Options->AutoFormat As You Type->Replace as you type->"Straight quotes" with “smart quotes” is not activated. Of course I want the macro to work regardless of any setting for manual typing.
How can the code be changed to get the expected result?
The way to solve this kind of problem is to turn the option off for the duration of the code, then turn it back on again if it was on.
The following code snippet saves the user's setting, turns off the option, then restores the user's setting (whether or not the option was on or off).
Dim bReplaceQuotes as Boolean
'Save the user's setting
bReplaceQuotes = Options.AutoFormatAsYouTypeReplaceQuotes
Options.AutoFormatAsYouTypeReplaceQuotes = False
'Do the FindReplace
'Restore the user's settings at the end
Options.AutoFormatAsYouTypeReplaceQuotes = bReplaceQuotes

word dialogue box opens

This is my first post, I am not a programmer.
I built a Word macro (visual basic) that does a search and replace for many items (dates and numbers) within a highlighted block of text. Here's a single search and replace segment. It works well, but after each segment a Y/N dialogue box (in this case Word 2003) appears asking if I want to search the remainder of the document--I DONT.
Query: Is there anything I can add to macro that would respond "NO" (after each search and replace segment) as the macro runs so I don't have to select "no" after each of the 20 or so segments?
Typical segment:.
Selection.Find.ClearFormatting.
Selection.Find.Replacement.ClearFormatting.
With Selection.Find.
.Text = "2015".
.Replacement.Text = "2016".
.Forward = True.
.Wrap = wdFindAsk.
.Format = False.
.MatchCase = False.
.MatchWholeWord = False.
.MatchWildcards = False.
.MatchSoundsLike = False.
.MatchAllWordForms = False.
End With.
Selection.Find.Execute Replace:=wdReplaceAll
...
THX
The line
.Wrap = wdFindAsk
tells Word that you want it to ask the user whether to keep searching. Changing that line to
.Wrap = wdFindStop
should fix your problem.
try this:
Application.DisplayAlerts = False
*YOUR CODE HERE*
Application.DisplayAlerts = True
Not sure about Word 2003 (!)

Microsoft Word macro for formatting selected text, such as italics for all instances of e.g

this is my first question,
I have to go over a large number of documents and make sure that several formatting issues are correct. An example of such an issue would be to make sure that all periods, ".", are not bold, italics, underline, etc. Another example would be to make sure that all "etc." are in italics.
I have a list of the needed formatting issues.
Instead of going over each document and using the find/replace function I would rather write a macro that I can apply to each document.
I have no experience with VBA. I do on the other hand, have some experience with programming in C sharp and C in general.
Any help would be greatly appreciated.
BTW, I'm not asking for a complete program, rather a sample from which I can learn and continue with my own.
There are a couple options:
1.Under the Developer tab in Word, you can hit the "Record Macro" button and do a find and replace multiple times while recording the macro using the ctrl + H shortcut.
2.Have multiple smaller macros setup (such as the two below) hit the "Record Macro" and run them in the order that you want.
Sub ItalicizeEct()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Italic = True
With Selection.Find
.Text = "ect."
.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
Sub RemoveBoldPeriods()
Dim PunctAllRng As Word.Range
Set PunctAllRng = ActiveDocument.Range
With PunctAllRng.Find
.Format = True
.Text = "."
.Font.Bold = True
.Replacement.Text = "."
.Replacement.Font.Bold = False
.Execute Replace:=wdReplaceAll
End With
End Sub
3.Or simply write a large macro that will go through all the editing processes you need