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
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
P Prevent Outlook 2016 from using DASL filter Using Outlook 4
I Button PDF in Outlook Contact custom form Outlook VBA and Custom Forms 0
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
X Outlook automation pull from PDF Using Outlook 5
O Outlook 365 - How to create / copy a new contact from an existing one? Using Outlook 3
M Gmail address associated with Outlook on new phone Using Outlook 9
D Cannot populate certain UserProperties in Outlook from Excel Outlook VBA and Custom Forms 2
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
G Outlook 2016: Want IMAP Data Files on My D: Drive and Not C: Drive Using Outlook 1
V Validating Outlook form with "OR" and "AND" Outlook VBA and Custom Forms 1
D Outlook 2016 64bit, Cannot Save in 'HTML', format Using Outlook 1
J Connect outlook to office365 exchange _without_ signing in office apps Using Outlook 4
D Outlook launch email showing new unread email in reading pane Using Outlook 11
M Convertor for Outlook Express Mail Store (.dbx) to Outlook Mail Store (.pst) Using Outlook 0
K Outlook adds space to rows in HTML email signatures viewed on mobile Using Outlook 1
L Outlook 2010 Outlook 2010 Outlook VBA and Custom Forms 2
V Outlook behaving oddly Using Outlook 2
M HTML email signature in Outlook Using Outlook 1
C What folders are needed when reinstalling Outlook 2010 Using Outlook 0

Similar threads

Top