Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-24-2014, 06:13 AM
lukestkd lukestkd is offline Formatting table cells in a document Windows 7 64bit Formatting table cells in a document Office 2010 64bit
Novice
Formatting table cells in a document
 
Join Date: Feb 2014
Posts: 1
lukestkd is on a distinguished road
Default Formatting table cells in a document

Hello all,
I have the following code which formats a specific piece of text in a mail merge document, however the text is in a series of cells, and I would like to be able to format the actual cell rather than the text itself.

Is there a way to modify the code I have to do this?

Many thanks,

Luke

Code:
Sub HighlightTargetsN()

Dim range As range
Dim i As Long
Dim TargetList

TargetList = Array("MMN") ' put list of terms to find here

For i = 0 To UBound(TargetList)

Set range = ActiveDocument.range

With range.Find
.Text = TargetList(i)
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

Do While .Execute(Forward:=True) = True
range.HighlightColorIndex = wdRed
range.Bold = True
range.Font.ColorIndex = wdRed
range.Font.Underline = wdUnderlineThick
range.Font.UnderlineColor = wdRed
range.Font.Name = "TW Cen MT"
range.Font.Size = 14
Loop

End With
Next

End Sub

Reply With Quote
  #2  
Old 02-24-2014, 02:21 PM
macropod's Avatar
macropod macropod is offline Formatting table cells in a document Windows 7 32bit Formatting table cells in a document 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 code like:
Code:
Sub HighlightTargetsN()
Dim Rng As Range, i As Long, TargetList
TargetList = Array("MMN") ' put list of terms to find here
For i = 0 To UBound(TargetList)
  Set Rng = ActiveDocument.Range
  With Rng
    With .Find
      .Text = TargetList(i)
      .Format = True
      .MatchCase = True
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Forward = True
      .Wrap = wdFindStop
      .Execute
    End With
    Do While .Find.Found
      .HighlightColorIndex = wdBrightGreen
      With .Font
        .Bold = True
        .ColorIndex = wdRed
        .Underline = wdUnderlineThick
        .UnderlineColor = wdRed
        .Name = "TW Cen MT"
        .Size = 14
      End With
      If .Information(wdWithInTable) = True Then
        .Cells(1).Shading.BackgroundPatternColorIndex = wdYellow
      End If
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
End Sub
Note: I've played around with the colouring so as to differentiate each component's actions. Also, it's not a good idea to give variables the same names as reserved words (e.g. range), as this can lead to strange errors.

For a different way of handling this, which uses the mailmerge functionality instead of post-processing via a macro, see: http://answers.microsoft.com/en-us/o...7-86553a7f8188
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-24-2014, 02:37 PM
macropod's Avatar
macropod macropod is offline Formatting table cells in a document Windows 7 32bit Formatting table cells in a document 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

Cross-posted at: http://www.excelforum.com/newreply.p...6843&noquote=1 and http://www.vbaexpress.com/forum/show...-in-a-document
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting table cells in a document VBA and hyperlinks in table cells viuf PowerPoint 13 02-19-2012 10:34 AM
Formatting table cells in a document Make Cells Mimic The Formatting Of One Cell skoz55 Excel 1 11-09-2011 09:00 AM
Formatting table cells in a document Copy table cell formatting across multiple cells / tables pakistanray Word Tables 2 10-31-2011 08:07 AM
Formatting Cells for Currency and Accounting Sparetyme Excel 1 07-11-2010 01:26 PM
Formatting table cells in a document Cut and paste a range of cells and preserve formatting StarWeaver Excel 1 03-02-2010 01:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:44 PM.


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