Outlook 2002 VBA code.

N

NakedJ

New to coding and need to write script for saving Outlook 2002 (sp3) e-
mail as seperate HTML Files. I feel like I am pretty close, but keep
getting a "Runtime Error 91". What am I missing?

Sub GetFolderContents()

Dim objOutlook As New Outlook.Application
Dim MyOrt As String
Dim objNameSpace As NameSpace
Dim objFolder As MAPIFolder
Dim objMail As Object

Dim i As Integer
'Ask for destination folder
MyOrt = InputBox("Destination", "Save Attachments", "C:\Documents
and Settings\jaflint\My Documents\e-mail")

Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
'Access the Inbox

For i = 1 To objFolder.Items.Count 'Loop through
all
strname = objMail.Subject
objMail.Subject = "Subject of mail message" 'Subject
line of
'message
objMail.Body = "Body of mail message" 'Body of
mail message

objMail.Saveas "MyOrt" & "strname & .HTML",
olHTML 'Save the message



Next
Set objFolder = Nothing
Set objNameSpace = Nothing
Set objOutlook = Nothing
Set objMail = Nothing
End Sub

Thanks,

Jason
 
S

Sue Mosher [MVP-Outlook]

What statement raises the error? Is this code running in Outlook VBA or somewhere else? Note that only messages in HTML format can be saved as HTML files .

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
N

NakedJ

What statement raises the error? Is this code running in Outlook VBA or somewhere else? Note that only messages in HTML format can be saved as HTML files .
Yes, this is running in VBA. The error seems to generate at the [Set
objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNameSpace.GetDefaultFolder(olFolderInbox)]
series.

I understand that only e-mails in HTML format can be saved. I have
tried to do this a number of ways, but keep failing at the last
minute. I have tried this code as well, but it says that a parameter
is not valid. (I can step through without errors).

Sub ExportAll()

Dim Message As MailItem
Dim myOrt As String
myOrt = InputBox("Destination", "Save Attachments", "C:\Documents and
Settings\jaflint\My Documents\email")
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
On Error Resume Next
For Each Message In myInbox.Items

Message.Saveas ("myOrt" & ".html"), olHTML

Next Message

End Sub

Thanks for the quick reply...

Jason
 
S

Sue Mosher [MVP-Outlook]

"Seems to generate"? If you step through the code, you should know *exactly* what statement generates the error. You don't need to use GetNamespace in Outlook VBA. You can use Application.Session to return a valid Namespace object.

You also still need to add code to check the BodyFormat property to determine whether it's an HTML format message.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers


NakedJ said:
What statement raises the error? Is this code running in Outlook VBA or somewhere else? Note that only messages in HTML format can be saved as HTML files .
Yes, this is running in VBA. The error seems to generate at the [Set
objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNameSpace.GetDefaultFolder(olFolderInbox)]
series.

I understand that only e-mails in HTML format can be saved. I have
tried to do this a number of ways, but keep failing at the last
minute. I have tried this code as well, but it says that a parameter
is not valid. (I can step through without errors).

Sub ExportAll()

Dim Message As MailItem
Dim myOrt As String
myOrt = InputBox("Destination", "Save Attachments", "C:\Documents and
Settings\jaflint\My Documents\email")
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
On Error Resume Next
For Each Message In myInbox.Items

Message.Saveas ("myOrt" & ".html"), olHTML

Next Message

End Sub

Thanks for the quick reply...

Jason
 

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