Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #27  
Old 11-13-2021, 06:54 PM
John O'Rourke John O'Rourke is offline How to insert paragraph character after every 500 characters? Windows 10 How to insert paragraph character after every 500 characters? Office 2019
Novice
 
Join Date: Nov 2021
Posts: 6
John O'Rourke is on a distinguished road
Smile Latest and Greatest

I decided that there is a problem in the VBA word handling of for each p in doc.paragraphs and the for each sn in p.range.sentences. When you insert vbcr the number of paragraphs increases and the number of total sentences decreases. It thinks you are in the next paragraph sort of but its first sentence is the last sentence of the prior paragraph. I decided to abandon for each method and use set p=doc.paraphs(i)
and just increment i by hand and read the next paragraph as if I were processing a file. Then I can completely control the positioning, characters, sentences and go through the document quickly and accurately. I don't now get short sentences inside each paragraph but there may be a couple of short sentences as the last sentence. This may require playing with various cutoffs to get something that handles most conditions but I like what I have now. For those of you that shudder at gotos please don't look at the code.

Code:
Sub insertp()

Dim doc As Document
Dim p As Paragraph
Dim sn As Variant
Dim ts As Long
Dim tc As Long
Dim i As Long

Set doc = ActiveDocument

Application.ScreenUpdating = False

i = 0
GoTo NextParagraph

ProcessPara:

        Set sn = p.Range.Sentences(ts)
        tc = tc + Len(sn)
           
        If tc >= 300 And ts < p.Range.Sentences.Count Then
            p.Range.Sentences(ts).InsertAfter vbCr
            GoTo NextParagraph
        End If
        
        ts = ts + 1
        If ts < p.Range.Sentences.Count Then
            GoTo ProcessPara
        End If
        
NextParagraph:

    i = i + 1
    If i <= doc.Paragraphs.Count Then
        Set p = doc.Paragraphs(i)
        If p.Range.Sentences.Count > 0 And p.Range.Characters.Count > 450 Then
            ts = 1
            tc = 0
         GoTo ProcessPara
        Else
            GoTo NextParagraph
        End If
    End If
    
Application.ScreenUpdating = True

End Sub
Reply With Quote
 

Tags
paragraph character



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert paragraph character after every 500 characters? Restricting paragraph styles without restricting character styles Red Pill Word 5 05-25-2012 01:06 PM
character set bobster Word 0 06-07-2011 10:17 AM
How to insert paragraph character after every 500 characters? tab key arrows first character carolinason Word 6 10-30-2010 06:45 PM
Junk characters (box-like characters) in Word file Sashikala Word 1 04-20-2010 02:03 PM
Character style stripped while applying paragraph style sams_gates Word 0 08-29-2009 02:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:49 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft