View Single Post
 
Old 12-17-2022, 12:15 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Your sample documents do not reflect the details you provided so it is no wonder it did not work. Based on your revised example the following will work and will also allow for the spelling error.

Code:
Sub CopyNamesToExcel()
Dim oTable As Table
Dim oRng As Range
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim xlCell As Object
Dim i As Integer, j As Integer
Dim sName As String
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err Then
        Set xlApp = CreateObject("Excel.Application")
    End If
    On Error GoTo 0
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Sheets(1)
    xlApp.Visible = True
    'Set xlCell = xlSheet.Range("A1")
    'xlCell.value = "Name"
    'j = 2
    j = 1
    For Each oTable In ActiveDocument.Tables
        Set oRng = oTable.Cell(1, 2).Range
        oRng.End = oRng.End - 1
        If InStr(1, oRng.Text, "NAMES OF PERSON") > 0 Then
            sName = Replace(oRng.Text, "NAMES OF PERSON", "")
            sName = Replace(sName, "VOLUNTARIY RETIRED", "")    '? Spelling
            sName = Replace(sName, "VOLUNTARILY RETIRED", "")
            sName = Replace(sName, Chr(13), "")
            Set xlCell = xlSheet.Range("A" & j)
            xlCell.value = Trim(sName)
            j = j + 1
        End If
    Next oTable
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote