Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-16-2022, 12:26 PM
ranjan ranjan is offline Replace String Windows 10 Replace String Office 2019
Advanced Beginner
Replace String
 
Join Date: May 2021
Posts: 80
ranjan is on a distinguished road
Default Replace String

In a word document am having "N" no. of tables. I want to replace a string in the first row for every table in a document by find & replace, can we automate this task ...

Range is first row in a every table (only one row will exist)

Format type: .docx & .rtf

Find & Replace Strings are fixed for every table in a document.

Find string is in small letter "sample" and replace string is in capital letter "Sample" in the below case.

Example:

Find String: "sample"
Replace String: "Sample"

Your assistance has been greatly appreciated



Please find a test document in attachment.
Attached Files
File Type: docx test.docx (15.2 KB, 8 views)
Reply With Quote
  #2  
Old 02-16-2022, 03:08 PM
Guessed's Avatar
Guessed Guessed is offline Replace String Windows 10 Replace String Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This can be achieved from two different directions.
1. Step through all the tables to see if a string exists in the first row
2. Find a string and see if that found instance is in the first row of a table

The best method depends on how many instances are likely to be found in/out of scope and what you mean by
Quote:
Find & Replace Strings are fixed for every table in a document.
Have a go at the code yourself and post what you have. Perhaps looking back at some of your previous code will help https://www.msofficeforums.com/word-...deletions.html
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 02-17-2022, 12:38 AM
ranjan ranjan is offline Replace String Windows 10 Replace String Office 2019
Advanced Beginner
Replace String
 
Join Date: May 2021
Posts: 80
ranjan is on a distinguished road
Default

Only replace a first row in every table and shouldn't get replaced in any where else in a document...

I have tried the below one, can you review the range once.

Range is first row of a every table. (It shouldnt get replaced otherthan a first row of a tables)

Code:
Sub Test_Replace()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "sample"
    .Replacement.Text = "Sample"
    .Format = False
    .Forward = False
    .Wrap = wdFindStop
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With
  Do While .Find.Execute
    If .Information(wdWithInTable) = True Then
      Set Rng = ActiveDocument.Tables(1).Cell(1, 0)
        Do While Rng.Tables.Count > 0
       Loop
      Exit Do
    End If
    .Collapse wdCollapseStart
  Loop
End With
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #4  
Old 02-17-2022, 01:31 AM
gmayor's Avatar
gmayor gmayor is offline Replace String Windows 10 Replace String Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It would be simpler just to replace 'sample' with 'Sample' in the first cell of each table e.g.
Code:
Sub Macro1()
Dim oTable As Table
Dim oCell As Range
    For Each oTable In ActiveDocument.Tables
        Set oCell = oTable.Cell(1, 1).Range
        oCell.Text = Replace(oCell.Text, "sample", "Sample")
    Next oTable
    Set oCell = Nothing
    Set oTable = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 02-17-2022, 02:39 AM
ranjan ranjan is offline Replace String Windows 10 Replace String Office 2019
Advanced Beginner
Replace String
 
Join Date: May 2021
Posts: 80
ranjan is on a distinguished road
Default

Hi,

Thanks Graham Mayor its quite working well and I liked how you did it so simply, but After running a macro one table enter is creating for every first row of a table. it can be avoided or not.

Upon your initiation, I tried it another way, and it worked well.

Thanks a lot for your help...

Code:
Sub Test_Replace()
Dim oTable As Table
Dim oCell As Range
    For Each oTable In ActiveDocument.Tables
        Set oCell = oTable.Cell(1, 1).Range
        oCell.Find.Execute FindText:="simple", ReplaceWith:="Simple", Replace:=wdReplaceAll
    Next oTable
    Set oCell = Nothing
    Set oTable = Nothing
End Sub
Reply With Quote
Reply

Tags
find, replace



Similar Threads
Thread Thread Starter Forum Replies Last Post
how do I replace string 123-4 with 123 4 davidjm Word VBA 25 11-03-2021 02:40 PM
Replace String Replace characters in a string Marcia Excel Programming 5 05-04-2020 05:15 AM
Replace String Wildcard replace any string in context with a specified string wardw Word 7 05-07-2018 09:13 AM
Replace String Replace characters in a string Anthon Excel Programming 1 11-03-2016 12:48 AM
Replace String Find and replace a string of text errtu Word 1 01-31-2013 02:09 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:38 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft