VBA MS Word Font color - vba

I want to read read through a Word document, find any text that is marked in any color other then black and delete it. How to find the text color in VBA?

Try the following code:
Sub DeleteNonBlack()
Dim Wrd As Range
For Each Wrd In ActiveDocument.Words
If Wrd.Font.Color<>wdColorBlack and wrd.Font.Color<>wdColorAutomatic Then
Wrd.Delete
end if
Next Wrd
End Sub
HTH

Related

Finding words containing two font sizes in Word VBA

Example: I copy text from a PDF file to MsWord. All text is font 14 but word "LOVE"
"L" is size 14
"OVE" is size 9
I'm looking to find that words like that. Can anyone help me? Using VBA Code. Thanks.
The .Font.Size property is useful in this case. It returns "9999999" if the word has mixed font sizes.
Sub CheckFonts()
Dim objSingleWord As Range
Dim objDoc As Document
Set objDoc = ActiveDocument
With objDoc
For Each objSingleWord In .Words
Debug.Print objSingleWord.Text; objSingleWord.Font.Size
Next
End With
End Sub
Hope this helps.

Deleting Bullet Paragraph in Word using Excel VBA

I have the following text within a word document:
Total Amount Owed: <<Payment>>
Dates Mowed:
• 2/6/2019
• 2/14/2019
• <<Mowing1>>
How do I complelely remove the text and bullet point for the line containing the string <<Mowing1>> using VBA?
Thank you in advance!
May try
Sub test()
Dim Pg As Paragraph,PgTxt as String
For Each Pg In ActiveDocument.Paragraphs
If Not Pg.Range.ListFormat.List Is Nothing Then 'Process only bulleted list
PgTxt = Pg.Range.Text
If InStr(1, PgTxt, "<<Mowing1>>") > 0 Then
Pg.Range.Delete
End If
End If
Next
End Sub

How to write VBA to format sentence starting with // in Word 2016?

I have a 400+ page coding manual I use, and unfortunately turned off the green for all the comments in the manual. I can't undo it, as I hadn't noticed it until it was too late. Its ruined years of work.
How would I write VBA to parse the document finding sentences starting with // and ending in a Paragraph mark and change the color of them? Or assign a style to them?
Here is a start that I have cobbled together, I admire people who can write code without intellisence, its like trying to find your way blindfolded
Dim oPara As Word.Paragraph
Dim rng As Range
Dim text As String
For Each oPara In ActiveDocument.Paragraphs
If Len(oPara.Range.text) > 1 Then
Set rng = ActiveDocument.Range(oPara.Range.Start,oPara.Range.End)
With rng.Font
.Font.Color = wdColorBlue
End With
End If
Next
End Sub
The following seems to work:
Dim oPara As Word.Paragraph
Dim text As String
For Each oPara In ActiveDocument.Paragraphs
text = oPara.Range.text
'Check the left 2 characters for //
If Left(oPara.Range.text, 2) = "//" Then
oPara.Range.text = "'" & text
End If
Next
I assume you are using VBA so by placing a ' in front of // it will turn the line green. You could modify the code to replace // with ' if desired. The opera.range.text should grab the entire paragraph.

insert missing period at end of paragraph in word

i receive reports (word document in .doc format ) from clients which due to some processes at their end are missing periods () at the end of most paragraphs. I have to manually add periods. Is there any code in word vba macros to accomplish this.
Thank you.
This is a fairly simple example on how to add a period to every paragraph that contains text. You could extend it to see if the paragraph really has no period at the end but I leave that up to you to decide.
Call the Macro from the Developer Tab after you added the macro:
Sub TestAddPeriod()
Dim oPara As Word.Paragraph
Dim rng As Range
Dim text As String
For Each oPara In ActiveDocument.Paragraphs
If Len(oPara.Range.text) > 1 Then
Set rng = ActiveDocument.Range(oPara.Range.Start, oPara.Range.End - 1)
rng.InsertAfter "."
End If
Next
End Sub

How to find equation editor in word document using VBA?

I am writing a macro in VBA Excel, which is used to do some data processing on a word document. During this, I've changed the font name for the entire document to Times New Roman. But I don't want the same change applied to the 'equation editor' boxes in the document, since their font is "Cambria Math". Changing the font to Times New Roman is resulting into ambiguous data.
The Equations object changed post 2007. Pre 2007, you could work with those objects by declaring Field objects. For example
UNTESTED
Sub Sample()
Dim fldEqn As Field
For Each fldEqn In ActiveDocument.Fields
If fldEqn.Type = wdFieldEmbed Then
If InStr(1, fldEqn, "Equation.3") Then
With fldEqn.Result.Font
'
'~~> Rest of the code
'
End With
End If
End If
Next oField
End Sub
To work with the Equation Objects from 2007 onwards you have to use the OMaths collection.
You can change the font of all the equations using this code
Sub Sample()
Dim eqns As OMath
For Each eqns In ActiveDocument.OMaths
With eqns.Range.Font
'
'~~> Rest of the code
'
End With
Next
End Sub