Program email save as text file

H

hlock

Using code from Word, we want to customize it for Outlook 2007. The goal of
the code is to save an email as a .txt file and prepare it for importing to
our document repository. Not familiar with programming, we have tried to
work with the code below. Now, it keeps getting stuck at message.save as
part. Any suggestions?

Sub SaveToIDM_Claim()
Dim fso
Dim Fil
Dim ns As Outlook.NameSpace
Dim message As MailItem
Dim ext As String
Dim filename As String
Dim tempfile As String
Dim tempdir As String
Dim path As String
Dim del As String ' import delete parameter
Dim app As String 'import application parameter

' Set fso = New FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
Set ns = GetNamespace("MAPI")
' Change AppName to an import parameter...
app = "/a=clmdoc"

' Save a copy of the email to a file of the same name
' but in the system's temporary directory...
tempdir = fso.GetSpecialFolder(2)
filename = "importemail"

' If an email has not yet been saved, it does not have an extension
' So add an .txt extension...
If fso.GetExtensionName(filename) = "" Then
filename = filename & ".txt"
End If

' Save the extension...
ext = fso.GetExtensionName(filename)
path = fso.BuildPath(tempdir, filename)

' If a file with than name already exists,
' start generating random file names until one does not exist...
Do While fso.FileExists(path)
tempfile = fso.GetTempName
tempfile = fso.GetBaseName(tempfile) & "." & ext
path = fso.BuildPath(tempdir, tempfile)
Loop

' Finally save a copy of the email...
message.SaveAs "path", olTXT

' If a file path string with embedded spaces is imported,
' it thinks each space delimits a new file name. To avoid this confusion,
' get the 8.3 format of the file path string...
Set Fil = fso.GetFile(path)
path = Fil.ShortPath
Set Fil = Nothing

ExecCmd "ttimport.exe " & app & " " & path

' Delete the file in the temporary directory...
' fso.DeleteFile path, True

Set fso = Nothing

End Sub
 
S

Sue Mosher [MVP]

This statement saves the message to a file literally named "path," because
you're using a string literal:

message.SaveAs "path", olTXT

When what you probably want is to use the string variable built in the
previous code statements:

message.SaveAs path, olTXT
 
H

hlock

Thanks. I changed it to read as:

message.SaveAs path, olTXT

I get a Run-time error '91': Object variable or With block variable not set.
Ideas?
I appreciate your help.
 
S

Sue Mosher [MVP]

There's no Set message = statement to instantiate the message object
variable, i.e. to associate it with a particular Outlook item. The details
of that statement depend on what item you want to save, information which
you have, but we don't.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
H

hlock

Understood. We'd like to be able to run the code when a message is open or
when we highlight a message in a folder. That's the ideal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top