Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-17-2014, 03:39 PM
Singh_Edm Singh_Edm is offline Insert parentheses before and after each word in a file. Windows 8 Insert parentheses before and after each word in a file. Office 2013
Advanced Beginner
Insert parentheses before and after each word in a file.
 
Join Date: Jan 2014
Posts: 41
Singh_Edm is on a distinguished road
Default Insert parentheses before and after each word in a file.

Hi guys!
I have a 1430 page word file where I want to:
  1. Copy a line till a period (including the period).
  2. Have a line break
  3. Paste the line.
  4. Enclose each word of the pasted line in parentheses.
  5. Line break
  6. Return to step 1.

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)(.)
Reply With Quote
  #2  
Old 06-17-2014, 05:21 PM
macropod's Avatar
macropod macropod is offline Insert parentheses before and after each word in a file. Windows 7 32bit Insert parentheses before and after each word in a file. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 06-20-2014, 02:58 PM
Singh_Edm Singh_Edm is offline Insert parentheses before and after each word in a file. Windows 8 Insert parentheses before and after each word in a file. Office 2013
Advanced Beginner
Insert parentheses before and after each word in a file.
 
Join Date: Jan 2014
Posts: 41
Singh_Edm is on a distinguished road
Default

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.'
Reply With Quote
  #4  
Old 06-20-2014, 03:58 PM
macropod's Avatar
macropod macropod is offline Insert parentheses before and after each word in a file. Windows 7 32bit Insert parentheses before and after each word in a file. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 06-20-2014, 04:01 PM
Singh_Edm Singh_Edm is offline Insert parentheses before and after each word in a file. Windows 8 Insert parentheses before and after each word in a file. Office 2013
Advanced Beginner
Insert parentheses before and after each word in a file.
 
Join Date: Jan 2014
Posts: 41
Singh_Edm is on a distinguished road
Default

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.
Attached Files
File Type: docx Test.docx (11.3 KB, 8 views)
Reply With Quote
  #6  
Old 06-20-2014, 04:15 PM
macropod's Avatar
macropod macropod is offline Insert parentheses before and after each word in a file. Windows 7 32bit Insert parentheses before and after each word in a file. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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).
Attached Files
File Type: doc Test.doc (30.0 KB, 11 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 06-20-2014, 04:21 PM
Singh_Edm Singh_Edm is offline Insert parentheses before and after each word in a file. Windows 8 Insert parentheses before and after each word in a file. Office 2013
Advanced Beginner
Insert parentheses before and after each word in a file.
 
Join Date: Jan 2014
Posts: 41
Singh_Edm is on a distinguished road
Default

Ok it worked now. Not sure what was wrong. Nonetheless, thanks a ton!!!
Reply With Quote
  #8  
Old 09-19-2014, 03:25 PM
Singh_Edm Singh_Edm is offline Insert parentheses before and after each word in a file. Windows 8 Insert parentheses before and after each word in a file. Office 2013
Advanced Beginner
Insert parentheses before and after each word in a file.
 
Join Date: Jan 2014
Posts: 41
Singh_Edm is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 09-19-2014, 05:14 PM
macropod's Avatar
macropod macropod is offline Insert parentheses before and after each word in a file. Windows 7 64bit Insert parentheses before and after each word in a file. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #10  
Old 11-19-2015, 09:25 PM
Singh_Edm Singh_Edm is offline Insert parentheses before and after each word in a file. Windows 8 Insert parentheses before and after each word in a file. Office 2013
Advanced Beginner
Insert parentheses before and after each word in a file.
 
Join Date: Jan 2014
Posts: 41
Singh_Edm is on a distinguished road
Default

Thanks Paul!
Reply With Quote
Reply

Tags
macro, parentheses



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert a pdf file into a word document raistlin Word 2 01-08-2014 01:42 PM
Insert parentheses before and after each word in a file. Word insert only 1st page of multi-paged PDF file Evgeniy Word 1 02-04-2012 01:36 PM
Insert parentheses before and after each word in a file. Insert-Text from File in Word Ribbon? tatihulot Word 2 10-12-2011 04:40 AM
Insert parentheses before and after each word in a file. Parentheses Changing on Document 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

Other Forums: Access Forums

All times are GMT -7. The time now is 11:48 AM.


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