Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-02-2014, 06:22 PM
mrayncrental mrayncrental is offline Add asterisks in front of all bold sentences in a table cell Windows 7 64bit Add asterisks in front of all bold sentences in a table cell Office 2007
Novice
Add asterisks in front of all bold sentences in a table cell
 
Join Date: Feb 2014
Posts: 15
mrayncrental is on a distinguished road
Angry Add asterisks in front of all bold sentences in a table cell

Hi - I am trying to create a Word macro that searches a column in a table for all bold sentences and adds 2 asterisks at the beginning of each sentence. So far I have come up with the code below. But it only finds the first sentence over and over again. I will have cells that have more than 1 bold sentence so I don't want to use range and move from cell to cell because it ignores the other sentences.

Thanks in advance for your help!


Sum AsteriskText()

Dim keepSearch As Boolean

keepSearch = False

Selection.Tables(1).Columns(3).Select


Do
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute


If Selection.Find.Found Then
keepSearch = True
Else
keepSearch = False
End If

With Selection


.InsertBefore "**"

End With


Loop While keepSearch


End Sub
Reply With Quote
  #2  
Old 02-02-2014, 07:50 PM
fumei fumei is offline Add asterisks in front of all bold sentences in a table cell Windows 7 64bit Add asterisks in front of all bold sentences in a table cell Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Please use code tags when posting codes.
Quote:
so I don't want to use range and move from cell to cell because it ignores the other sentences.
No, you DO want to use range, because it does NOT ignore other sentences.
Code:
Sub AsterisksBold()
Dim r As Range
Dim SentenceRange As Range
Set r = Selection.Range.Cells(1).Range
For Each SentenceRange In r.Sentences
  If SentenceRange.Font.Bold = True Then
     SentenceRange.InsertBefore "**"
  End If
Next
End Sub
Assuming of course you really do mean sentences, not paragraphs.

Oh, and the above REQUIRES that the selection is in the cells that you want to act. It will fail if the selection is not in a table. Best practice would actually test to make sure it IS in a table first. I did not bother...you should do it though.
Reply With Quote
  #3  
Old 02-02-2014, 08:32 PM
mrayncrental mrayncrental is offline Add asterisks in front of all bold sentences in a table cell Windows 7 64bit Add asterisks in front of all bold sentences in a table cell Office 2007
Novice
Add asterisks in front of all bold sentences in a table cell
 
Join Date: Feb 2014
Posts: 15
mrayncrental is on a distinguished road
Default Thanks for your help, but stuck in cell

Your code works great ---but I need help getting the selection/range to advance to the next sentence & next cell. Here is what I have:

Dim keepSearch As Boolean
Dim r As Range
Dim SentenceRange As Range
Dim tRow As Row
Dim tCell As Cell
Dim myTable As Table

Set myTable = ActiveDocument.Tables(1)
For Each tRow In myTable.Rows
Set tCell = tRow.Cells(3)
Set r = Selection.Range.Cells(1).Range
For Each SentenceRange In r.Sentences
If SentenceRange.Font.Bold = True Then
SentenceRange.InsertBefore "BOMBME"
End If
Next
Next


This doesn't move to the 3rd cell (not sure how to set this). When I start in the right cell, it goes to first sentence in the cell (column 3), then the 2nd sentence, but never makes it to the third sentence or out of the first cell [in column 3].

Can you help me move the selection forward? Many thanks!!
Reply With Quote
  #4  
Old 02-03-2014, 12:06 AM
fumei fumei is offline Add asterisks in front of all bold sentences in a table cell Windows 7 64bit Add asterisks in front of all bold sentences in a table cell Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

If you are going to use a table object, then you do not need to use selection at all. Nor do you need to use rows and columns if you are processing all cells. And again, please use code tags when you are posting code.
Code:
Sub Bombme()
Dim r As Range
Dim SentenceRange As Range
Dim aCell As Cell
Dim myTable As Table
Set myTable = ActiveDocument.Tables(1)
For Each aCell In myTable.Range.Cells
   Set r = aCell.Range
   For Each SentenceRange In r.Sentences
      If SentenceRange.Font.Bold = True Then
         SentenceRange.InsertBefore "BOMBME "
      End If
   Next
Next
End Sub
This goes through every cell in table(1). The Selection does not need to be in the table. If there is some reason you have to be in cell(3), then please state that. Please state exactly what you want.
Reply With Quote
  #5  
Old 02-03-2014, 03:50 AM
macropod's Avatar
macropod macropod is offline Add asterisks in front of all bold sentences in a table cell Windows 7 32bit Add asterisks in front of all bold sentences in a table cell 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

Do be aware that what Word VBA treats as a sentence often isn't what most people would consider so...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 02-03-2014, 08:06 AM
mrayncrental mrayncrental is offline Add asterisks in front of all bold sentences in a table cell Windows 7 64bit Add asterisks in front of all bold sentences in a table cell Office 2007
Novice
Add asterisks in front of all bold sentences in a table cell
 
Join Date: Feb 2014
Posts: 15
mrayncrental is on a distinguished road
Default

This code runs perfect! Many thanks!

If I only want to run it for the 3rd column in the table (instead of all cells) how would I set the range?

(Newbie to forum posting - will use Code tag in future)
Reply With Quote
  #7  
Old 02-04-2014, 05:47 PM
fumei fumei is offline Add asterisks in front of all bold sentences in a table cell Windows 7 64bit Add asterisks in front of all bold sentences in a table cell Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Please note what macropod posted. "Sentences" often surprise people. What Word considers a sentence is NOT intuitive.
Reply With Quote
  #8  
Old 02-04-2014, 06:35 PM
macropod's Avatar
macropod macropod is offline Add asterisks in front of all bold sentences in a table cell Windows 7 32bit Add asterisks in front of all bold sentences in a table cell 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

Quote:
Originally Posted by mrayncrental View Post
If I only want to run it for the 3rd column in the table (instead of all cells) how would I set the range?
Change:
For Each aCell In myTable.Range.Cells
to:
For Each aCell In myTable.Columns(3).Cells
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 02-06-2014, 11:48 PM
mrayncrental mrayncrental is offline Add asterisks in front of all bold sentences in a table cell Windows 7 64bit Add asterisks in front of all bold sentences in a table cell Office 2007
Novice
Add asterisks in front of all bold sentences in a table cell
 
Join Date: Feb 2014
Posts: 15
mrayncrental is on a distinguished road
Default

perfect. many thanks!
Reply With Quote
Reply

Tags
macro table search



Similar Threads
Thread Thread Starter Forum Replies Last Post
Add asterisks in front of all bold sentences in a table cell Keep box drawn in front of Video in FRONT position a_gunslinger PowerPoint 1 04-18-2013 07:54 AM
Format Bold in one line makes all lines bold Nitte Word 2 02-07-2013 12:34 AM
asterisks turned into broken line -- can't get rid of gsjackson Word 3 12-02-2011 03:14 PM
Table of Authorities Entries - Zombie Bold emilypage Word 2 10-25-2011 06:56 AM
Auto-populate an MS Word table cell with text from a diff cell? dreamrthts Word Tables 0 03-20-2009 01:49 PM

Other Forums: Access Forums

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