Program email save as text file

Status
Not open for further replies.
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

Sue Mosher

"hlock" <hlock> wrote in message

news:C3834495-0AFC-4F23-9E26-F9E566FFFE18@microsoft.com...
> 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

>
 
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.

"Sue Mosher [MVP]" wrote:


> 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

> > Sue Mosher
> > >

> "hlock" <hlock> wrote in message
> news:C3834495-0AFC-4F23-9E26-F9E566FFFE18@microsoft.com...
> > 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]

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

"hlock" <hlock> wrote in message

news:7560FBF0-453A-4782-807A-17BF0564F00A@microsoft.com...
> 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.

> "Sue Mosher [MVP]" wrote:
>
> > 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
>

>> "hlock" <hlock> wrote in message
> > news:C3834495-0AFC-4F23-9E26-F9E566FFFE18@microsoft.com...
> > > 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
 
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.

"Sue Mosher [MVP]" wrote:


> 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
> > >

> "hlock" <hlock> wrote in message
> news:7560FBF0-453A-4782-807A-17BF0564F00A@microsoft.com...
> > 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.
> > "Sue Mosher [MVP]" wrote:
> >
> >> 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
> >
> >> "hlock" <hlock> wrote in message
> >> news:C3834495-0AFC-4F23-9E26-F9E566FFFE18@microsoft.com...
> >> > 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]

The GetCurrentItem() function at

http://www.outlookcode.com/codedetail.aspx?id=50 returns the currently

selected or open item. It assumes that Outlook is already running.

Sue Mosher

"hlock" <hlock> wrote in message

news:245139A8-94B5-40F4-A870-116210EEDD1B@microsoft.com...
> 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.

> "Sue Mosher [MVP]" wrote:
>
> > 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.
>

>> "hlock" <hlock> wrote in message
> > news:7560FBF0-453A-4782-807A-17BF0564F00A@microsoft.com...
> > > 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.
> >> > "Sue Mosher [MVP]" wrote:
> >> >> 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
> > >
>> >> "hlock" <hlock> wrote in message
> > >> news:C3834495-0AFC-4F23-9E26-F9E566FFFE18@microsoft.com...
> > >> > 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

>

>
>> .
> >
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
I There is no email program associated to perform requested action Using Outlook 0
A outlook 2007 - there is no email program associated ... Using Outlook 3
D How to set permissions to allow external program to send an email? Using Outlook 0
M Allow or Deny when a Program tries to send an email Outlook VBA and Custom Forms 1
S Email Organizer Program Outlook VBA and Custom Forms 2
J Program Checkbox that will activate a text box in a Outlook fallible form. Outlook VBA and Custom Forms 1
T Outlook "A program is trying to access Outlook" Using Outlook 3
Andrew Quirl Open attachment, manipulate without add-on program? Outlook VBA and Custom Forms 5
R SearchPublicFolders program Outlook VBA and Custom Forms 1
Diane Poremsky To Change the Default E-mail Program in Windows New Slipstick.com Articles 0
Diane Poremsky Trial/Beta program for BCM replacement BCM (Business Contact Manager) 8
J it goes like outlook has stopped working, a problem caused the program to stop working correctly, wi Using Outlook 2
J My Outlook program has stopped receiving. I can still send Using Outlook 1
C New version of MFCMAPI program Using Outlook 0
K A program is trying to send an e-mail message on your behalf... Using Outlook 1
F Default Program for Opening .pst Files ? Using Outlook 6
A Outlook-Program Not Closing Properly Using Outlook 10
B How to remove an old program from the Tools dropdown? Using Outlook 7
T getting an error This file does not have a program associated with it for performing this action. C Using Outlook 7
N The add-in "C:\program files\google\google desktop search\googledesktopO..." could not be installed Using Outlook 32
R Access Program Only Looping Part Way Through Outlook Inbox Outlook VBA and Custom Forms 2
R how to (re)set the default data file for Outlook in a VB program Outlook VBA and Custom Forms 2
J Program won't end in Win7 Outlook VBA and Custom Forms 1
D Outlook 2003 - A program is trying to access e-mail addresses Outlook VBA and Custom Forms 5
D Running a Program from Custom Action Rule Outlook VBA and Custom Forms 1
G GAL program acces in outlook 2007 Outlook VBA and Custom Forms 2
H Program "Click on Ribbon Button" Using VBA Outlook VBA and Custom Forms 1
M Run external program from Outlook VBA macro Outlook VBA and Custom Forms 5
N How to attach a file with MS Outlook, from a program? Outlook VBA and Custom Forms 3
Q why this program doesn't work if Outlook is not manually launched? Outlook VBA and Custom Forms 4
V how to switchoff program access message in outlook 2003 Outlook VBA and Custom Forms 1
V "A program has attachment filename open" Message returns Outlook VBA and Custom Forms 4
J Program Compatibility Issue - Outlook 2003 BCM BCM (Business Contact Manager) 4
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
P Sending email from outlook IMAP to GMAIL where embedded images are added as attachment Using Outlook 1
D auto forward base on email address in body email Outlook VBA and Custom Forms 0
P How did hacker cause link to delete and archive containing email? Using Outlook 6
I 'quote marks' around email name Using Outlook 1
N contact list seen in Contact folder but knot in Address book or when 'TO' button is clicked in new email Using Outlook 0
T Junk Email does not get added to the Blocked Sender List Using Outlook 0
P i-Phone 6s Plus receiving 2 notifications from Outlook email Using Outlook 1
C WARNING - DO NOT USE AN OUTLOOK.COM ADDRESS FOR GOOGLE ACCOUNT RECOVERY EMAIL Using Outlook 10
T Why is every new email pinned (to keep it on top)? Windows 10 Using Outlook 6
M Outlook, send to > mail recipient - results in plain text email Using Outlook 1
P Can I create a Rule that sends me an email when I get a Task? Using Outlook 3
M Issue transferring Outlook 2016 email accounts & settings onto new computer Using Outlook 8
R Auto Assign Category colours to Incoming Emails based on whom the email is addressed Outlook VBA and Custom Forms 3
S Outlook email to configure setup for each mail Outlook VBA and Custom Forms 1
S Macro to extract email addresses of recipients in current drafted email and put into clipboard Outlook VBA and Custom Forms 2
A Is there an ID field you can use to pair a reply to the sent email? Outlook VBA and Custom Forms 4

Similar threads

Top