Outlook body to word document

Status
Not open for further replies.

Joey Jeff

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Hello! There are about a million references on how to send an email from a word document, but nearly none on how to create a word document from an email automatically. I need a script that, using rules on outlook, reads a certain word in the subject line, and then automatically creates a word document with that emails body. I am incredibly new at macros and have no idea how to do this, so if you can spare a little extra time on how the code works i would be eternally grateful. Thanks!
 

Forum Admin

Senior Member
You either need to use a run a script rule to copy the mail body or save the message as a docx file.

This copies the body to a document

Code:
Sub SaveWord(Item As Outlook.MailItem)
   Dim oWord As Word.Application
   Set oWord = CreateObject("Word.Application")
   With oWord
     .Documents.Add
     .Selection.InsertBefore (Item.Body)
     .Visible = True
   End With

    Set oWord = Nothing
 
End Sub


This macro: http://www.slipstick.com/developer/convert-messages-rtf-format-save-doc-file-type/ - saves messages as a docx file type. It needs tweaked to work in a run a script rule though.
 

Joey Jeff

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Thanks! Im running into a couple of issues however, it works if i change "Dim oWord As Word.Application" to "Dim oWord As Object" and i have no idea why. When it is the former i get the error "Compile Error: User Type Not Defined". Also, i am running into issues saving the document into a folder. Thanks so much for your response!
 

Joey Jeff

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I had the both versions of the code "ActiveDocument.SaveAs Filename:='my path'" as well as "ActiveDocument.SaveAs 'My Path" as well as "oWord.Save as 'My Path'", and literally nothing happened. Word just opens and has the text from the email there, but it is not saved.
 

Joey Jeff

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Yeah, that didnt work either, it just opens the document with the body of the email there, and nothing happens. Also, i tried putting that line under the "with oWord" as ".SaveAs.....", and the document didnt even open at all, literally nothing happened. Thanks for helping me through this, its a bit frustrating haha
 

Joey Jeff

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Here is my code:
Sub SaveWord(Item As Outlook.MailItem)
Dim oWord As Object
Dim sName As String
Dim dtDate As Date
Set oWord = CreateObject("Word.Application")
sName = Item.Subject
dtDate = Item.ReceivedTime
With oWord
.Documents.Add
.Selection.InsertBefore (Item.Body)
.Visible = True
End With

oWord.SaveAs "C:\test\htdocs\worddocuments" & sName & dtDate & ".doc", olRTF

Set oWord = Nothing
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
First thing off the bat - it's saving it to C:\test\htdocs, with a filename of worddocumentsNameDate.doc

I'll test it next and see if it works here.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Oh, and you need to format the date and check the subject for illegal characters - windows won't save if the name is not valid.

Add this above the oword.saveas line then open the immediate window (on the View menu in the VB Editor) - send a tesat message so you can see what the macro is using for the name.
Debug.Print "C:\test\htdocs\worddocuments" & sName & dtDate & ".doc"

my filename was
C:\Users\dianep\Documents\print\Data Record 7/24/2014 14:42:087/24/2014 11:42:23 AM.doc

(my test message generator creates a message with a subject of "Data Record [Now]", so there are two dates in the filename, the one in the subject and one for the received time. )


I added these lines before With oWord
sName = sName & dtDate
ReplaceCharsForFileName sName, "_"

