![]() |
|
|||||||
|
|
|
Thread Tools | Display Modes |
|
|
|
#1
|
||||
|
||||
|
The following will do what you asked based on your example: Code:
Sub CopyNamesToExcel()
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
For i = ActiveDocument.Paragraphs.Count To 1 Step -1
If InStr(1, ActiveDocument.Paragraphs(i).Range.Text, "Name of person") > 0 Then
Set oRng = ActiveDocument.Paragraphs(i).Range
oRng.MoveStartUntil "n"
oRng.Start = oRng.Start + 1
oRng.End = oRng.End - 1
sName = Trim(Replace(oRng.Text, Chr(34), ""))
Set xlCell = xlSheet.Range("A" & j)
xlCell.value = sName
j = j + 1
End If
Next i
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#2
|
|||
|
|||
|
Thank you Sir for the quick help
When I run the macro, the name not extracted fully. I herewith attached example files, the word source and how the expected Excel output file would be. Thanks in advance. John |
|
| Tags |
| find specific text |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Find a Specific word in doc file using VBA
|
Priyantha Gamini | Word VBA | 7 | 12-14-2022 05:18 AM |
Macro to check the existence of a word docx file and create a new word file with specific content.
|
staicumihai | Word VBA | 14 | 11-15-2016 01:42 AM |
| Macro to highlight repeated words in word file and extract into excel file | aabri | Word VBA | 1 | 06-14-2015 07:20 AM |
| needles and haystacks – how to find that one .doc file containing a specific word? Windows 7 | eNGiNe | Word | 0 | 04-01-2015 11:15 PM |
Word Macro: Save file as text with current file name
|
jabberwocky12 | Word VBA | 2 | 10-22-2010 12:23 PM |