#1
|
|||
|
|||
Search and replace ends of sentences in tables
Hello!
I have a large file with about 70 tables, each with four lines of text. Some of the entries have a period at the end of the sentence, but not all. Does anyone have any ideas as to how I could search and replace the ends of each field in every table for a period? It would save a lot of eyestrain! Many thanks in advance! |
#2
|
||||
|
||||
Try the following macro:
Code:
Sub Demo() Application.ScreenUpdating = False Dim Tbl As Table, Cll As Cell, Str As String For Each Tbl In ActiveDocument.Tables With Tbl.Range.Find .ClearFormatting .Replacement.ClearFormatting .Text = "([!.\!\?\:\;])(^13)" .Replacement.Text = "\1.\2" .MatchWildcards = True .Forward = True .Format = False .Wrap = wdFindStop .Execute Replace:=wdReplaceAll End With For Each Cll In Tbl.Range.Cells With Cll.Range Do While .Characters.Last.Previous.Text Like "[ " & Chr(9) & "]" .Characters.Last.Previous.Text = vbNullString Loop Str = Right(Trim(Split(.Paragraphs.Last.Range.Text, vbCr)(0)), 1) If Str Like "[!(!)(?)(.)(:)]" Then If Cll.ColumnIndex = 1 Then .Characters.Last.InsertBefore ":" Else .Characters.Last.InsertBefore "." End If End If End With Next Next Application.ScreenUpdating = True End Sub For Mac macro installation & usage instructions, see: Word:mac - Install a Macro
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks!
I'll try this out, thank you!
In the same document, I am also trying to make the column widths of all the tables (all of which contain 2 columns and 4 rows, with the first column being significantly narrower than the rest), but the following macro is giving me the error "Run-time error '5992': Cannot access individual columns in this collection because the table has mixed cell widths". Any ideas how I could fix this as well, since I've got you on the line!? Macro: Sub SetColumnWidths2()Many thanks! emblaw |
#4
|
|||
|
|||
I'm afraid the periods-macro didn't work. :/
Thank you for trying though! But I've probably caught most of them by now (took a couple out for the test though). However the column widths would indeed be helpful to get a useful macro for (and if you have an idea for a fix for your original one, it's definitely something that would prove useful in the future! I am new to the world of macros but as a long-time word-user who can do most things word has to offer, this is something I want to get on top of as well! Best, emblaw |
#5
|
||||
|
||||
The macro in post #2 assumes each cell has only one paragraph of at least two words. If that's not how your tables are formatted, you'll need to provide more details.
As for the table formats: Code:
Sub SetColumnWidths2() Dim t As Table For Each t In ActiveDocument.Tables t.Columns.DistributeWidth t.Columns(1).Width = CentimetersToPoints(3.75) t.Columns(2).Width = CentimetersToPoints(12) Next t End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Thank you macropod - each cell does indeed (on occasion) have more than one paragraph - usually it's just the one, but I see one example of 11 paragraphs (that one includes a numbered list, with each entry containing a paragraph of text) - in every case it is only the first row, second column, that may have multiple paragraphs. Incidentally, this cell is also the least likely to be missing periods, so a code that checked only rows 2-4 of each table would do the job! If that helps!
Could this also be causing issue with my column-width macro? Thanks again! |
#7
|
||||
|
||||
I've revised the punctuation macro in post #2. Did you try the macro in post #5.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Quote:
Results: post #2 macro - didn't work I'm afraid :/ post #5 macro - it's working - there's some formatting deviations (from the user that created the document, the sort of user that just uses the space bar to align things as they don't know how to use formatting, I'm sure you know the type) which are making it stutter, but thanks to your macro trying to fix it before resulting in an error, I am now able to much more easily spot the problematic tables. You have saved me a lot of manual laboring - thank you!! I don't supposed you could suggest a good resource where I could start to learn to use word macros from scratch? Best wishes, emblaw |
#9
|
||||
|
||||
The revised macro in post #2 works for me, regardless of how many paragraphs a cell has. If it's not working for you, you'll need to attach a document to a post with a sample table that isn't being processed correctly.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Hi again - It is adding them in the first column - I didn't spot that until just now, but it was easy to fix - rather than the second one (where they were missing some of the time). Here is a test doc illustrating the missing periods, where I've run the macro.
Best, emblaw |
#11
|
||||
|
||||
I've further revised the macro in post #2 so it ensures the contents in column 1 end with a colon. Try it now.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
search and replace, tables |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Search and replace | hernans | Word VBA | 5 | 07-02-2018 07:01 PM |
how to highlight all "indexed" sentences using find and replace? | smallxyz | Word | 2 | 02-06-2016 02:54 AM |
VBA Table – Search All Tables - Find & Replace Text in Table Cell With Specific Background Color | jc491 | Word VBA | 8 | 09-30-2015 06:10 AM |
Find/Replace, Duplicating a word in a sentence to hundreds of sentences. | DDDD | Word | 0 | 10-08-2013 10:48 AM |
Search and Replace - Clear Search box | JostClan | Word | 1 | 05-04-2010 08:46 PM |