Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-08-2020, 03:08 AM
emblaw emblaw is offline Search and replace ends of sentences in tables Windows 10 Search and replace ends of sentences in tables Office 2016
Novice
Search and replace ends of sentences in tables
 
Join Date: Jun 2020
Posts: 9
emblaw is on a distinguished road
Default 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!
Reply With Quote
  #2  
Old 06-08-2020, 03:13 PM
macropod's Avatar
macropod macropod is offline Search and replace ends of sentences in tables Windows 7 64bit Search and replace ends of sentences in tables Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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 PC macro installation & usage instructions, see: Installing Macros
For Mac macro installation & usage instructions, see: Word:mac - Install a Macro
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-09-2020, 03:13 AM
emblaw emblaw is offline Search and replace ends of sentences in tables Windows 10 Search and replace ends of sentences in tables Office 2016
Novice
Search and replace ends of sentences in tables
 
Join Date: Jun 2020
Posts: 9
emblaw is on a distinguished road
Default 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()
Dim t As Table
For Each t In ActiveDocument.Tables
t.Columns(1).Width = CentimetersToPoints(3.75)
t.Columns(2).Width = CentimetersToPoints(12)
Next t
End Sub
Many thanks!
emblaw
Reply With Quote
  #4  
Old 06-09-2020, 03:20 AM
emblaw emblaw is offline Search and replace ends of sentences in tables Windows 10 Search and replace ends of sentences in tables Office 2016
Novice
Search and replace ends of sentences in tables
 
Join Date: Jun 2020
Posts: 9
emblaw is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 06-09-2020, 03:50 AM
macropod's Avatar
macropod macropod is offline Search and replace ends of sentences in tables Windows 7 64bit Search and replace ends of sentences in tables Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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]
Reply With Quote
  #6  
Old 06-09-2020, 03:59 AM
emblaw emblaw is offline Search and replace ends of sentences in tables Windows 10 Search and replace ends of sentences in tables Office 2016
Novice
Search and replace ends of sentences in tables
 
Join Date: Jun 2020
Posts: 9
emblaw is on a distinguished road
Default

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!
Reply With Quote
  #7  
Old 06-09-2020, 04:15 AM
macropod's Avatar
macropod macropod is offline Search and replace ends of sentences in tables Windows 7 64bit Search and replace ends of sentences in tables Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 revised the punctuation macro in post #2. Did you try the macro in post #5.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 06-09-2020, 04:36 AM
emblaw emblaw is offline Search and replace ends of sentences in tables Windows 10 Search and replace ends of sentences in tables Office 2016
Novice
Search and replace ends of sentences in tables
 
Join Date: Jun 2020
Posts: 9
emblaw is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
I've revised the punctuation macro in post #2. Did you try the macro in post #5.
Thank you, no I missed that somehow - on it!

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
Reply With Quote
  #9  
Old 06-09-2020, 04:40 AM
macropod's Avatar
macropod macropod is offline Search and replace ends of sentences in tables Windows 7 64bit Search and replace ends of sentences in tables Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 emblaw View Post
post #2 macro - didn't work I'm afraid
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]
Reply With Quote
  #10  
Old 06-09-2020, 06:42 AM
emblaw emblaw is offline Search and replace ends of sentences in tables Windows 10 Search and replace ends of sentences in tables Office 2016
Novice
Search and replace ends of sentences in tables
 
Join Date: Jun 2020
Posts: 9
emblaw is on a distinguished road
Default

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
Attached Files
File Type: docm macro-testdoc.docm (15.2 KB, 6 views)
Reply With Quote
  #11  
Old 06-09-2020, 03:08 PM
macropod's Avatar
macropod macropod is offline Search and replace ends of sentences in tables Windows 7 64bit Search and replace ends of sentences in tables Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 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]
Reply With Quote
Reply

Tags
search and replace, tables

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and replace ends of sentences in tables Search and replace hernans Word VBA 5 07-02-2018 07:01 PM
Search and replace ends of sentences in tables how to highlight all "indexed" sentences using find and replace? smallxyz Word 2 02-06-2016 02:54 AM
Search and replace ends of sentences in tables 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

Other Forums: Access Forums

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