Deleting Bullet Paragraph in Word using Excel VBA - 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

Related

WORD VBA to INSERT text after a list

At the end of our a Word2010 document we've a numbered list of 6 items. We would like to add a text, say, End of Document at the end of that document using VBA. But when I try the following code it always add a new list item (item 7) to the list with that text as shown in image below. NOTE: We don't have control over the document. So the last line of the document is always item number 6 of the list and when a user runs VBA code the code is supposed to add last line at the end of the document as End of document. And this line should not be the last item of the list.:
Sub test()
Dim oList As List
Set oList = ActiveDocument.Lists(1)
oList.Range.InsertParagraphAfter
oDoc.Content.InsertAfter "End of Document";
End Sub
Snapshot of the list at the End of a document:
Since you want to insert at the end of the document, you dont even need to find the list, this should do:
With ActiveDocument.Content
.InsertParagraphAfter
With .Paragraphs(.Paragraphs.Count).Range
.InsertAfter "End of Document"
.Style = wdStyleNormal
End With
End With

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

Count number of bullets in Active word Documents VBA

I am using this code to count number of bullets in word documents. But its always returning zero.
Sub FindBullet()
Dim oPara As Word.Paragraph
Dim count As Integer
count = 0
'Select Entire document
Selection.WholeStory
With Selection
For Each oPara In .Paragraphs
If oPara.Range.ListFormat.ListType = WdListType.wdListBullet Then
count = count + 1
End If
Next
End With
'Gives the count of bullets in a document
MsgBox count
End Sub
Well, I just opened up a blank Word doc, popped the code in and ran it, and it is returning however many bullets I put in. I did not modify your code...
Could your bullets really be something else, like a numbered list or some other symbol?

VBA MS Word Font color

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