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
maxdbaseAsked:
Who is Participating?
 
Rgonzo1971Connect With a Mentor Commented:
Hi,

pls try
Sub Macro()
  
    oldval = "2016"
    newval = "2017"
    Row = 1
    
    Set tbl = ActiveDocument.Tables(1)
    If InStr(1, tbl.Cell(Row, 2).Range.Text, oldval, vbTextCompare) Then
        res = Replace(tbl.Cell(Row, Col).Range.Text, oldval, newval)
        tbl.Cell(Row, Col).Range.Text = Left(res, Len(res) - 2)
    End If
End Sub

Open in new window

Regards
0
 
Ryan ChongCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.