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!
 
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.
 
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!
 
If you don't have the Word object model set as a reference, you need to use object, not application.

What happens when you try to save the file? The save doc file type link above shows how to do it.
 
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.
 
use this format: oWord.SaveAs "C:\email\" & sName & ".doc", olRTF
 
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
 
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
 
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.
 
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
 
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
S Create Outlook Task from Template and append Body with Email Body Outlook VBA and Custom Forms 4
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
X Custom icon (not from Office 365) for a macro in Outlook Outlook VBA and Custom Forms 0
Victor_50 Problem - Google Workspace will stop "unsafe" access to Outlook end 2024 Using Outlook 3
C New pc, new outlook, is it possible to import auto-complete emailaddress Using Outlook 2
T Outlook 365 won't take new working password Using Outlook 0
P Can't add custom field to custom Outlook form, it always adds to the Folder instead Outlook VBA and Custom Forms 2
B Sync Outlook Public Folders to Contacts Using Outlook 2
D Delete Outlook emails from MS server Using Outlook 9
B Outlook tasks and PDF Using Outlook 4
D Outlook 2019 is no longer asking for password ... Using Outlook 5
Kika Melo How to mark as Junk any message not from Contacts (in Outlook.com) Using Outlook 3
L Outlook attachments from OneDrive as links Using Outlook 0
G Outlook 365 My iCloud Outlook doesn’t work after reinstalling Microsoft365 on Windows 10 PC – now I get error message on contacts and calendar Using Outlook 1
T How to Export & Import GMAIL Contacts into Outlook 2021 ? Using Outlook 4
M Synchronization and backup of Outlook from local to server. Using Outlook 8
T How to get an EVENT COLOR option in Outlook 2021 ? Using Outlook 0
K How can I delete an e-mail from Outlook Using Outlook 1

Similar threads

Back
Top