Consulting

Results 1 to 10 of 10

Thread: Send email with attached file (vba)

  1. #1
    VBAX Newbie
    Joined
    Mar 2006
    Posts
    1
    Location

    Question Send email with attached file (vba)

    Helo everyone:

    I need some help to create a VBA code in excel that open outlook and attaching a file *.htm.
    I dont need the workbook just the *.htm file.-

    Thank so much .-

    Raraya

  2. #2
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Raraya,

    That is fairly simple:

    [VBA]
    Sub SendMessage()

    Dim olApp As Object
    Dim olMgs As Object

    Set olApp = CreateObject("Outlook.Application")
    Set olMsg = olApp.CreateItem(0)

    With olMsg
    .Subject = "some subject"
    .Body = "some body text"
    .To = "someone@someplace.com"
    .Attachments.Add "c:\folder\subfolder\file.thm"
    .Send
    End With

    Set olMsg = Nothing
    Set olApp = Nothing

    End Sub
    [/VBA]


    Of course, if you have Office 200 SP2 or later, you may have the Outlook automation security warning to deal with...
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  3. #3
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    221
    Location
    You think the " .Attachments.Add "c:\folder\subfolder\file.thm" " would work with Lotus ? That would save me a lot of time .... hmm ....

  4. #4
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    221
    Location
    Didn't work .... could you maybe try and help me w/ this one?

    [vba]
    Private Sub lotus()
    'On Error Resume Next
    'single Request
    Dim Maildb As Object 'The mail database
    Dim UserName As String 'The current users notes name
    Dim MailDbName As String 'The current users notes mail database name
    Dim maildoc As Object 'The mail document itself
    Dim Session As Object 'The notes session
    Dim Subject As String 'The subject string
    Dim ReturnReceipt As String 'The ReturnReceipt string
    Dim Recipient As String 'The Recipient string (or you could use the list)
    Dim ccPerson As String
    Dim Recip(10) As Variant 'The Recipient list
    Dim BodyText As String 'The body text
    Dim SaveIt As Boolean 'Save to sent mail
    Dim WasOpen As Integer 'Checking to see if the Mail DB was already
    Dim ClipBoard As DataObject
    Dim attachments As Object ' <<<<<<<< Is this what I would add?

    Subject = "Single Request From " & Sheets("sheet1").Range("B2").Value & _
    " For " & Sheets("sheet1").Range("B6").Value & "'s " & _
    Sheets("Sheet1").Range("B10").Value ' " Request"
    'put name if distro
    Recipient = "Workgroupinbox@company.com"
    ccPerson = txtCCPerson.Text

    SaveIt = True
    Set Session = CreateObject("Notes.NotesSession")
    UserName = Session.UserName
    MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - _
    InStr(1, UserName, " "))) & ".nsf"
    Set Maildb = Session.GETDATABASE("", MailDbName)
    If Maildb.IsOpen = True Then
    WasOpen = 1 'Already open for mail
    Else
    WasOpen = 0
    Maildb.OPENMAIL 'This will prompt you for password
    End If

    Sheets("sheet1").Activate
    Sheets("Sheet1").Range("A1:b13").Select
    'Range("A1:B13").Select
    Selection.Copy

    Set ClipBoard = New DataObject
    ClipBoard.GetFromClipboard

    Set maildoc = Maildb.CREATEDOCUMENT
    maildoc.Form = "Memo"
    maildoc.sendto = Recipient
    maildoc.copyto = ccPerson
    maildoc.Subject = Subject
    maildoc.body = ClipBoard.GetText
    maildoc.attachments.Add "C:\test.txt" ' <<<< added this in ...
    maildoc.ReturnReceipt = "1"
    maildoc.SAVEMESSAGEONSEND = SaveIt
    maildoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
    maildoc.Send 0, Recipient 'this sends memo


    End Sub
    [/vba]
    Last edited by debauch; 03-17-2006 at 06:17 PM.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Debauch
    Can you please insert linebreaks in your posted code as scrolling long lines is a bit of a nuisance.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    221
    Location
    Sorry mdm, Im not sure I know what u mean. I can reduce it though .

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    At the moment, your code needs to be scrolled. This can be a problem for those with a small screen resolution. If you break the code using the underscore character, this can be avoided. I'll edit it for you.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Contributor Ivan F Moala's Avatar
    Joined
    May 2004
    Location
    Auckland New Zealand
    Posts
    185
    Location
    Kind Regards,
    Ivan F Moala From the City of Sails

  9. #9
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    221
    Location
    I know this wasn't really my thread, so thanks Ivan, and thanks mdm, I will line break from now on.

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Is this solved?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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