Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-05-2014, 01:46 AM
Sandy27 Sandy27 is offline Loop through Word doc to find and reformat text Windows 7 64bit Loop through Word doc to find and reformat text Office 2010 64bit
Novice
Loop through Word doc to find and reformat text
 
Join Date: Nov 2013
Posts: 8
Sandy27 is on a distinguished road
Default Loop through Word doc to find and reformat text


I have created a Word file containing letters from an Access database. The font for the whole file is Times New Roman and fontsize 12. I send a copy of each letter to the subject and, at the end of each letter, there is a line that starts with “cc “ and is then followed by the name and address of the subject.

I have been trying to go through the document searching for “cc “, selecting the sentence and then changing the fontsize to 9, but I am struggling with the Word VBA. This is what I have so far:
Code:
With objWord.Visible = True 
    objDocs("Temporary.docx").Activate 
    .Selection.HomeKey unit:=wdStory 
    Do 
        With .Selection.Find 
            .Execute findtext:="cc " 
            .Wrap = wdFindStop 
            Selection.Extend 
            Selection.Extend 
            Selection.Extend 
            .ClearFormatting 
            .Font.Name = "Times New Roman" 
            .Font.Size = 12 
            .Text = "" 
            With .Replacement 
                .ClearFormatting 
                .Font.Size = 9 
                .Text = "" 
                .Collapse Direction:=wdCollapseEnd 
            End With 
        End With 
    Loop Until .Selection.Find.Found = False 
End With
I know my syntax is faulty, but am I using the right search term? Any help greatly appreciated.

Last edited by macropod; 01-05-2014 at 03:18 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 01-05-2014, 07:32 AM
gmaxey gmaxey is offline Loop through Word doc to find and reformat text Windows 7 32bit Loop through Word doc to find and reformat text Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Something like this provided each cc line constitutes a singe paragraph. Change oDoc to represent your objDoc.

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc As Word.Document
Dim oRng As Word.Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "cc"
    While .Execute
      If oRng.Start = oRng.Paragraphs(1).Range.Start Then
        With oRng
          .MoveEnd wdParagraph
          With .Font
            .Name = "Times New Roman"
            .Size = 12
          End With
          .Collapse wdCollapseEnd
        End With
      End If
    Wend
  End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 01-05-2014, 03:29 PM
macropod's Avatar
macropod macropod is offline Loop through Word doc to find and reformat text Windows 7 32bit Loop through Word doc to find and reformat text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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 believe you could use something like:
Code:
With objWord.Documents("Temporary.docx").Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^13cc [!^13]@^13"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    .Start = .Start + 1
    .Font.Size = 9
    .Collapse 0 '0= wdCollapseEnd
    .Find.Execute
  Loop
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 01-06-2014, 02:01 PM
Sandy27 Sandy27 is offline Loop through Word doc to find and reformat text Windows 7 64bit Loop through Word doc to find and reformat text Office 2010 64bit
Novice
Loop through Word doc to find and reformat text
 
Join Date: Nov 2013
Posts: 8
Sandy27 is on a distinguished road
Default

Thank you both very much for your help. Neither solution seemed to work initially and I have been experimenting with variations on the code. I also realised that I should be searching either for "cc" + Tab or, possibly, CR + "cc" + Tab. The nearest I have got is with this:
Code:
With .Documents("Temporary.docx").Range
  Do
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "cc^9"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchWildcards = True
      .Execute
    End With
    .Font.Size = 9
    .Collapse 0 '0= wdCollapseEnd
    .Find.Execute
  Loop While .Find.Found
End With
which, bizarrely, changes every other "cc" + Tab to font size 9, but not the adjacent name and address (which is in a single line), but doesn't affect the rest of the letters, which remain at font size 12. I'd be grateful for any comments.

Last edited by macropod; 01-06-2014 at 02:23 PM. Reason: Added code tags & formatting
Reply With Quote
  #5  
Old 01-06-2014, 02:30 PM
macropod's Avatar
macropod macropod is offline Loop through Word doc to find and reformat text Windows 7 32bit Loop through Word doc to find and reformat text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Quote:
Originally Posted by Sandy27 View Post
I also realised that I should be searching either for "cc" + Tab or, possibly, CR + "cc" + Tab. The nearest I have got is
...
which, bizarrely, changes every other "cc" + Tab to font size 9, but not the adjacent name and address (which is in a single line), but doesn't affect the rest of the letters, which remain at font size 12. I'd be grateful for any comments.
Your Find expression only looks for "cc" + Tab, without regard to what's before or after. Therefore, it will change just that combo wherever its found. With the code I posted, changing:
.Text = "^13cc [!^13]@^13"
to:
.Text = "^13cc[ ^t][!^13]@^13"
will act on all paragraphs starting with 'cc' followed by a tab or space. Similarly, deleting '.Start = .Start + 1' and the first ^13 will work on any "cc" + Tab/space, till the end of that paragraph.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 01-06-2014, 03:48 PM
Sandy27 Sandy27 is offline Loop through Word doc to find and reformat text Windows 7 64bit Loop through Word doc to find and reformat text Office 2010 64bit
Novice
Loop through Word doc to find and reformat text
 
Join Date: Nov 2013
Posts: 8
Sandy27 is on a distinguished road
Default

That did the trick. Thanks very much indeed Paul.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop through Word doc to find and reformat text Loop action in Word until not found kilburfi Word VBA 2 07-12-2013 01:26 AM
Loop through Word doc to find and reformat text Reformat line lengths joe925 Word 1 03-18-2013 05:06 PM
reformat file renato Word 1 06-13-2012 06:32 AM
Loop through Word doc to find and reformat text Macro to loop in Word Yamaha Rider Word VBA 2 02-07-2012 05:33 PM
Loop through Word doc to find and reformat text Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:04 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