![]() |
|
#1
|
|||
|
|||
|
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.
|
|
#2
|
||||
|
||||
|
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:
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#3
|
|||
|
|||
|
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
|
|
#4
|
||||
|
||||
|
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 |
|
#5
|
|||
|
|||
|
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
|
|
| 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 characters in a string
|
Marcia | Excel Programming | 5 | 05-04-2020 05:15 AM |
Wildcard replace any string in context with a specified string
|
wardw | Word | 7 | 05-07-2018 09:13 AM |
Replace characters in a string
|
Anthon | Excel Programming | 1 | 11-03-2016 12:48 AM |
Find and replace a string of text
|
errtu | Word | 1 | 01-31-2013 02:09 PM |