Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > Including an Attachment in Outlook 97 mail msg from Excel


Reply
 
Thread Tools Display Modes
  #1  
Old 02-24-2003, 09:00 AM
NateBrei's Avatar
NateBrei NateBrei is offline
Contributor
 
Join Date: Jul 2002
Location: Omaha, NE
Posts: 571
Default [RESOLVED] Including an Attachment in Outlook 97 mail msg from Excel


I am automating a daily file updating process. I have the process working fine. Now, I would like to automate the process of attaching that file to an email. I've got the Excel macro so it sends out a message (recipient, subject, & body), but I can't get the attachment to work. I've tried doing F1 on the .Attachment, but all I get is that Outlook VBA isn't installed & instructions as to how to install them. The trouble is, they are on my machine & I've followed the instructions, but I still get the same help info on how to install.

Can anyone give me the syntax for using the .Attachment statement in the following code? Thanks in advance.
Code:
Sub EmailTest() Dim objOutlook As Object Dim objNS As Outlook.NameSpace Dim objMsg As Outlook.MailItem Dim intChar As Integer Set objOutlook = GetObject(, "Outlook.Application") Set objNS = objOutlook.GetNamespace("MAPI") strCurrentUser = objNS.CurrentUser intChar = InStr(1, strCurrentUser, ",") strCurrentUser = Mid(strCurrentUser, intChar + 2) Set objMsg = objNS.Application.CreateItem(olMailItem) With objMsg .Recipients.Add "Brei, Nathan" .Subject = "Test Email from Excel" .Body = "This is a test email from an Excel application" 'Errors on the next statement .Attachments = "H:\Temp\Junk.doc" .Send End With Set objMsg = Nothing Set objNS = Nothing
Nate

Last edited by NateBrei; 02-24-2003 at 10:41 AM.
Reply With Quote
  #2  
Old 02-24-2003, 10:34 AM
Wamphyri's Avatar
Wamphyri Wamphyri is offline
Variable not defined

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Ottawa, Ontario
Posts: 4,793
Default

Nate, a little hint use Attachments.Add "H:\Temp\Junk.doc"
__________________
-Carl
Reply With Quote
  #3  
Old 02-24-2003, 10:41 AM
NateBrei's Avatar
NateBrei NateBrei is offline
Contributor
 
Join Date: Jul 2002
Location: Omaha, NE
Posts: 571
Default

Duh, Carl... Thanks. That shouldn't have even been worth the time to write the initial post, have you or anyone else read it, or have you respond.

Sorry for wasting everyone's time. I feel ssssoooo....stupid. Of course, like all your suggestions, it works perfectly...

Nate
Reply With Quote
  #4  
Old 03-28-2003, 06:38 AM
Tiglet Tiglet is offline
Newcomer
 
Join Date: Mar 2003
Location: UK
Posts: 4
Default Send attachment using MAPI into Groupwise

I have been trying to do exactly the same thing except using MAPI as we have Novell Groupwise. This code uses message boxes for convenience, however, the attachment doesn't actually get attached - it is only attaching a reference to the file attached. Therefore, when the recipient tries to open it, Groupwise comes up with an error. Does anybody know how to solve this?
I am running Excel 2000 & Groupwise 5.5

Thanks

Tiglet


Sub MapiSendMail()
Dim objSession As Object
Dim objMessage As Object
Dim objRecipient As Object
Dim sProfile As String
Dim sSubjPrmpt As String
Dim sTextPrmpt As String
Dim sEmailPrmpt As String
Dim sMsgTitle As String
sProfile = "Novell Default Settings"
sEmailPrmpt = "Enter valid Email Name of message recipient:"
sSubjPrmpt = "Enter the subject line for this message:"
sTextPrmpt = "Enter the text for this message:"
sMsgTitle = "Mapi Macro"
' Create the Session Object.
Set objSession = CreateObject("MAPI.session")
' Log on using the session object.
objSession.Logon profileName:=sProfile
' Add a new message object to the OutBox.
Set objMessage = objSession.Outbox.Messages.Add
' Set the properties of the message object.
objMessage.Subject = InputBox(sSubjPrmpt, sMsgTitle)
objMessage.Text = InputBox(sTextPrmpt, sMsgTitle)
' Add a recipient object to the objMessage.Recipients collection.
objMessage.Attachments.Add ("c:\temp\B4000.xls")
Set objRecipient = objMessage.Recipients.Add
' Set the properties of the recipient object.
objRecipient.Name = InputBox(sEmailPrmpt, sMsgTitle)
objRecipient.Resolve
objMessage.Send showdialog:=False
MsgBox "Message sent successfully!"
' Log off using the session object.
objSession.Logoff
Reply With Quote
  #5  
Old 03-31-2003, 07:03 AM
NateBrei's Avatar
NateBrei NateBrei is offline
Contributor
 
Join Date: Jul 2002
Location: Omaha, NE
Posts: 571
Default

This is an edited version of what I ended up using for this application... It works to add an attachment (Excel file) to an Outlook message (accessed through an Excel VBA application). In the example below, strReportFileName is the filename that the user opened through an open dialog box earlier in the app. There are two differences I can see between your code & this. 1) You have quotes around your filename, but these are needed since you are providing the string. 2) You have parentheses around your filename. I do not. Perhaps that could make a difference...
Code:
With objMsg .Recipients.Add "LastName, FName" .Subject = "Daily Repricing Report (current through " & _ strCurrentThroughDate & ")" .Body = "Attached is the Daily Repricing Report (also containing " & _ "month-to-date) current through " & strCurrentThroughDate & "." .Body = .Body & strSignature .Attachments.Add strReportFileName .Send End With
Hope this helps.
Nate
Reply With Quote
  #6  
Old 03-31-2003, 08:26 AM
Wamphyri's Avatar
Wamphyri Wamphyri is offline
Variable not defined

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Ottawa, Ontario
Posts: 4,793
Default

I've never used Novell Groupwise but perhaps you could use the Mapi controls instead. Mapi Session and Mapi Messages.
Code:
MAPISession1.SignOn With MAPIMessages1 .SessionID = MAPISession1.SessionID .Compose .RecipAddress = "wamphryi.carl@vb.com" .ResolveName .MsgSubject = "Your Subject" .MsgNoteText = "Your Body" .AttachmentIndex = 0 .AttachmentPosition = 0 .AttachmentPathName = "C:\tempfile.xls" .Show .Send End With MAPISession1.SignOff
Or you depending on your needs you may only need to use the SendMail method from Excel's Object Library. I will automatically attach the workbook and send it to whom ever you specify.
Code:
ActiveWorkbook.SendMail recipients:="Jean Selva"
__________________
-Carl
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->