How do I mass-extract email addresses out of several word documents at once?

Anonymous
2011-07-17T15:24:20+00:00

Hello All,

I have an important question.

How do I mass-extract email addresses out of several word documents at once?

I have a folder saved on my desktop with about 12000 word documents. All these documents have an email address in them but I want to know how to export all these email addresses without opening every single file one by one.

I hope somebody can help.

Kind regards,

EM

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2011-07-18T05:02:41+00:00

It is not possible to extract the e-mail addresses without opening the documents, but the process can be automated with a macro. The following may do what you want, so try it on a folder containing ONLY a small subset of the documents before attempting to try it on a huge number of files. The following macro will extract any e-mail address in the documents and write them, one to a line, to a new document. It will not make any permanent changes to the documents themselves.

NOTE: IT WILL CRASH IF ANY OF THE DOCUMENTS ARE PASSWORD PROTECTED

http://www.gmayor.com/installing_macro.htm

Sub BatchProcess()

Dim strFilename As String

Dim strPath As String

Dim oDoc As Document

Dim oNewDoc As Document

Dim oRng As Range

Dim hLink As Hyperlink

Dim fDialog As FileDialog

Set oNewDoc = Documents.Add

Options.AutoFormatReplaceHyperlinks = True

Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog

    .Title = "Select folder containing the documents and click OK"

    .AllowMultiSelect = False

    .InitialView = msoFileDialogViewList

    If .Show <> -1 Then

        MsgBox "Cancelled By User", , _

               "List Folder Contents"

        Exit Sub

    End If

    strPath = fDialog.SelectedItems.Item(1)

    If Right(strPath, 1) <> "" _

       Then strPath = strPath + ""

End With

If Left(strPath, 1) = Chr(34) Then

    strPath = Mid(strPath, 2, Len(strPath) - 2)

End If

strFilename = Dir$(strPath & "*.doc?")

While Len(strFilename) <> 0

    WordBasic.DisableAutoMacros 1

    Set oDoc = Documents.Open(strPath & strFilename)

    oDoc.Range.AutoFormat

    For Each hLink In oDoc.Hyperlinks

        If InStr(1, hLink.Address, "@") Then

            oNewDoc.Range.InsertAfter hLink.Range & vbCr

        End If

    Next hLink

    oDoc.Close SaveChanges:=wdDoNotSaveChanges

    WordBasic.DisableAutoMacros 0

    strFilename = Dir$()

Wend

oNewDoc.Paragraphs.Last.Range.Delete

oNewDoc.Range.Sort

oNewDoc.Range.Find.Execute FindText:="(*^13)@", _

                           MatchWildcards:=True, _

                           ReplaceWith:="\1", _

                           Replace:=wdReplaceAll

End Sub

Was this answer helpful?

10+ people found this answer helpful.
0 comments No comments

