Program email save as text file

  • Thread starter Thread starter hlock
  • Start date Start date
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
 
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

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


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


> .
>
 
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
S Email Generated from another program and then edited sends original email. Using Outlook 2
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
P Recommendation for PST repair program Using Outlook 1
P Outlook 365 Bad experience with KernelAPPS PST Repair program Using Outlook 0
P anyone use 4n6' Eudora Conversion program or other programs? Using Outlook 1
P How to clear out all contacts in iCloud, so I can use iCloud-based sync program Using Outlook 1
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 Using Outlook 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
Y QQ on Scheduled Delivery of an Email Using Outlook 0
T Replace Text in Email Subject Outlook VBA and Custom Forms 3
Rupert Dragwater Cannot reestablish gmail (email address) account in Outlook 365 Using Outlook 12
M Outlook 365 adding standard message in body of template email Outlook VBA and Custom Forms 3
E Create Rule to Forward Email if Subject Begins With Using Outlook 2
V iCloud For Windows v15.x - Missing Email Features ? Using Outlook 4
M Thunderbird email client - I wonder Using Outlook 1
D Outlook Desktop App Email Software Using Outlook 0
P Email and calendar entry text now shifts right about 3 tabs worth of space Using Outlook 1
J Outlook macro to run before email is being send Outlook VBA and Custom Forms 3
T Outlook 2010 Creating a email 'Group' in OL 2010 Using Outlook 2
D Send email from Outlook Alias using Mac? Using Outlook 0

Similar threads

Back
Top