insert missing period at end of paragraph in word - vba

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

Related

Macro for Adding Text To Begining of Every Paragraph

I am trying to create a Word macro that will go through a large document that I have and add the text "SAMPLE" to the beginning of every paragraph.
The document contains a Title page, Table of Contents and Headings throughout and I would prefer none of these have the "SAMPLE" text on them, just the paragraphs.
Below is some macro code I have found on various sites and kind of pieced together to do somewhat of what I want. It does place the "SAMPLE" text at the beginning of some paragraphs but not all, usually only the first paragraph of a new section within my document. And it also places it at the end of the Table of Contents and Beginning of the Title page.
I am brand new to macros in Word so any help is appreciated or if there is a better way of doing this perhaps? There might even be some unnecessary bits in this code since it is pieced together from other samples.
Sub SAMPLE()
Application.ScreenUpdating = False
Dim Par As Paragraph, Rng As Range
For Each Par In ActiveDocument.Paragraphs
If Par.Style = "Normal" Then
If Rng Is Nothing Then
Set Rng = Par.Range
Else
Rng.End = Par.Range.End
End If
Else
Call RngFmt(Rng)
End If
If Par.Range.End = ActiveDocument.Range.End Then
Call RngFmt(Rng)
End If
Next
Application.ScreenUpdating = True
End Sub
Sub RngFmt(Rng As Range)
If Not Rng Is Nothing Then
With Rng
.End = .End - 1
.InsertBefore "SAMPLE"
End With
Set Rng = Nothing
End If
End Sub
Provided your Title, Table of Contents and Headings etc. don't use the Normal Style - as they shouldn't - you really don't need a macro for this - all you need is a wildcard Find/Replace where:
Find = [!^13]*^13
Replace = SAMPLE: ^&
and you specify the Normal Style as a Find formatting parameter. You could, of course, record the above as a macro, but that seems overkill unless you're doing this often.

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

VBA - Word change the concept of word(a.b is one word or two)

I have a question: i want to have an array of words of a WORD document, which are larger than 29 and Shorter than 40 characters. I implemented it in VBA this way:
Sub function()
Dim arr(1000) As String
counter = 0
For Each sentence In ActiveDocument.StoryRanges
For Each w In sentence.Words
If Len(w) > 28 And Len(w) < 40 Then
arr(counter) = w
counter = counter + 1
End If
Next
Next
End Sub
The Problem is that I want all words with char '_' cosidered as one word; for example: 'Adrian_link_mart' is one word and not 3: 'Adrian' and 'link' and 'mart' like it will be considered
thanks for your help, adrian
This may help. There is a bit of a wrinkle as you will see below.
Option explicit
Sub test()
' Use a collection rather than an array as we don't need
' to know the size in advance
Dim word_array As Collection
' Word doesn't actually have a 'word' object. Probably because
' that clashes with Word the application. So instead of Word.Word
' we are using word.range which gives us all the utility we will
' need
Dim my_word_range As Word.Range
Dim my_range As Word.Range
For Each my_range In ActiveDocument.StoryRanges
For Each my_word_range In my_range.Words
With my_word_range
Do While .Next(unit:=wdCharacter) = "_"
' '_' is considered to be a word by Word so we need to
' count two Word words to get to the end of the next
' text word IYSWIM
.MoveEnd unit:=wdWord, Count:=2
Loop
If .Characters.Count > 28 And .Characters.Count < 40 Then
word_array.Add Item:=.Text
End If
End With
Next
Next
End Sub
If you are new to VBA then
Include Option explicit at the top of every module
In the VBA IDE go Tools.Option.Editor.Code Settings and make sure every box is ticked.
Learn how to use F1. In the VBA IDE, putting the cursor on a keyword and pressing F1 will bring up the MS help page for that keyword

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.

Counting how many comments and how they are distributed in VBA code?

I am looking to create some metrics about the quality of the VBA code I am writing, through different ratios of the actual code written and comment lines written.
Ideally I am looking for a VBA script/function to detect the comment lines in Macro Enabled workbooks and Excel add-ins and being able to differentiate where the comments and code are written e.g. have the comment to code ratio for each module and form in a project.
Below is the code I have so far, but I only managed to found how to give the total count of the lines and the count for the declaration lines. Is there something similar for comments?
Public Sub moduleInfo()
Dim objModule As Object
For Each objModule In Application.VBE.ActiveVBProject.VBComponents
With objModule
Debug.Print .Name, .CodeModule.CountOfLines, .CodeModule.CountOfDeclarationLines
End With
Next objModule
End Sub
You can check the existence of the character ' to spot a comment line. The comment ,ight occur anywhere in the code, such as after the instruction (you can easily modify the code if you want to count only lines that are purely comments). You can also count blank lines, because the CountOfLines property includes these.
Public Sub moduleInfo()
Dim comp As VBComponent, m As CodeModule
Debug.Print "Module", , "Lines", "Declarations", "Blanks", "Comments"
For Each comp In Application.VBE.ActiveVBProject.VBComponents
Set m = comp.CodeModule
Dim comments As Integer, blanks As Integer, i As Integer, line As String
For i = 1 To m.CountOfLines
line = Trim(m.Lines(i, 1))
If Len(line) = 0 Then
blanks = blanks + 1
ElseIf InStr(line, Chr(39)) Then
comments = comments + 1
End If
Next
Debug.Print m.Name, , m.CountOfLines, m.CountOfDeclarationLines, blanks, comments
Next
End Sub