45 additional answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.6K Reputation points MVP Volunteer Moderator
    2016-12-28T00:54:13+00:00

    Insert

    oNewDoc.Range.Tables(1).Cell(1, 1).Range.InsertBefore strPath

    between

    oNewDoc.Range.ConvertToTable

    and

    oNewDoc.Range.Tables(1).Select

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-12-27T14:00:25+00:00

    Doug, many thanks. That worked beautifully!

    It's incredible how helpful you are.

    My deepest respect.

    I'd like to share my slightly modified (amateurish but still may be of use to somebody) variant:

    Changes:

    1. Part of the path where folder selection dialog starts can be fixed, so you won't have to go through all directory structure again and again
    2. Removes .doc* extension from the filenames it saves
    3. Copies result (table) in clipboard and quits after completion

    I'd like to add one more feature - save the name of the chosen folder (with all *.doc files) in the first row (left cell) of the table but haven't figured it yet.)

    Sub BatchProcess()

    Application.ScreenUpdating = True

    Dim strFilename As String

    Dim strPath As String

    Dim oDoc As Document

    Dim oNewDoc As Document

    Dim oRng As Range

    Dim hLink As Hyperlink

    Dim fDialog As FileDialog

    Set oNewDoc = Documents.Add

    Options.AutoFormatReplaceHyperlinks = True

    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

    With fDialog

        .Title = "Select folder containing the documents and click OK"

        .AllowMultiSelect = False

        .InitialFileName = "C:\Your path here" & ""

        If .Show <> -1 Then

            MsgBox "Cancelled By User", , _

                   "List Folder Contents"

            Exit Sub

        End If

        strPath = fDialog.SelectedItems.Item(1)

        If Right(strPath, 1) <> "" _

           Then strPath = strPath + ""

    End With

    If Left(strPath, 1) = Chr(34) Then

        strPath = Mid(strPath, 2, Len(strPath) - 2)

    End If

    strFilename = Dir$(strPath & "*.doc?")

    While Len(strFilename) <> 0

        WordBasic.DisableAutoMacros 1

        Set oDoc = Documents.Open(strPath & strFilename)

        oDoc.Range.AutoFormat

        For Each hLink In oDoc.Hyperlinks

            If InStr(1, hLink.Address, "@") Then

                oNewDoc.Range.InsertAfter oDoc.Name & vbTab & hLink.Range & vbCr

            End If

        Next hLink

        oDoc.Close SaveChanges:=wdDoNotSaveChanges

        WordBasic.DisableAutoMacros 0

        strFilename = Dir$()

    Wend

    oNewDoc.Paragraphs.Last.Range.Delete

    oNewDoc.Range.Sort

    oNewDoc.Range.Find.Execute FindText:="(*^13)@", _

                               MatchWildcards:=True, _

                               ReplaceWith:="\1", _

                               Replace:=wdReplaceAll

    oNewDoc.Range.Find.Execute FindText:=".doc*", _

                               MatchWildcards:=True, _

                               ReplaceWith:="", _

                               Replace:=wdReplaceAll

    oNewDoc.Range.ConvertToTable

    oNewDoc.Range.Tables(1).Select

    Selection.Copy

    ActiveWindow.Close

    Application.Quit

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 323.6K Reputation points MVP Volunteer Moderator
    2016-12-24T02:26:13+00:00

    Use:

    Sub BatchProcess()

    Dim strFilename As String

    Dim strPath As String

    Dim oDoc As Document

    Dim oNewDoc As Document

    Dim oRng As Range

    Dim hLink As Hyperlink

    Dim fDialog As FileDialog

    Set oNewDoc = Documents.Add

    Options.AutoFormatReplaceHyperlinks = True

    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

    With fDialog

        .Title = "Select folder containing the documents and click OK"

        .AllowMultiSelect = False

        .InitialView = msoFileDialogViewList

        If .Show <> -1 Then

            MsgBox "Cancelled By User", , _

                   "List Folder Contents"

            Exit Sub

        End If

        strPath = fDialog.SelectedItems.Item(1)

        If Right(strPath, 1) <> "" _

           Then strPath = strPath + ""

    End With

    If Left(strPath, 1) = Chr(34) Then

        strPath = Mid(strPath, 2, Len(strPath) - 2)

    End If

    strFilename = Dir$(strPath & "*.doc?")

    While Len(strFilename) <> 0

        WordBasic.DisableAutoMacros 1

        Set oDoc = Documents.Open(strPath & strFilename)

        oDoc.Range.AutoFormat

        For Each hLink In oDoc.Hyperlinks

            If InStr(1, hLink.Address, "@") Then

                oNewDoc.Range.InsertAfter oDoc.Name & vbTab & hLink.Range & vbCr

            End If

        Next hLink

        oDoc.Close SaveChanges:=wdDoNotSaveChanges

        WordBasic.DisableAutoMacros 0

        strFilename = Dir$()

    Wend

    oNewDoc.Paragraphs.Last.Range.Delete

    oNewDoc.Range.Sort

    oNewDoc.Range.Find.Execute FindText:="(*^13)@", _

                               MatchWildcards:=True, _

                               ReplaceWith:="\1", _

                               Replace:=wdReplaceAll

    oNewDoc.Range.ConvertToTable

    End Sub

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2016-12-23T14:19:17+00:00

    Thanks.

    You provided correction to original macros but I should say that original macros works fine with .doc.

    It won't do filenames, which is exactly the reason I asked for help on this forum.

    Your function is more advanced, it extracts filenames as well as e-mail addresses I suppose but it's working only with .docx type.

    And it happens so that I need to process multiple .doc files.

    Any help and assistance to solve this complication would be invaluable.

    Was this answer helpful?

    0 comments No comments