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
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
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
C Outlook 2007 Removing then adding account restores junk email processing Using Outlook 0
G Place jpg in body of email Outlook VBA and Custom Forms 1
F Wishlist Outlook suddenly began synchronizing deleted items every time I delete a single email. Using Outlook 2
N Save Selected Email Message as .msg File Outlook VBA and Custom Forms 12
HarvMan Toggle between calendar and email in Outlook 365 Using Outlook 7
F Email being marked as Spam by Gmail and not being visible in Outlook Using Outlook 5
G Email time stamp Using Outlook 2
G Schedule recurring email and attachments display Outlook VBA and Custom Forms 3
G Save and Rename Outlook Email Attachments Outlook VBA and Custom Forms 0
B Need to Copy an email to a subfolder Outlook VBA and Custom Forms 2
M How to setup outlook after importing old account information - Entering email account info creates with "(1)" after the account! Using Outlook 1
K Multiple Rules on Single Email Using Outlook 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
e_a_g_l_e_p_i Outlook 2010 How to set default email address for website links Using Outlook 3
O Same email address, same person, names in so many ways Using Outlook 4
D Create new email from the received Email Body with attachment Outlook VBA and Custom Forms 10

Similar threads

Top