#1
|
|||
|
|||
How to insert paragraph character after every 500 characters?
Hello,
I have one MS word 2007 document. I would like to add a paragraph character after every 400-500 characters. My basic need is to create a group of sentences. Those sentences should not contain more than 500 characters. This whey, each group of 500 characters (ie 5-8 lines) can help me build one slide of PPT quickly. Or, Any one has better idea how to PROPERLY convert Word to ppt? Please help. Thank you. |
#2
|
||||
|
||||
Quote:
Powerpoint slides should have the least number of words possible to convey the main points of what is being discussed, not reams of text for people to read. Be that as it may, try: Code:
Sub TextSplitter() Dim Rng As Range Application.ScreenUpdating = False With ActiveDocument Set Rng = .Range(0, 0) Do With Rng On Error GoTo ErrExit .MoveEndUntil cset:=vbCr, Count:=wdForward If Len(.Text) > 500 Then .End = .Start + 500 .End = .Start + InStrRev(Rng.Text, ".") + 1 If .Characters.Last.Text <> vbCr Then .Characters.Last.Delete .InsertAfter vbCr End If End If DoEvents .Start = .Paragraphs.Last.Next.Range.Start End With Loop Until Rng Is Nothing ErrExit: End With Set Rng = Nothing Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks macropod,
Your solutions works. But not exactly as expected. You quoted "Those sentences should not contain more than 500 characters." But what I am expecting is each PARAGRAPH ( Not sentences) should not contain more than 500 characters. One paragraph can itself contain N number of sentences. But total characters should not exceed 500. The reason behind this is that, one PPTX slide with font size 25 can store maximum 500 characters. My idea is to break a document in paragraphs. Each paragraph can contain many sentences, but all to gather they should contain max 500 characters. I know you are very very near to the solution. I am C# expert, I don't understand VB else I would have solved this. Your help is highly appreciated. Thank you very much. |
#4
|
||||
|
||||
The result is the same, because the code I posted limits the paragraph length to the same 500 characters, according to however many whole sentences will fit in that 500 character count.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Dear macropod,
Thanks for the reply. I was my mistake that I was applying your above procedure on already processed document. What I did was first I broke whole document's sentenses into paragraphs and then applied your procedure. Hence I was unable to detect the new paragraph created by your procedure as the document was already full of paragraphs. I request you to help me in adding three more things. 1)BEFORE above procedure executes, I want to remove all unnecessary paragraph characters. The reason behind this is that I have created this document by coverting a PDF. And to maintain line-to-line formatting, PDF processors add this paragraph at the end of each line. The algorithm would be: a) Find a paragraph character. b) If it's previous character is NOT full point ".", then replace the paragraph character with a space. I would use regular expressions in C# to achieve this. This procedure should execute BEFORE any instruction in your Sub TextSplitter(). 2) I want to split all sentenses to a new paragraph. Already I am using Find and replace feature as Find "._ " and then repalce with ".^p" Even this should execute BEFORE any instruction in your Sub TextSplitter() but after the previous instructions are executed. 3) Finally, your procedure throws "Run time error" after the document processing reaches at end. I would use Try and catch block in C#. Also, I want to remove particular objects from complete document such as a horizontal line. We can't use Find and replace for this purpose. Then How to select all Line objects at once and delete? Thanks for your valuable help. Last edited by aditya_bokade; 06-30-2012 at 09:02 AM. Reason: Forgot to mention one point. |
#6
|
|||
|
|||
Dear macropod,
By getting inspired by you, I tried to create a macro in Ms Power point. I wanted a macro which after execution can apply underline, make it bold and color red the selected text. So I went to MS Word and recorded the macro. It worked as expected. When I copied the same procedure to MS Power point, it says "Object required." This is my macro: Sub aaaaaaa() Selection.Font.Color = wdColorRed ----> here it stops execution. If Selection.Font.Underline = wdUnderlineNone Then Selection.Font.Underline = wdUnderlineSingle Else Selection.Font.Underline = wdUnderlineNone End If End Sub Yes, I have selected some text in MS Power point before running the macro. This same idea works in MS Word but not in MS power point. If it works, it will be very helpful to me. Please help. Thank you. |
#7
|
||||
|
||||
For a macro to clean up the text before processing, see: https://www.msofficeforums.com/word/...d-returns.html
VBA doesn't have the equivalent of 'Try', but you could add: On Error Resume Next after: With ActiveDocument Regarding the line objects, I suspect they're 'shapes', like autoshape lines, but without seeing the document I can't be sure. If they are, a macro could easily enough delete them, but I'd need to know whether there are other shapes that need to be preserved. As for your PowerPoint macro, you generally cannot simply copy & paste a macro from Word to PowerPoint, especially one produced by the macro recorder. The vba object models for the two applications are quite different. For example, without even considering whether PowerPoint's vba object model has the procedures you're trying to use, all the statements beginning with 'wd', like 'wdColorRed' are Word-specific.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Hi everyone,
I've come across this thread and this is exactly a macro I needed in my translation work. I translate in MS Word and prefer to do it paragraph by paragraph so I always have a view of the original text in the most convenient way, and have only the original document open. I type in different color from the one used in the original document which allows me to remove the original text when I finish the translation, and the translated text falls in place neatly.That said, the problem I have is with bigger paragraphs which occasionally, (not too often obviously), come up in the document, taking up the whole page or even more, and I can't have a view of the text I translate and type at the same time. This code works as a charm in my Word 2013 (I set the limit to 1000 characters instead of 500) and I get paragraphs size suited to my purpose but what I need is to have the place in the text where the code adds a paragraph break marked some way so when I get to that section of the text I'm reminded that the paragraph is created by this code i.e that it doesn't exist in the original format of the document I translate. I need my translation to keep the original format of the document so I'd need to remove the paragraph breaks which the code inserted. Is it possible to insert into this code some sort of a special symbol that would follow the inserted paragraph breaks? That would enable me to restore the original format in the translated text by removing all instances of paragraph breaks followed by that symbol through Replace function. Thank you for the help. |
#9
|
||||
|
||||
You could change:
.InsertAfter vbCr to, say: .InsertAfter "***" & vbCr & "***" This will put three asterisks either side of the paragraph break inserted by the macro. You could, of course, choose any other combination of characters or only insert them before or after the vbCr that creates the paragraph break.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Thank you very much macropod, just tried it and it worked perfectly! Just what I needed.
One note though, which I forgot to mention earlier. When I run a macro I get a pop-up window reporting "Object variable or With block variable not set" . It is a little annoying but doesn't affect the functionality of the code and I get paragraphs split with asterisks, works just fine. After playing with it a bit I noticed a small hitch, not too big a problem but anyway. When the chunk of text that makes up one paragraph is bigger than twice (is, say, thrice the size) the value of characters which is set as maximal in the code it won't add two breaks. In other words it can only split in half, i.e add one break per a paragraph. When I run the macro again it adds the remaining break to the rest of the text which now can be split in half. I set the maximum value to 1000 and it doesn't matter much to me personally, I rarely if ever encounter such big paragraphs. And it is not a problem to press macro button a few times more, just in case the document contained some gigantic paragraphs. |
#11
|
||||
|
||||
I've added some error-trapping to the code to eliminate the "Object variable or With block variable not set" message. As for large paragraphs, I've run the code on paragraphs with over 4000 characters and they're all neatly split at 500 (or 1000)character intervals. Did you change both instances of 500 to 1000?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
The "Object variable or With block variable not set" message is gone
I figured that the problem with the code failing to split longer paragraphs occurs only when I add asterisks i.e when I replace the original line with .InsertAfter "***" & vbCr & "***", otherwise it works just perfect as you said. I've tried setting different values and it works like a charm. It also works with the line with asterisks inserted only the macro needs to be run repeatedly to have the same result. Hope you fix this too macropod |
#13
|
||||
|
||||
In that case, try using:
.InsertAfter vbCr & "*** " & vbCr instead of: .InsertAfter "***" & vbCr & "***"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
The previous one (.InsertAfter "***" & vbCr & "***"   gave me a sequence : *** Pb ***, and it worked except for bigger paragraphs where I had to rerun the macro to get the result. I don't have use for the first three asterisks, it suffices to me to have the paragraph marked at the beginning, but they are not a problem either, that part goes with the original text which is cut out.
The revised code (.InsertAfter vbCr & "*** " & vbCr) inserts a sequence Pb *** Pb and this one does it perfectly, and at one go, whatever the length of the paragraph. This one actually does what I asked for, but I have a problem of different kind now. To conveniently move to the next paragraph, I've created a keyboard shortcut using autohotkey which places the cursor after the paragraph break (CTRL+DOWN ARROW places it at the beginning of the next paragraph and that doesn't suit me so I made a combination of Word shortcuts to place the insertion point in between the paragraphs, and added a code from the net to change the text color to red), so that the other paragraph break in the sequence PB *** PB is a problem. Is it possible to change it so the second paragraph break is left out and I get PB*** inserted instead. That would be pitch perfect for my purposes, and a great time-saver. Thanks for the help again macropod. |
#15
|
||||
|
||||
Try it with:
.InsertAfter Chr(11) & "****" & vbCr
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
paragraph character |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
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 |