Link to home
Create AccountLog in
Avatar of antonioking
antoniokingFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Use VB in Outlook to save body as Word .doc

PLease can someone help me with some VB code to save create a word document from the contents of an email.

I want Outlook to automatically save emails with a particular subject that have been sent as a word doc to a specific folder. (say C:\Emails)

The document's name, will come from a reference number that is in the actual email.

It is the 7 digit number that appears after the words "Event Ref: "
Avatar of shorvath
shorvath
Flag of Canada image

try this:

    Dim outApp As Outlook.Application
    Dim outNameSpace As Outlook.NameSpace
    Dim outFolder As Outlook.Folder
    Dim outMailItem As Outlook.MailItem
    Dim RefNum As String
    
    Set outApp = CreateObject("Outlook.Application")
    Set outNameSpace = outApp.GetNamespace("MAPI")
    Set outFolder = outNameSpace.GetDefaultFolder(olFolderInbox)
   
    For Each outMailItem In outFolder.Items
        If outMailItem.Subject = "TEST" Then   'Check to see if Subject line = TEST
            If InStr(1, outMailItem.Body, "Event Ref:") > 0 Then
                RefNum = Mid(outMailItem.Body, InStr(1, outMailItem.Body, "Event Ref:"), 7)
                outMailItem.SaveAs "C:\Emails\" & RefNum & ".doc", olDoc
            End If
        End If
    Next outMailItem

Open in new window

Avatar of antonioking

ASKER

Thanks for this, do I need to create a rule or will this run on all emails being sent?
it will run on all emails sent if you put it inside the Application_ItemSend event.
ASKER CERTIFIED SOLUTION
Avatar of shorvath
shorvath
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks for this code, I will test tomorrow morning.
Works perfectly.
Thank you!