Avatar of Keith McElroy
Keith McElroy
 asked on

VB Script automate Microsoft Word Table Cell Content Replace Part of Text

I have a couple hundred Microsoft Word Tables each with a cell with the following value
FY 2016
Actual

I need to increment the year and attempted this way which did not do anything:

dim newval
newval = "2017"
call replace(tbl.cell(row,2).range.text,"2016",newval)

Question:  How can I modify this code so that it replaces the 2016 with 2017?

If possible I wish to keep the original text and just replace the 2016.  
That way I do not loose the line break
VB ScriptMicrosoft WordVBA

Avatar of undefined
Last Comment
Rgonzo1971

8/22/2022 - Mon
Ryan Chong

you can try:

Sub test()
    Dim oldval, newval, Row, Col
    Dim tbl As Table
    
    oldval = "2016"
    newval = "2017"
    Row = 1
    Col = 2
    
    Set tbl = ActiveDocument.Tables(1)
    If InStr(1, tbl.Cell(Row, Col).Range.Text, oldval, vbTextCompare) Then
        tbl.Cell(Row, Col).Range.Text = newval
    End If
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Rgonzo1971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61