Results 1 to 7 of 7
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    862
    Thanks
    0
    Thanked 10 Times in 9 Posts

    Send doc as Attachment (Office 12)

    I want users to be able to send documents which they have open in Word. Specifically, I would like to email the open letter to the main recipients (To) and to the CC and BCC groups, divided in the Outlook mail message as they are in the Word letter. I can easily get the email addresses of all of the contacts in the letter. (Letter is based on a distribution group, which is flagged in a custom property.) I have semi success with two methods. I can create a mail item and easily fill To, CC and BCC with the emai addresses. But I can't seem to specify an attachment (a clone of the current document which I make and store in the Temp folder).

    I have attached a Ref to the Outlook library to my project. My code:
    Set msg = Outlook.Application.CreateItem(olMailItem)
    msg.To = strTo
    msg.CC = strCC
    msg.BCC = strBCC
    msg.Display
    That works beautifully. msg.Attachments doesn't accept a string (file/path); perhaps it won't accept anything.

    The other method is to use ActiveDocument.SendMail. That creates a mail message with the current document attached to it. But there doesn't seem to be a way to fill the To, CC and BCC fields.

    I actually prefer the first method. Is there a way to specify the file to be attached?

    Many thanks,
    Richard Barrett

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 18 Times in 18 Posts

    Re: Send doc as Attachment (Office 12)

    Try

    msg.Attachments.Add "C:TempSomething.doc"

    You could also use

    With ActiveDocument.MailEnvelope.Item
    .To = "one@this.com"
    .Cc = "two@that.com"
    .Bcc = "three@other.com"
    End With
    ActiveWindow.EnvelopeVisible = True

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    862
    Thanks
    0
    Thanked 10 Times in 9 Posts

    Re: Send doc as Attachment (Office 12)

    Thank you, as always.

    Richard

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    862
    Thanks
    0
    Thanked 10 Times in 9 Posts

    Re: Send doc as Attachment (Office 12)

    I'm encountering an error today as I again use what worked well yesterday. I did get the same error yesterday, but retyping the line of code seemed to fix things (although I changed nothing.) The error is "Automation error: The specified module could not be found"

    My code (function + test sub) is below. I've detached and reattached the reference to the Outlook library, and that doesn't help.

    Sub NewOutlookMessage(sTo As String, sCC As String, sBCC As String, sFileToAttach As String)
    Dim olApp As Outlook.Application
    Dim mymsg As Outlook.MailItem

    Set mymsg = Outlook.Application.CreateItem(olMailItem) ' Error this line With mymsg
    .To = sTo
    .CC = sCC
    .BCC = sBCC
    If sFileToAttach <> "" Then
    .Attachments.Add sFileToAttach
    End If
    .Display
    End With

    End Sub

    Sub testmsg()
    NewOutlookMessage "richard@to.com", "cc@cc.com", "bcc@bcc.com", ""
    End Sub

    Thank you.
    Richard

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 18 Times in 18 Posts

    Re: Send doc as Attachment (Office 12)

    I assume that With mymsg should have been a separate line. With that change, the code works correctly for me in Outlook 2002. Perhaps someone who has Office 2007 can test the code. For completeness, here is the corrected code:

    Sub NewOutlookMessage(sTo As String, sCC As String, sBCC As String, sFileToAttach As String)
    Dim olApp As Outlook.Application
    Dim mymsg As Outlook.MailItem

    Set mymsg = Outlook.Application.CreateItem(olMailItem) ' Error this line
    With mymsg
    .To = sTo
    .CC = sCC
    .BCC = sBCC
    If sFileToAttach <> "" Then
    .Attachments.Add sFileToAttach
    End If
    .Display
    End With
    End Sub

    Sub testmsg()
    NewOutlookMessage "richard@to.com", "cc@cc.com", "bcc@bcc.com", ""
    End Sub

  6. #6
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    862
    Thanks
    0
    Thanked 10 Times in 9 Posts

    Re: Send doc as Attachment (Office 12)

    Thank you. Yes, that line apparnently jumped up during transmission.

    I just realized that this consistently functions as expected if Outlook is running. If Outlook is not running, the error always occurs. That's easy enough to control, but is it to be expected?

    Richard

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 18 Times in 18 Posts

    Re: Send doc as Attachment (Office 12)

    Yes. Search this forum for CreateObject and GetObject to see how to get a valid reference to Outlook.Application whether Outlook is running or not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •