In this post, we want to exhibit you the quick way to find all one-sentence paragraphs in your Word document via VBA.
Now and then, some documents can have very specific requirements for their formatting and structures. For instance, you may not be allowed to have paragraphs with just one sentence. These paragraphs are not hard to recognize, but it takes time, especially it’s a long document.
Today, we will offer you the fast way to specify all one-sentence paragraphs in your document.
Find All One-sentence Paragraphs in One Document
- Firstly, Word counts a period as a sentence. So if there are words such as “Mr.” or “Ms.”, Word considers it as a sentence. To exclude such a distraction, you need to replace “Mr.” with “Mr”. And when finish find paragraph of single sentence, you can change them back. To replace words, you can refer to this link: How to Find and Replace Multiple Items in Your Word Document
- Second, press “Alt+ F11” to trigger VBA editor.
- Then click “Normal” project.
- Click “Insert” tab on the menu bar and choose “Module” on its drop-down menu.
- Next double click on module to open it.
- Paste following codes on the module:
Sub HighlightParagraphsWithSingleSentence() Dim nParagraphNum As Integer Dim nCountParagraph As Integer Dim objParagraphRange As Range Dim nCountSentence As Integer Dim nHighlightNum As Integer nCountParagraph = ActiveDocument.Paragraphs.Count nHighlightNum = 0 For nParagraphNum = 1 To nCountParagraph Set objParagraphRange = ActiveDocument.Paragraphs(nParagraphNum).Range nCountSentence = objParagraphRange.Sentences.Count ' Highlight all paragraphs with single-sentence. If nCountSentence = 1 And objParagraphRange.Characters.Count > 1 Then nHighlightNum = nHighlightNum + 1 objParagraphRange.HighlightColorIndex = wdYellow End If Next If nHighlightNum > 0 Then MsgBox ("There are " & nHighlightNum & " paragraphs with single sentence and they are highlighted.") Else MsgBox ("There are no paragraphs with single sentence") End If End Sub
- Last but not the least, click “Run” button or hit “F5”.
You will receive such a message box, telling you the job is done.
Find All One-sentence Paragraphs in Multiple Documents
- To begin with, you have to put all target documents in a folder.
- Then install and run bellowing macro:
Sub HighlightParagraphsWithSingleSentenceInMultipleFiles() Dim nParagraphNum As Integer Dim nCountParagraph As Integer Dim objParagraphRange As Range Dim nCountSentence As Integer Dim StrFolder As String Dim strFile As String Dim objDoc As Document Dim dlgFile As FileDialog Dim nHighlightNum As Integer Dim strSummary As String Set dlgFile = Application.FileDialog(msoFileDialogFolderPicker) With dlgFile If .Show = -1 Then StrFolder = .SelectedItems(1) & "\" Else MsgBox ("No Folder is selected!") Exit Sub End If End With strFile = Dir(StrFolder & "*.doc*", vbNormal) While strFile <> "" nHighlightNum = 0 Set objDoc = Documents.Open(FileName:=StrFolder & strFile) Set objDoc = ActiveDocument nCountParagraph = ActiveDocument.Paragraphs.Count For nParagraphNum = 1 To nCountParagraph Set objParagraphRange = ActiveDocument.Paragraphs(nParagraphNum).Range nCountSentence = objParagraphRange.Sentences.Count ' Highlight all paragraphs with single-sentence. If nCountSentence = 1 And objParagraphRange.Characters.Count > 1 Then nHighlightNum = nHighlightNum + 1 objParagraphRange.HighlightColorIndex = wdYellow End If Next objDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument objDoc.Save If nHighlightNum = 0 Then objDoc.Close End If strSummary = strSummary & strFile & " : " & nHighlightNum & " paragraphs with single sentence." & vbCrLf strFile = Dir() Wend MsgBox (strSummary) End Sub
- In the “Browse” window open, pick the folder where you keep documents and click “OK”.
The macro then will highlight all one-sentence paragraphs and leave documents open. If a document doesn’t contain a one-sentence paragraph, it will be closed. Besides, there is a message box, indicating the number of one-sentence paragraphs in each document.
Deal with Data Loss Incident
User errors and sudden power outage can result in the dead of Word. While a collapsed Word can not only affect daily work, but also lead to doc damage. This is by no means the most irritating part of a data disaster. It’s better to get a repairing utility to recover data immediately.
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including recover excel and pdf repair software products. For more information visit www.datanumen.com