macro code change from excel to word

Hi Experts,

does anyone on here know how to change the coding below to extract emails into word rather than excel?

it is a macro that I use in outlook 2003 to extract emails into an excel spread sheet (2003).

Dim eMails(), noms() As String
Dim EmailFromBody As Boolean
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long



Sub GetEmail()
    Set rep = Outlook.Application.ActiveExplorer.CurrentFolder

    reponse = MsgBox("Do you want to extract emails from messages ?", vbYesNoCancel)
    If reponse = vbCancel Then
      Exit Sub
    ElseIf reponse = vbYes Then
      EmailFromBody = True
    Else
      EmailFromBody = False
    End If
    ReDim Preserve eMails(1), noms(1)
    eMails(1) = ""
    noms(1) = ""

    GetEmailFromFolder rep
    If eMails(1) <> "" Then
        NomFichier = GetTempDir() & "/emails.xls"
        Close #1
        Open NomFichier For Output As #1
            For i = 1 To UBound(eMails)
                Print #1, AfficheEmail(noms(i), eMails(i))
            Next
        Close #1
        MsgBox UBound(eMails) & " emails found in  " & rep, vbInformation, "Done"
        OpenExcel (NomFichier)
    Else
        MsgBox "No email found " & rep, vbInformation, "Done"
    End If

End Sub

Function AfficheEmail(Nom, email)
    email = Replace(email, "'", "")
    If Nom = "" Or Nom = "body" Then
       Nom = email
    End If
    AfficheEmail = email + vbTab + Nom + vcrlf
End Function

Sub GetEmailFromFolder(MyFolder)
    Dim myItemRec, MyItem As Object
    Dim myMailItem As Outlook.MailItem
  
    For Each MyItem In MyFolder.Folders
            GetEmailFromFolder MyItem
    Next

    rep = Replace(MyFolder.FolderPath, "\", vbTab)
  
    For Each MyItem In MyFolder.Items
        If TypeName(MyItem) = "MailItem" Then
   
            For Each myItemRec In MyItem.Recipients
                addMail myItemRec.Name & vbTab & "dest" & rep, myItemRec.Address
            Next
           
            addMail MyItem.SenderName & vbTab & "exp" & rep, MyItem.SenderEmailAddress
            
           If EmailFromBody Then findMail MyItem.body, rep
        End If
    Next
End Sub

Sub addMail(Nom, email)
    email = TrimEmail(email)
    Nom = Trim(Nom)
    If email <> "" And InStr(email, "@") > 0 And InStr(email, ".") > 0 Then
       
        Find = UBound(Filter(eMails, email, True, vbTextCompare))
        If eMails(1) = "" Then
            eMails(1) = email
            noms(1) = Nom
        ElseIf Find = -1 Then
           
            ReDim Preserve eMails(UBound(eMails) + 1)
            ReDim Preserve noms(UBound(noms) + 1)
            eMails(UBound(eMails)) = email
            noms(UBound(noms)) = Nom
        Else
        
            If Len(Nom) > Len(noms(Find)) And InStr(Nom, "@") = 0 Then
                noms(Find) = Nom
            End If
        End If
    End If
End Sub

Sub findMail(body, rep)
    at = InStr(body, "@")
    Do While at > 1
        D = at - 1
        Do While carOk(Mid(body, D, 1))
            D = D - 1
            If D = 0 Then
               Exit Do
            End If
        Loop
        F = at + 1
        Do While carOk(Mid(body, F, 1))
            F = F + 1
            If F = Len(body) Then
               Exit Do
            End If
        Loop
        If D < at - 3 And F > at + 4 Then
            addMail vbTab & "corps" & rep, Mid(body, D + 1, F - D - 1)
        End If
        at = InStr(at + 1, body, "@")
    Loop
End Sub

Function carOk(c)
    If c = "." Or c = "-" Or c = "_" Or (c >= "0" And c <= "9") Or (c >= "A" And c <= "Z") Or (c >= "a" And c <= "z") Then
        carOk = True
    Else
        carOk = False
    End If
End Function

Function carOkDebut(c)
    If c = "-" Or c = "_" Or (c >= "0" And c <= "9") Or (c >= "a" And c <= "z") Then
        carOkDebut = True
    Else
        carOkDebut = False
    End If
End Function

Function carOkFin(c)
    If (c >= "a" And c <= "z") Then
        carOkFin = True
    Else
        carOkFin = False
    End If
End Function

Function TrimEmail(email_ini)
  email = Trim(LCase(email_ini))
  D = Len(email)
  For i = 1 To D
    If Not carOkDebut(Left(email, 1)) Then
      email = Mid(email, 2, Len(email) - 1)
    Else
      Exit For
    End If
  Next i
  D = Len(email)
  For i = 1 To D
    If Not carOkFin(Right(email, 1)) Then
      email = Mid(email, 1, Len(email) - 1)
    Else
      Exit For
    End If
  Next i
  TrimEmail = email
End Function

Sub OpenExcel(FileName)
    Set xls = CreateObject("Excel.Application")
    xls.Workbooks.Open FileName
    xls.Visible = True
    Exit Sub
End Sub

Function GetTempDir() As String
    Dim buffer As String * 256
    Dim Length As Long
    Length = GetTempPath(Len(buffer), buffer)
    GetTempDir = Left(buffer, Length)
End Function

Open in new window

LVL 2
peggiegregAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

regmigrantCommented:
The code is not putting the emails directly into excel it is creating a text file then getting Excel to load it:

     
NomFichier = GetTempDir() & "/emails.xls"                      'sets up the filename
        Close #1                                                                                       ' checks the file isn't open already
        Open NomFichier For Output As #1                                          ' opens the file
            For i = 1 To UBound(eMails)
                Print #1, AfficheEmail(noms(i), eMails(i))                           'writes all the names, emails (as text!)
            Next
        Close #1                                                                                       ' close the file
        MsgBox UBound(eMails) & " emails found in  " & rep, vbInformation, "Done"
        OpenExcel (NomFichier)                                                              'Opens the file with excel

Open in new window

                                                               

If you use Windows Explorer to find that file you can right click and 'open with' Word (or notepad) etc.

However if you want to replicate current behaviour with Word you need to add Word under 'tools, references' and replace:

Sub OpenExcel(FileName)
    Set xls = CreateObject("Excel.Application")
    xls.Workbooks.Open FileName
    xls.Visible = True
    Exit Sub
End Sub

Open in new window


with
Sub OpenWord(FileName)
    Set wrd = CreateObject("word.Application")
    wrd.documents.add FileName
    wrd.Visible = True
    Exit Sub
End Sub

Open in new window


Then these lines have to change as well:

NomFichier = GetTempDir() & "/emails.xls"  to NomFichier = GetTempDir() & "/emails.doc"

OpenExcel (NomFichier)  to OpenWord (NomFichier)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peggiegregAuthor Commented:
I replaced this code and now it has opened in word, you've clearly shown what is happening so thank you for this.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.