(and used C:\Users\dianep\Documents\print\" & sName & ".doc" to create the filename)

that turned my files name into
C:\Users\dianep\Documents\print\Data_Record_7_24_2014_14_48_337_24_2014_11_48_35_AM.doc

I'm, not sure why the admin is using the word object - saving the messages as rtf is all you really need to do. Well, it does include the message header - I guess that is why. You need to call more of the word object model if you want to save - the short snippet only opens the document onscreen.




Code:
Sub SaveWord(Item As Outlook.MailItem)

Dim sName As String
Dim dtDate As Date

   Item.BodyFormat = olFormatRichText
   sName = Item.Subject
   dtDate = Item.ReceivedTime
   sName = sName & dtDate
   ReplaceCharsForFileName sName, "_"

Debug.Print "C:\Users\dianep\Documents\print\" & sName & ".doc"
Item.SaveAs "C:\Users\dianep\Documents\print\" & sName & ".doc", olRTF

End Sub


Private Sub ReplaceCharsForFileName(sName As String, sChr As String)
  sName = Replace(sName, "/", sChr)
  sName = Replace(sName, "\", sChr)
  sName = Replace(sName, ":", sChr)
  sName = Replace(sName, "?", sChr)
  sName = Replace(sName, Chr(34), sChr)
  sName = Replace(sName, "<", sChr)
  sName = Replace(sName, ">", sChr)
  sName = Replace(sName, "|", sChr)
  sName = Replace(sName, "&", sChr)
  sName = Replace(sName, "%", sChr)
  sName = Replace(sName, "*", sChr)
  sName = Replace(sName, " ", sChr)
  sName = Replace(sName, "{", sChr)
  sName = Replace(sName, "[", sChr)
  sName = Replace(sName, "]", sChr)
  sName = Replace(sName, "}", sChr)
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This version saves just the body, not the header block, but it looks like sh** if the message has HTML links.

Code:
Sub SaveWord(Item As Outlook.MailItem)

Dim oWord As Object
Dim sName As String
Dim dtDate As Date
sName = Item.Subject
dtDate = Item.ReceivedTime

   sName = sName & dtDate
   ReplaceCharsForFileName sName, "_"
  
Debug.Print sName

Set oWord = CreateObject("Word.Application")

With oWord
.Documents.Add
.Selection.InsertBefore (Item.Body)
.Visible = True
End With

Debug.Print "C:\Users\dianep\Documents\print\" & sName & ".docx"

oWord.activedocument.SaveAs2 FileName:="C:\Users\dianep\Documents\print\" & sName & ".docx", FileFormat:= _
        wdFormatXMLDocument, CompatibilityMode:=15

Set oWord = Nothing

End Sub


Private Sub ReplaceCharsForFileName(sName As String, sChr As String)
  sName = Replace(sName, "/", sChr)
  sName = Replace(sName, "\", sChr)
  sName = Replace(sName, ":", sChr)
  sName = Replace(sName, "?", sChr)
  sName = Replace(sName, Chr(34), sChr)
  sName = Replace(sName, "<", sChr)
  sName = Replace(sName, ">", sChr)
  sName = Replace(sName, "|", sChr)
  sName = Replace(sName, "&", sChr)
  sName = Replace(sName, "%", sChr)
  sName = Replace(sName, "*", sChr)
  sName = Replace(sName, " ", sChr)
  sName = Replace(sName, "{", sChr)
  sName = Replace(sName, "[", sChr)
  sName = Replace(sName, "]", sChr)
  sName = Replace(sName, "}", sChr)
End Sub
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
kburrows Outlook Email Body Text Disappears/Overlaps, Folders Switch Around when You Hover, Excel Opens Randomly and Runs in the Background - Profile Corrupt? Using Outlook 0
S New Outlook Appointment - Select All Body Text and Change Font and Size Outlook VBA and Custom Forms 1
M White square in body of Outlook Messages (O2016 Version 2012 32bit Click To Run) Using Outlook 4
A How to get body of all emails in outlook 2016 to view in blue color Using Outlook 1
L dynamic and static dates in Outlook contact "notes" ie. body Using Outlook 2
P Insert link in email body to attached document in Outlook 365 Outlook VBA and Custom Forms 0
W Space in an Outlook appointment body Using Outlook 0
J VBA Outlook : Subject line : Cut and Paste name to heading , number to very end of the body of Email Outlook VBA and Custom Forms 1
E Copy e-mail body from outlook and insert into excel Outlook VBA and Custom Forms 3
C Transfer Outlook TextBox Text Into Email Body Outlook VBA and Custom Forms 2
K ind specific Subject line from outlook and copy the content of the email body to exce Outlook VBA and Custom Forms 0
D Outlook macro with today's date in subject and paste clipboard in body Outlook VBA and Custom Forms 1
K Extract email address from body and auto-reply outlook Using Outlook 1
K Display sub-folders in body of outlook Using Outlook 1
divan Outlook 2007 - Replace email body with custom text Using Outlook 9
K Importing appointment body from excel in outlook 2010 Using Outlook 1
Vijay Kumar Tables in outlook body Outlook VBA and Custom Forms 1
Aussie Looking for Outlook macro to Copy Recipient Names into Email Body Outlook VBA and Custom Forms 3
R Body of email not visible in Outlook Data File Sent Folder Using Outlook 4
H Using Outlook Rules to search for NewLines in message body Using Outlook 1
S Pre populate subject + body + attachment to already open email in outlook 2007 Using Outlook 2
I Outlook Appointments - Setting default text in message body Using Outlook 3
W Outlook body to Excel Document Using Outlook 35
P HTA creating Outlook MeetingItem - need formatted body text Using Outlook 4
M When I click “new message” in outlook, my cursor defaults to the body of the m Using Outlook 6
M Mail in HTML format can't be sent if url or number is in the body Outlook 2010 Using Outlook 2
L No body text in Outlook 2010 messages Using Outlook 0
S Blank body when replying with an Outlook Form. Using Outlook 21
W Recipients of Outlook messages have characters/words missing in body of message Using Outlook 6
S Clickable link on Outlook HTML Body Outlook VBA and Custom Forms 3
T How to get MailItem.Body without security warning in Outlook 2010 Outlook VBA and Custom Forms 2
T Can not change email body content on Outlook 2010 Outlook VBA and Custom Forms 2
S HELP: Create Buttons in Outlook Email body or Tool bar Outlook VBA and Custom Forms 1
A Outlook 2019 folder counter Using Outlook 0
A Relocate Search Bar in Outlook Using Outlook 0
e_a_g_l_e_p_i Need clarification on 2-Step Verification for Gmail using Outlook 2021 Using Outlook 9
L Opening People Outlook 2021 Using Outlook 2
e_a_g_l_e_p_i Outlook 2021 not letting me setup my Gmail using pop Using Outlook 1
Geldner Problem submitting SPAM using Outlook VBA Form Outlook VBA and Custom Forms 2
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
M Outlook 2016 outlook vba to look into shared mailbox Outlook VBA and Custom Forms 0
P Can no longer sync Outlook with iPhone calendar after iPhone update to 17.1.1 Using Outlook 2
O Outlook - Switch from Exchange to IMAP Using Outlook 2
e_a_g_l_e_p_i Is it possible to have a reminder in Outlook 2021 for every 90 days Using Outlook 3
farrissf Outlook 2016 Optimizing Email Searches in Outlook 2016: Seeking Insights on Quick Search vs Advanced Search Features Using Outlook 0
C Advanced search terms for "Outlook Data File" Using Outlook 1
N Reply to Outlook messages by moving messages to a specific Outlook folder Outlook VBA and Custom Forms 1
O How to find out the domain and server settings that my Outlook is using? Using Outlook 2
A Outlook 365 (OutLook For Mac)Move "On My Computer" Folder Items From Old To New Mac Computer Using Outlook 3
H Integrating Alexa & Outlook Pro 2021 Using Outlook 2

Similar threads

Top