none
Word Macro - Need help on Text compare and print RRS feed

  • Question

  • Hi,

    I just started learning Macro for Word and have difficulties in getting the below problem solved. Any help would be greatly appreciated.

    In the word doc, I would first highlight a range which includes both tables and pics.

    Within the tables, I would like to print a statement in the last cell in the row when the second cell matches with a certain preset statement.

     For Example:

    This is what I have highlighted in the word document

    Index

    Name

    Comment

    1

    AB

     

    2

    B

     

    3

    C

     

    4

    AB

     

    I would like to do something similar to this:

    If cell(2) == AB

    Print "a" on cell(3)

     

    If cell(2) == B

    Print "b" on cell (3)

    In order to get the below result:

    Index

    Name

    Comment

    1

    AB

    a

    2

    B

    b

    3

    C

     

    4

    AB

    a

    I seem to be having trouble on comparing string and integer and not sure how to navigate to the last cell to print.

    Thank you in advance for any help provided.

    Wednesday, November 8, 2017 1:44 AM

Answers

  • The Cells in a Word table are a bit tricky to work with since they contain extra termination characters at the end of the text that need to be removed when comparing. Here is an example of what you are trying to do:

    Dim RowIndex As Integer
    Dim CellValue As String
    
    For RowIndex = 1 To ActiveDocument.Tables(1).Rows.Count
        'Remove cell terminator characters
        CellValue = Replace(ActiveDocument.Tables(1).Cell(RowIndex, 2).Range.Text, Chr(7), "")
        CellValue = Replace(CellValue, Chr(13), "")
        CellValue = Replace(CellValue, Chr(11), "")
        If CellValue = "AB" Then
            ActiveDocument.Tables(1).Cell(RowIndex, 3).Range.Text = "a"
        Else
            If CellValue = "B" Then
                ActiveDocument.Tables(1).Cell(RowIndex, 3).Range.Text = "b"
            End If
        End If
    Next RowIndex


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Yukari-Kun Thursday, November 9, 2017 3:09 AM
    Wednesday, November 8, 2017 2:04 PM

All replies

  • The Cells in a Word table are a bit tricky to work with since they contain extra termination characters at the end of the text that need to be removed when comparing. Here is an example of what you are trying to do:

    Dim RowIndex As Integer
    Dim CellValue As String
    
    For RowIndex = 1 To ActiveDocument.Tables(1).Rows.Count
        'Remove cell terminator characters
        CellValue = Replace(ActiveDocument.Tables(1).Cell(RowIndex, 2).Range.Text, Chr(7), "")
        CellValue = Replace(CellValue, Chr(13), "")
        CellValue = Replace(CellValue, Chr(11), "")
        If CellValue = "AB" Then
            ActiveDocument.Tables(1).Cell(RowIndex, 3).Range.Text = "a"
        Else
            If CellValue = "B" Then
                ActiveDocument.Tables(1).Cell(RowIndex, 3).Range.Text = "b"
            End If
        End If
    Next RowIndex


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Marked as answer by Yukari-Kun Thursday, November 9, 2017 3:09 AM
    Wednesday, November 8, 2017 2:04 PM
  • Thank you so much. Your code helped me solved my problem. :)

    I didn't realize that there are all these termination characters at the end.

    Thursday, November 9, 2017 3:11 AM