Link to home
Start Free TrialLog in
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
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

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
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial