![]() |
#1
|
|||
|
|||
![]() Hi guys! I have a 1430 page word file where I want to:
For e.g if I have the following text I love MS Office forums. Forum members are very helpful. I could use their kind help. After my macro, the display should look like: I love MS Office forums. (I) (love) (MS) (Office) (forums)(.) Forum members are very helpful. (Forum) (members) (are) (very) (helpful)(.) I could use their kind help. (I) (could) (use) (their) (kind) (help)(.) |
#2
|
||||
|
||||
![]()
You could use a macro like the following. Just don't expect the processing to be instantaneous in a 1430-page document!
Code:
Sub ParseDoc() Application.ScreenUpdating = False Dim i As Long, RngSrc As Range, RngTgt As Range With ActiveDocument.Range For i = .Sentences.Count To 1 Step -1 Set RngSrc = .Sentences(i) With RngSrc Do While .Characters.Last Like "[ " & vbCr & Chr(160) & "]" .End = .End - 1 If .Start = .End Then Exit Do Loop End With If Len(RngSrc.Text) > 1 Then Set RngTgt = RngSrc.Duplicate With RngTgt .InsertAfter Chr(11) .Collapse wdCollapseEnd .FormattedText = RngSrc.FormattedText .InsertBefore "(" .Characters.Last.InsertBefore ")(" .InsertAfter ")" & Chr(11) With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "[ ]{1,}" .Replacement.Text = ") (" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End With End If Next With .Find .ClearFormatting .Replacement.ClearFormatting .Forward = True .Wrap = wdFindContinue .Format = False .MatchWildcards = True .Text = "^l^13" .Replacement.Text = "^p" .Execute Replace:=wdReplaceAll .Text = "^l " .Replacement.Text = "^l" .Execute Replace:=wdReplaceAll End With End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Thank you for your effort but I had a problem executing that.
I tried that but it gave an error saying "Runtime Error 424. Object required.' When I clicked Debug, it highlighted this line. For i = Sentences.Count To 1 Step -1 I don't know what to change here as I don't really understand the code. I just know how to use a code, make a macro out of it and hit 'run.' |
#4
|
||||
|
||||
![]()
When the error occurs, has any of the document been processed (the macro starts at the back and works its way forward)? If so, what occurs immediately in front of the last-processed sentence? Are there any tables in the document?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
I've attached the word file for your review. I wanted to test the code on this before going on to the 1430 file. The 1430 page file does not have any tables. Just text and page breaks.
|
#6
|
||||
|
||||
![]()
I've run the macro on your test document and I don't get the 'Runtime Error 424. Object required' result. Here it is back in the .doc format (the board setup doesn't allow docm files), post-macro (I ran the macro with the document in the docx format, though).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
Ok it worked now. Not sure what was wrong. Nonetheless, thanks a ton!!!
|
#8
|
|||
|
|||
![]()
Hi
The code you gave for inserting parentheses before and after each word in a file works great. What modification would the code need if I want it to apply to only that text in the document which is font size 20. Then the code should turn all the parentheses it just added as red and the words inside the parentheses as red? To understand, please see the example below. Note: I couldn't see the tool to show the variation in font size for my post, so assume that the bold represents font size 20. ORIGINAL SAMPLE Tim is a computer wiz kid RESULT AFTER RUNNING CURRENT CODE Tim is a computer wiz kid (Tim) (is) (a) (computer) (wiz) (kid) DESIRED RESULT Tim is a computer wiz kid Tim is a (computer) wiz kid Last edited by Singh_Edm; 09-19-2014 at 03:35 PM. Reason: Elaborated question. |
#9
|
||||
|
||||
![]()
You don't really need a macro for that - it can be done with a wildcard Find/Replace. An equivalent macro is:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range.Find .ClearFormatting .Forward = True .Format = True .Font.Size = 20 .Wrap = wdFindContinue .MatchWildcards = True .Text = "<[A-Za-z]@>" With .Replacement .ClearFormatting .Text = "(^&)" .Font.ColorIndex = wdRed End With .Execute Replace:=wdReplaceAll End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
![]()
Thanks Paul!
|
![]() |
Tags |
macro, parentheses |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Insert a pdf file into a word document | raistlin | Word | 2 | 01-08-2014 01:42 PM |
![]() |
Evgeniy | Word | 1 | 02-04-2012 01:36 PM |
![]() |
tatihulot | Word | 2 | 10-12-2011 04:40 AM |
![]() |
Subject1157 | Word | 2 | 06-16-2011 08:46 PM |
How to insert a .mov file in Word Document | Jai25 | Word | 0 | 02-11-2010 04:40 PM |